【代码贡献】修复 QAOA 重复调用时沿用旧线路层数的问题 - #104
Open
youshen2025 wants to merge 1 commit into
Open
youshen2025 wants to merge 1 commit into
youshen2025 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
队伍 youshen,关联本源杯任务 #13,目标分支
develop。问题与结果
同一个
QAOA实例先调用一层run_qaoa_circuit,再传入两层角度时,原代码仍使用第一次缓存的
self.layer,静默忽略第二层。反过来从多层切到少层会触发
IndexError。这会使复用对象的深度扫描得到错误结果。下面第二次调用应等价于新实例的两层调用;在原版上,完整分布的最大绝对差约
0.10628。修复后与独立稠密状态演化逐项一致(绝对容差 1e-12)。
改动及接口约定
self.layer。ValueError,不再截断或出现索引异常;层数和成功运行计数保持不变,后续合法调用仍可正常执行。
run(layer=...)的正整数层数约定不变。
求解目标、优化策略或依赖。
数学验证与测试
测试对成本
0.2 + 0.7*x0 - 1.1*x1 + 1.3*x0*x1独立计算全部基态能量,逐层施加
diag(exp(-i*gamma*E))及解析RX(-2*beta)张量积,将完整概率与真实 PyQPanda3 CPUQVM 比较。数学基线不调用库的成本转换或线路构造函数。
覆盖符号/Pauli 输入,0/1/2/3 层增减,list/tuple/NumPy 角度,空/不等长序列,
错误输入后的恢复,以及真实 SLSQP 默认和 INTERP 优化与直接调用交替使用。
测试采用固定角度和精确概率,不依赖测量抽样、全局最优命中或模拟器 mock。
5f973ef:原有 18 项测试通过(7.58 s);16 项新回归全部失败。全部通过。失败为未修改的原有
Test_QUBO_GAS_origin_run.py随机 GAS 最优解断言,单独重跑该项一次通过(1.52 s)。两次记录均保留,未修改原测试。
运行方法(项目
.venv安装 PyQPanda3 与现有测试依赖后):OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 PYTHONPATH=pyqpanda-algorithm .venv/bin/python -m pytest -c test/pytest.ini -o addopts='' test/QAOA/Test_qaoa_repeated_depth.py --timeout=60 -q测试沿用
test/pytest.ini实际识别的Test_*.py命名,以便完整测试自动收集。使用 PyQPanda3 0.4.1、项目内 Python 3.11–3.13 虚拟环境,没有硬件或凭据要求。
查重及范围
2026-09-26 检索全部开放 PR 和含 layer/depth/层数的历史 PR,并核对 #40、#45、
#61、#96 的实际范围。#45 修复 QUBO 包装器的
layer=None默认值;本次处理QAOA 实例跨调用复用的线路深度,两者独立。本 PR 不包含已有寄存器、XY mixer、
风险目标或 GAS 修复,也不依赖这些尚未合并的 PR。
这是线路正确性修复,不声明量子优势、全局最优保证或官方 CI 已通过。
附加兼容性验证:临时 checkout
ca2e0ef合并当前应用 #92、四项独立库修复#95/#96/#97/#98 与本次修复,Python 3.12.3 的 266 项测试通过(33.97 s),
应用核心覆盖率 98.93%。这些其他 PR 的代码没有混入本分支;此为本地兼容
证据,不替代官方 CI 或维护者审核。