This release is a pre-release and may not be stable for production use.
Cqlib-QAOA
英文版见 English version。
本项目是由中电信量子集团开发团队打造的基于 Cqlib 的 QAOA 量子算法包,包含新建优化任务,生成哈密顿量,创建量子电路,执行量子电路,测量采样,返回优化结果等功能。
安装说明
环境要求:Python 3.10 及以上版本。
使用 pip 安装 cqlib-qaoa:
pip install cqlib-qaoa
结构说明
- algorithms:算法主体模块,内置 QAOA 算法主体、能量值评估和优化结果。
- ansatz:量子线路模块,内置 QAOA 专用线路生成。
- execution:算法执行后端模块,内置
LocalRunner后端(statevector 模拟器)和TianYanRunner后端(中电信“天衍”量子计算云平台)。 - mappings:问题模型转换模块,内置 QUBO 转换和代价哈密顿量生成。
- optimizers:优化器模块,内置
SPSA/COBYLA/Nelder-Mead优化器。 - problems:问题定义模块,内置
MaxCut/TSP/VRP三类典型优化问题模型。 - results:问题解码模块,内置
MaxCut/TSP/VRP三类典型问题的优化结果解码过程。 - transpiler:量子线路分解模块,提供 RZZ、RXX、RYY 等双量子比特旋转门的基础分解。
- visualization:可视化模块,内置迭代历史曲线、概率分布和问题模型可视化。
应用示例:使用 QAOA 求解 MaxCut 问题。
from cqlib_qaoa.problems import MaxCut
from cqlib_qaoa.mappings import maxcut_to_qubo, qubo_to_ising
from cqlib_qaoa.visualization import plot_maxcut
from cqlib_qaoa.execution import LocalRunner
from cqlib_qaoa.algorithms import QAOASolver, QAOAConfig
from cqlib_qaoa.optimizers 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()
如需在中电信“天衍”量子计算云平台上执行,可将 LocalRunner() 替换为:
from cqlib_qaoa.execution import TianYanRunner
runner = TianYanRunner(login_key="YOUR_KEY", machine="tianyan_sw")
License
Apache License 2.0,详见 LICENSE。
Contributing
欢迎贡献!如需改进功能或修复缺陷,请提交 Issue 或发起 Pull Request。
Metadata
Release files for cqlib-qaoa 0.1.0b1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cqlib_qaoa-0.1.0b1.tar.gz | 68.4 kB | Without provenance | Details for cqlib_qaoa-0.1.0b1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cqlib_qaoa-0.1.0b1-py3-none-any.whl | Python 3 | none | any | Details for cqlib_qaoa-0.1.0b1-py3-none-any.whl |
Total release size: 145.4 kB
Release files / cqlib_qaoa-0.1.0b1.tar.gz
| Download URL | cqlib_qaoa-0.1.0b1.tar.gz |
|---|---|
| Size | 68.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
216972b0dd79060b40edd6b79cdfeb65d7dded9c9d904d97277184cd448afc6d
|
|
BLAKE2b-256 checksum How to use checksums |
fa5d842a7df1a34f23797db0657039818056aa83fe9e8f9577a22ada58de141f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.20
|
Release files / cqlib_qaoa-0.1.0b1-py3-none-any.whl
| Download URL | cqlib_qaoa-0.1.0b1-py3-none-any.whl |
|---|---|
| Size | 77.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
72987f4dbc4c160a70e93322d03c62ab9511a8cf310bfeebae6e1b4273e95e64
|
|
BLAKE2b-256 checksum How to use checksums |
fab34b51fc12a4c708af11f26893dd5814d787fbd872e91ac754b36e1b829c40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.20
|
Security for cqlib-qaoa 0.1.0b1
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.