Prove2Me
Navigate
DiscoverCollectionsFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Shor's order-finding subroutine: each j/rj/rj/r is observed with probability ≥4/(π2r)\ge 4/(\pi^2 r)≥4/(π2r) and recovered by continued fractions

Proved
ShorWiki.order_finding_succeeds

by Lucas · Oct 13, 2026 · Mathlib 0df444a (Lean v4.33.1)

quantum-computingshor-algorithm

Let 1<a<N1 < a < N1<a<N be integers with gcd⁡(a,N)=1\gcd(a,N) = 1gcd(a,N)=1, let r=ord⁡N(a)r = \operatorname{ord}_N(a)r=ordN​(a) be the order of aaa modulo NNN, let n=⌈log⁡2N⌉n = \lceil \log_2 N\rceiln=⌈log2​N⌉, and let Q=22nQ = 2^{2n}Q=22n. Run Shor's order-finding circuit — quantum phase estimation for the modular-multiplication gate U∣k⟩=∣ak mod N⟩U|k\rangle = |ak \bmod N\rangleU∣k⟩=∣akmodN⟩ with a 2n2n2n-qubit first register — on the input ∣0⟩⊗2n⊗∣1⟩|0\rangle^{\otimes 2n}\otimes|1\rangle∣0⟩⊗2n⊗∣1⟩, and measure the first register, obtaining an outcome k∈{0,…,Q−1}k \in \{0, \dots, Q-1\}k∈{0,…,Q−1} with probability P(k)P(k)P(k).

Fix j∈{0,1,…,r−1}j \in \{0, 1, \dots, r-1\}j∈{0,1,…,r−1} and call an outcome kkk optimal for jjj if

∣k22n−jr∣≤122n+1,\left|\frac{k}{2^{2n}} - \frac{j}{r}\right| \le \frac{1}{2^{2n+1}},​22nk​−rj​​≤22n+11​,

i.e. kkk is a nearest integer to 22nj/r2^{2n} j / r22nj/r. The theorem states:

  1. the optimal outcomes for jjj are observed with total probability
∑k optimal for jP(k)  ≥  4π2 r;\sum_{k \text{ optimal for } j} P(k) \;\ge\; \frac{4}{\pi^2\, r};k optimal for j∑​P(k)≥π2r4​;
  1. for every outcome kkk optimal for jjj, some convergent of the continued fraction expansion of k/22nk/2^{2n}k/22n equals j/gcd⁡(j,r)r/gcd⁡(j,r)\dfrac{j/\gcd(j,r)}{r/\gcd(j,r)}r/gcd(j,r)j/gcd(j,r)​ in lowest terms.

Summing (1) over the rrr values of jjj, a single run of the subroutine yields, with probability at least 4/π2≈0.4054/\pi^2 \approx 0.4054/π2≈0.405, an outcome from which the continued fractions algorithm recovers the reduced fraction j/rj/rj/r for some jjj — and hence the divisor r/gcd⁡(j,r)r/\gcd(j,r)r/gcd(j,r) of the order rrr. This is the precise content of the article's claim that phase estimation "gives as output a random integer of the form jr22n\frac{j}{r}2^{2n}rj​22n" and that 2n2n2n qubits suffice for the continued fractions algorithm to recover it.

Formalization Note The probability P(k)P(k)P(k) is computed from the explicit circuit defined in ShorWiki_Defs (Hadamard gates, the controlled gates C-U2iC\text{-}U^{2^i}C-U2i, inverse QFT). The convergents are Mathlib's Real.convergent of the real number k/22nk/2^{2n}k/22n, and "recovers j/gcd⁡(j,r)j/\gcd(j,r)j/gcd(j,r) and r/gcd⁡(j,r)r/\gcd(j,r)r/gcd(j,r)" is expressed as equality of the numerator and denominator of that rational convergent. The value 4/(π2r)4/(\pi^2 r)4/(π2r) combines the article's two statements "balanced probability 1/r1/r1/r to find each ∣ϕj⟩|\phi_j\rangle∣ϕj​⟩" and "p22nj/r≥4/π2p_{2^{2n}j/r} \ge 4/\pi^2p22nj/r​≥4/π2".

Preamble
import Mathlib
import Definitions.Def_ShorWiki_Defs

open Matrix Kronecker
Formal statement
namespace ShorWiki

theorem order_finding_succeeds (N a : ℕ) (ha : 1 < a) (haN : a < N)
    (hcop : Nat.Coprime a N) (j : ℕ) (hj : j < ord N a) :
    4 / (Real.pi ^ 2 * ord N a) ≤
        (∑ k ∈ Finset.univ.filter (fun k : Fin (2 ^ (2 * regBits N)) =>
            |((k : ℕ) : ℝ) / 2 ^ (2 * regBits N) - (j : ℝ) / ord N a|
              ≤ 1 / 2 ^ (2 * regBits N + 1)),
          measureFirst (orderFindingOutput N a) k) ∧
      ∀ k : Fin (2 ^ (2 * regBits N)),
        |((k : ℕ) : ℝ) / 2 ^ (2 * regBits N) - (j : ℝ) / ord N a|
            ≤ 1 / 2 ^ (2 * regBits N + 1) →
          ∃ m : ℕ,
            (Real.convergent (((k : ℕ) : ℝ) / 2 ^ (2 * regBits N)) m).num
                = ((j / Nat.gcd j (ord N a) : ℕ) : ℤ) ∧
            (Real.convergent (((k : ℕ) : ℝ) / 2 ^ (2 * regBits N)) m).den
                = ord N a / Nat.gcd j (ord N a) := by sorry

end ShorWiki
Source
Wikipedia, "Shor's algorithm", revision oldid=1378862216, https://en.wikipedia.org/w/index.php?title=Shor%27s_algorithm&oldid=1378862216, sections "Quantum order-finding subroutine", "Quantum phase estimation", "Continued-fraction algorithm to retrieve the period" and "Choosing the size of the first register" (PDF pp. 3–6)
Human review
  • Endorsed by Shuze Chen · Oct 13, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Lucas · Oct 13, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me