Skip to content

【代码贡献】实现 Long 精确量子搜索算法,理论上达到零失败概率 - #106

Open
QuantumJourney2026 wants to merge 11 commits into
OriginQ:developfrom
QuantumJourney2026:feat/long-exact-grover
Open

QuantumJourney2026 wants to merge 11 commits into
OriginQ:developfrom
QuantumJourney2026:feat/long-exact-grover

Conversation

@QuantumJourney2026

Copy link
Copy Markdown

背景

标准Grover搜索算法的成功率与目标解在搜索空间中的占比相关。标准Grover算法仅在特定目标解比例下才能达到100%成功率;当目标解比例为1/2时,算法成功率下降至最低,仅为50%。然而,在一些特殊的搜索问题场景下,搜索算法理论上达到100%的成功率至关重要。精确量子搜索算法具有重要的应用价值。

工作内容

本PR实现了清华大学龙桂鲁教授于2001年所提出的精确量子搜索算法(Grover algorithm with zero theoretical failure rate, Phys. Rev. A 64, 022307)。基于论文理论,本PR实现了算法最优迭代次数与相位的自动计算,通过将Oracle和Diffusion算子中的π相位反转替换为任意相位旋转,克服标准Grover算法成功率随目标解比例的升高反而下降的缺陷,在仿真程序中实现了搜索成功率严格等于100%。

本次提交新增两个文件:

  1. pyqpanda_alg/Grover/Long_exact_grover.py:Long精确Grover算法核心实现,包含算法迭代次数、算法相位的理论计算公式等;
  2. example/QAlgBase/exact_grover_example.py:仿真示例,对比标准Grover与Long精确Grover的概率分布。

同时修订一个文件:

  1. pyqpanda_alg/Grover/__init__.py:导出LongExactGrover类,保持模块接口风格与原有Grover统一。

核心实现要点:

  1. 根据搜索空间中元素的总个数以及其中目标解的数目,自动计算最优迭代次数;
  2. 同步求解对应的算法相位φ,并在Oracle算子和Diffusion算子中统一使用该相位,而不是标准的π相位,使算法成功率达到100%;
  3. 复用仓库原有Grover线路框架,保持代码风格、调用接口对齐;
  4. 支持多目标解场景。

仿真验证结果

仿真环境:CPUQVM理想模拟器,搜索空间规模 N=16

Case1:N=16,目标解数量 M=1,标记态 1010

  • 标准Grover:迭代3次,目标态概率 P=0.9613
  • Long精确Grover:迭代3次,目标态概率 P=1.0000

相同迭代次数,Long精确Grover将概率提升至严格100%

Case2:N=16,目标解数量 M=2,标记态 1010、0110

  • 标准Grover:迭代2次,目标态总概率 P=0.9453
  • Long精确Grover:迭代2次,目标态总概率 P=1.0000

迭代次数与oracle调用次数保持不变,搜索成功率达到理论100%。

仿真结论: 在相同Oracle调用次数条件下,Long精确量子搜索算法可以把目标态概率提升至100%;而标准Grover算法则存在一定失败概率。

自测情况

本地运行示例脚本 exact_grover_example.py,仿真输出符合理论预期。

参考文献:G. L. Long, Grover algorithm with zero theoretical failure rate, Phys. Rev. A 64, 022307 (2001).

关联Issue:#13

English Description

Background

The success probability of the standard Grover search algorithm depends on the ratio of target solutions in the search space. It reaches 100% only at specific ratios; for example, when the ratio is 1/2, the success rate drops to its minimum of 50%. However, in certain critical search scenarios, a theoretical success rate of exactly 100% is essential, which motivates exact quantum search algorithms.

What This PR Does

This PR implements the exact quantum search algorithm proposed by Prof. Gui-Lu Long (Tsinghua University) in 2001 (Grover algorithm with zero theoretical failure rate, Phys. Rev. A 64, 022307). Based on the theory of arbitrary-phase amplitude amplification, this implementation:

  • Automatically computes the number of iterations and the matched phase from the search space size and the number of solutions;
  • Replaces the standard π phase flip with the arbitrary phase rotation (Z gate → U1(φ) gate) in both the Oracle and the Diffusion operators, which is the key to achieving a theoretical success rate of exactly 100%;
  • Reuses the existing Grover circuit framework of this repository (via composition with the original Grover class), keeping the code style and calling interface consistent;
  • Supports multiple target solutions (M ≥ 1).

Changes

Two new files:

  1. pyqpanda_alg/Grover/Long_exact_grover.py — core implementation of the Long exact Grover algorithm, including the theoretical formulas for the optimal iteration number and phase;
  2. example/QAlgBase/exact_grover_example.py — simulation example comparing the full probability distributions of standard Grover and Long exact Grover.
    One modified file:
  3. pyqpanda_alg/Grover/__init__.py — exports the LongExactGrover class, keeping the module interface style consistent with the original Grover.

Simulation Results

Environment: CPUQVM ideal simulator, search space size N = 16
Case 1: N = 16, M = 1, marked state 1010

  • Standard Grover: 3 iterations, success probability P = 0.9613
  • Long exact Grover: 3 iterations, success probability P = 1.0000
  • With the same number of iterations, the success probability is improved to exactly 100%.
    Case 2: N = 16, M = 2, marked states 1010, 0110
  • Standard Grover: 2 iterations, total success probability P = 0.9453
  • Long exact Grover: 2 iterations, total success probability P = 1.0000
  • The number of iterations and oracle calls stays unchanged, while the success rate reaches the theoretical 100%.
    Conclusion: under the same number of oracle calls, the Long exact quantum search algorithm raises the success probability to exactly 100%, whereas the standard Grover algorithm always retains a non-zero failure probability.

Self-Testing

The example script exact_grover_example.py was run locally; all simulation outputs match the theoretical predictions (both iteration counts and success probabilities).

Reference

G. L. Long, Grover algorithm with zero theoretical failure rate, Phys. Rev. A 64, 022307 (2001).

Related Issue: #13

OriginQuantumCloud and others added 11 commits January 28, 2026 16:21
- 新增 Grover/Long_exact_grover.py:将精确 Grover 搜索中 oracle 与扩散算子两处的 -1 相位翻转替换为匹配相位 exp(i*phi),使成功率严格为 1(基于 G. L. Long 任意相位 Grover 算法)
- Grover/__init__.py:导出 LongExactGrover
- 新增 example/QAlgBase/exact_grover_example.py:对比标准 Grover 与 LongExactGrover 的迭代次数、成功率与完整概率分布
- 删除废弃示例 testeg_grover_markdata.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants