【代码贡献】修正 Grover 最优迭代次数计算,补充输入校验与依赖声明 - #36
Open
CREVIOS wants to merge 1 commit into
Open
Conversation
Grover.iter_num used floor(pi/4 * sqrt(N/M)), which is the small-angle limit of the exact optimum and only holds while M << N. The success probability after k iterations is sin^2((2k+1)*theta/2) with theta = 2*arcsin(sqrt(M/N)), so the first maximum is at k* = pi/(2*theta) - 1/2 rounded to the nearest integer. Sweeping every (q_num, sol_num) with q_num <= 14 gives 4154 cases where the previous formula returned a strictly worse iteration count and no case where it was better. The largest gap is q_num=13, sol_num=5053, where the old value k=1 succeeds with probability 0.175 while the optimum k=0 succeeds with probability 0.617. The single-solution results that the small-angle limit already got right are unchanged, including the value shown in the docstring example. iter_num now also rejects sol_num outside [1, 2**q_num]; previously sol_num=0 raised ZeroDivisionError. mark_data_reflection silently marked the wrong state when a mark_data entry was longer than the qubit register, because only its lowest bits were consumed, and raised an opaque IndexError when it was shorter. It now validates the length and alphabet of every entry. It also emitted a BARRIER on each '1' position. Those do not change the state but block the transpiler from merging neighbouring gates; for a 6-qubit, 4-target reflection they cost 41 extra gates and 8 extra layers after transpilation at optimization level 2. They are removed, and a new test asserts the operator stays diagonal with -1 exactly on the marked states. Additionally: - requirements.txt: add pandas and scikit-learn. QSVD.py and QSVR.py import them at module scope, so `import pyqpanda_alg` failed with ModuleNotFoundError on a clean install. - test/pytest.ini: testpaths listed QRAM, which does not exist, while the existing QARM tests were never collected. - Re-enable Test_grover_mark_data_reflection.py, which was commented out in full but passes against the current API.
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.
关联 Issue:#13 (【本源杯项目】优化/新增算法、开发创新应用)
参赛队伍:DU_Fanta
一、问题描述
1.
Grover.iter_num返回的迭代次数并非最优iter_num使用floor(pi/4 * sqrt(N/M))。该式是最优解在M << N时的小角度近似,解集稠密时会失效。Grover 搜索经过
k次迭代后的成功概率为sin^2((2k+1)*theta/2),其中theta = 2*arcsin(sqrt(M/N)),因此第一个极大值出现在k* = pi/(2*theta) - 1/2(四舍五入取整)。遍历所有
q_num <= 14、1 <= sol_num < 2**q_num的组合:差距最大的情形为
q_num=13, sol_num=5053:稀疏解集(单解)下两者结果一致,文档示例
iter_num(3, 2) == 1不变。此外
sol_num=0原会抛出ZeroDivisionError。2.
mark_data_reflection会静默标记错误的量子态当
mark_data中某一项的长度大于量子比特数时,代码只取其低位,静默标记了错误的态,没有任何提示:长度不足时则抛出难以理解的
IndexError。3.
mark_data_reflection中的 BARRIER 阻碍编译优化原实现在每个
'1'位置插入BARRIER。它们不改变量子态(已验证酉矩阵完全一致),但会阻止转译器合并相邻门。以optimization_level=2转译后:4. 全新安装无法
import pyqpanda_algQSVD.py在模块层import pandas,QSVR.py在模块层from sklearn... import,但二者均不在requirements.txt中:5.
test/pytest.ini中testpaths配置有误testpaths列出了并不存在的QRAM,而实际存在的QARM测试从未被收集。二、修改内容
pyqpanda_alg/Grover/Grover_core.pyiter_num改用精确最优公式并校验参数;mark_data_reflection校验mark_data长度与字符集,移除 BARRIER;同步更新 docstringpyqpanda-algorithm/requirements.txtpandas、scikit-learntest/pytest.iniQRAM更正为QARMtest/QAlgBase/Test_grover_iter_num.pytest/QAlgBase/Test_grover_mark_data_reflection.pyTest_grover_mark_data_reflection.py此前被整体注释,但在当前 API 下可以正常通过,本 PR 予以恢复。三、对用户的影响
iter_num在稠密解集下的返回值会变化(变得更优)。稀疏解集下结果不变。iter_num与mark_data_reflection新增了ValueError。原先静默产生错误结果或抛出ZeroDivisionError/IndexError的调用,现在会得到明确报错。这是有意的行为变更:静默标记错误的量子态比报错更危险。mark_data_reflection生成的电路不再含 BARRIER,酉矩阵不变。四、测试
修改前为 18 个用例,本 PR 新增 12 个、恢复 4 个。新增测试覆盖:
iter_num在q_num <= 10全部组合上等于成功概率的第一个极大值点;iter_num返回值的左右邻域成功概率均不更优;q_num=13, sol_num=5053);sol_num/q_num抛出ValueError;mark_data_reflection的酉矩阵为对角阵且仅在被标记态上为 -1;mark_data长度、字符集、空列表的校验;另已验证全新虚拟环境中
pip install .后可正常import pyqpanda_alg。English summary
Grover.iter_numused the small-angle limitfloor(pi/4 * sqrt(N/M))instead of the exact first maximumround(pi/(2*theta) - 1/2). Across allq_num <= 14, the new implementation is strictly better in 4154 cases and worse in none; worst previous caseq_num=13, sol_num=5053went from success probability 0.175 to 0.617.mark_data_reflectionsilently marked the wrong state when amark_dataentry was longer than the qubit register; it now validates length and alphabet.mark_data_reflectionthat do not change the unitary but block transpiler optimisation (up to 41 gates / 8 layers for a 6-qubit, 4-target reflection).pandasandscikit-learntorequirements.txt;import pyqpanda_algfailed on a clean install without them.testpathsintest/pytest.ini(QRAM->QARM) so the existing QARM tests are collected.Test_grover_mark_data_reflection.py, which was fully commented out but passes against the current API.Test suite: 18 -> 34 passing.