Skip to main content

Cqlib Algorithm

本项目是由中电信量子集团开发团队打造的基于 Cqlib 的量子算法包,包含新建优化任务,生成哈密顿量,创建量子电路,执行量子电路,测量采样,返回优化结果等功能,目前已实现 QAOA,后续将持续集成 VQE 等量子算法。


安装说明

使用 pip 下载 cqlib-algorithm 包:

pip install cqlib-algorithm

结构说明

  • algorithms:算法主体模块,内置QAOA算法主体、能量值评估、优化结果。
  • ansatz:量子线路模块,内置QAOA专用线路生成。
  • execution:算法执行后端模块,内置LocalRunner后端(statevector模拟器)、TianYanRunner后端( 中电信“天衍”量子计算云平台 )。
  • mappings:问题模型转化模块,内置QUBO转化、代价哈密顿量生成。
  • optimizers:优化器模块,内置 SPSA / COBYLA / Nelder-Mead优化器。
  • problems:问题定义模块,内置MaxCut / TSP / VRP三类典型优化问题模型。
  • results:问题解码模块,内置MaxCut / TSP / VRP三类典型问题的优化结果解码过程。
  • visualization:可视化模块,内置量子线路可视化、迭代历史曲线可视化、概率分布可视化、问题模型可视化。

应用示例:使用 QAOA 求解 MaxCut 问题。

from cqlib_algorithm.problems.maxcut import MaxCut
from cqlib_algorithm.mappings.convert import maxcut_to_qubo, qubo_to_ising
from cqlib_algorithm.visualization.maxcut_plot import plot_maxcut
from cqlib_algorithm.execution import LocalRunner, TianYanRunner
from cqlib_algorithm.algorithms.qaoa import QAOASolver, QAOAConfig
from cqlib_algorithm.optimizers.options import OptimizerOptions

def main():
    # ---- 定义 Maxcut 问题 ----
    weights = {(0,1):1, (1,2):1, (2,3):1, (3,0):1}
    mc = MaxCut(n=4, weights=weights)

    # ---- 绘制模型图 ----
    plot_maxcut(n=mc.n, weights=mc.weights, partition={}, title="MaxCut Problem")

    # ---- Maxcut -> QUBO ----
    qubo  = maxcut_to_qubo(mc)
    print(qubo)

    # ---- QUBO -> Ising ----
    ising = qubo_to_ising(qubo)
    print(ising)

    # ---- 选择优化器 ----
    # SPSA
    opt_cfg = OptimizerOptions(name="spsa", options={"maxiter": 100, "a": 0.2, "c": 0.2})

    # ---- QAOA求解 ----
    solver = QAOASolver(ising, 
                        runner=LocalRunner(), 
                        qaoa_cfg=QAOAConfig(reps=5, mixer="x"), 
                        opt_cfg=opt_cfg)

    res = solver.run()

    # ---- 优化结果 ----
    # 1) 结果打印
    res.print_result()
    
    # 2) 收敛曲线可视化
    res.plot_history(title="Optimization History")

    # 3) 概率分布可视化
    res.plot_probability(title="QAOA Probability (best θ)", topk=20)

    # 4) 解决方案可视化
    res.plot_maxcut_solution(n=mc.n, weights=mc.weights, title="MaxCut Solution(QAOA)")

if __name__ == "__main__":
    main()

License

Apache License 2.0,详见 LICENSE。


Contributing

欢迎贡献!如需改进功能或修复缺陷,请提交 Issue 或发起 Pull Request。


Metadata

Release files for cqlib-algorithm 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cqlib-algorithm 0.1.0
File Size Uploaded Provenance Actions
cqlib_algorithm-0.1.0.tar.gz 48.9 kB Without provenance Details for cqlib_algorithm-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cqlib-algorithm 0.1.0
File Interpreter ABI Platform Actions
cqlib_algorithm-0.1.0-py3-none-any.whl Python 3 none any Details for cqlib_algorithm-0.1.0-py3-none-any.whl

Total release size: 126.5 kB

Release files / cqlib_algorithm-0.1.0.tar.gz

Download URL cqlib_algorithm-0.1.0.tar.gz
Size 48.9 kB
Tags Source
SHA-256 checksum
How to use checksums
57d1b64082c1181d68985fceb2fcdd96242a4eceef59c0bdd21eb33565f60c7e
BLAKE2b-256 checksum
How to use checksums
5dfa9e8319c409702e801fad60f7fa901524593fe96c36d24c7865fcd943adf2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.17

Release files / cqlib_algorithm-0.1.0-py3-none-any.whl

Download URL cqlib_algorithm-0.1.0-py3-none-any.whl
Size 77.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d89d030e20708f2b687f4e0b4d56e388b6e32e99af0d8a2f78b5454fd981e690
BLAKE2b-256 checksum
How to use checksums
70c918945be4c7f93f5bbbaa7cfb2bfc5c0d3c5570a8747ad44f903a343a94dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.17

Security for cqlib-algorithm 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page