Documentation

CslibFrontier.Algorithms.Quantum.PeriodFinding

Exact quantum period finding #

This module formalizes the quantum core of Shor's algorithm in the case where the period divides the size of the Fourier register (Shor, §5; de Wolf, §5.3, "Easy case: r divides q").

The problem. A function f : ZMod N → S has exact period r (Cslib.Quantum.HasExactPeriod) when r ∣ N and f x = f x' exactly when x.val ≡ x'.val [MOD r]. For instance a ↦ x^a mod M on ZMod N has exact period the order r of x modulo M whenever r ∣ N (Cslib.Quantum.hasExactPeriod_pow).

The algorithm. Start in |0⟩|s₀⟩, apply the quantum Fourier transform (Cslib.Quantum.qft) to the first register, apply an oracle unitary U with U|x, s₀⟩ = |x, f x⟩, and obtain the state N^{-1/2} ∑_x |x⟩|f x⟩ (Cslib.Quantum.oracleState; Cslib.Quantum.unitaryEquiv_oracle_qft_ket_zero; Shor, eqs. (5.1)–(5.2)). The standard oracle |x, s⟩ ↦ |x, s + f x⟩ (Cslib.Quantum.addOracle) is one such unitary. Apply the Fourier transform to the first register again (Cslib.Quantum.periodState, Shor eqs. (5.3)–(5.4)) and measure the first register (Cslib.Quantum.periodFindingOutcome).

The analysis. The amplitude of |y, f x₀⟩ is N⁻¹ ∑_{x : f x = f x₀} ω^{xy}, a character sum over the coset x₀ + ⟨r⟩, whose squared modulus is the probability of Shor's eq. (5.5). By orthogonality over the subgroup ⟨r⟩ (ZMod.sum_dvd_val_stdAddChar_mul) it is ω^{x₀ y} / r when N / r divides y, and 0 otherwise (Cslib.Quantum.HasExactPeriod.periodState_apply). Since f takes r values, each multiple of N / r is measured with probability exactly 1 / r: the outcome is uniformly distributed on the r multiples of N / r (Cslib.Quantum.HasExactPeriod.periodFindingOutcome_eq; for the whole circuit (F_N ⊗ I) U (F_N ⊗ I)|0, s₀⟩, Cslib.Quantum.HasExactPeriod.measure_period_circuit).

Classical recovery. An outcome y = c · N / r with 0 ≤ c < r satisfies y / N = c / r, so the reduced denominator N / gcd(N, y) of y / N equals r exactly when c is coprime to r (Shor, §5; de Wolf §5.3). Equivalently, y has additive order r in ZMod N, and exactly φ(r) of the multiples do, so the recovery succeeds with probability exactly φ(r) / r (Cslib.Quantum.HasExactPeriod.periodFindingOutcome_recover). An elementary bound on φ(r) / r (Nat.le_totient_mul_log_add_one) makes this at least 1 / (log₂ r + 1) (Cslib.Quantum.HasExactPeriod.inv_log_add_one_le_periodFindingOutcome_recover).

Scope. This is the special case r ∣ N of Shor's analysis, which takes N = q a power of two with n² ≤ q < 2n² and handles r ∤ q by continued fractions; that case is not formalized. The Fourier transform and the oracle are unitary matrices, not gate-level circuits, so no gate counts, uniformity or approximation costs are claimed, and nothing here proves that factoring is in BQP. The recovery bound 1 / (log₂ r + 1) is weaker than the Ω(1 / log log r) of Shor and de Wolf, which rests on Hardy and Wright's Theorem 328 and is not formalized. The same computation solves the hidden subgroup problem for the subgroup ⟨r⟩ of ZMod N: the outcomes are uniform on its annihilator.

Main definitions #

Main results #

References #

structure Cslib.Quantum.HasExactPeriod {N : ℕ} {S : Type u_1} (f : ZMod N → S) (r : ℕ) :

f : ZMod N → S has exact period r: r divides N, and f x = f x' exactly when x and x' are congruent modulo r. This is the promise of exact period finding (de Wolf, §5.3, the case where r divides q).

theorem Cslib.Quantum.HasExactPeriod.pos {N : ℕ} [NeZero N] {S : Type u_1} {f : ZMod N → S} {r : ℕ} (h : HasExactPeriod f r) :
0 < r
theorem Cslib.Quantum.HasExactPeriod.eq_iff_sub_mem {N : ℕ} [NeZero N] {S : Type u_1} {f : ZMod N → S} {r : ℕ} (h : HasExactPeriod f r) (x x' : ZMod N) :
f x = f x' ↔ x' - x ∈ AddSubgroup.zmultiples ↑r

Under exact period r, f x = f x' exactly when x' - x lies in the subgroup generated by r.

theorem Cslib.Quantum.HasExactPeriod.card_image {N : ℕ} [NeZero N] {S : Type u_1} {f : ZMod N → S} {r : ℕ} [DecidableEq S] (h : HasExactPeriod f r) :

A function with exact period r takes exactly r values.

theorem Cslib.Quantum.hasExactPeriod_pow {N : ℕ} {G : Type u_2} [LeftCancelMonoid G] {x : G} (hx : orderOf x ∣ N) :
HasExactPeriod (fun (a : ZMod N) => x ^ a.val) (orderOf x)

Order finding is exact period finding when the order divides N: if orderOf x ∣ N, then a ↦ x ^ a.val has exact period orderOf x (Shor, §5, with x^a mod n in the group of units of ZMod n).

State preparation #

noncomputable def Cslib.Quantum.oracleState {N : ℕ} {S : Type u_1} [DecidableEq S] (f : ZMod N → S) :

The state N^{-1/2} ∑_x |x⟩|f x⟩ after one query to f on a uniform superposition (Shor, eq. (5.2)).

theorem Cslib.Quantum.oracleState_apply {N : ℕ} {S : Type u_1} [DecidableEq S] (f : ZMod N → S) (p : ZMod N × S) :
(oracleState f).ofLp p = if f p.1 = p.2 then ↑(√↑N)⁻¹ else 0
theorem Cslib.Quantum.norm_oracleState {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] (f : ZMod N → S) :
theorem Cslib.Quantum.unitaryEquiv_onFirst_qft_ket_zero {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] (s₀ : S) :
(unitaryEquiv (onFirst (qft N))) (ket (0, s₀)) = ∑ x : ZMod N, ↑(√↑N)⁻¹ • ket (x, s₀)

The Fourier transform of |0⟩ is the uniform superposition: (F_N ⊗ I)|0, s₀⟩ = N^{-1/2} ∑_x |x, s₀⟩ (Shor, eq. (5.1)).

theorem Cslib.Quantum.unitaryEquiv_oracle_qft_ket_zero {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] (U : ↥(Matrix.unitaryGroup (ZMod N × S) ℂ)) (s₀ : S) {f : ZMod N → S} (hU : ∀ (x : ZMod N), (unitaryEquiv U) (ket (x, s₀)) = ket (x, f x)) :

State preparation (Shor, eqs. (5.1)–(5.2)): for any oracle unitary U with U|x, s₀⟩ = |x, f x⟩, applying the Fourier transform to the first register of |0, s₀⟩ and then U gives N^{-1/2} ∑_x |x⟩|f x⟩.

noncomputable def Cslib.Quantum.addOracle {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] [AddGroup S] (f : ZMod N → S) :

The standard oracle |x, s⟩ ↦ |x, s + f x⟩ of a function into an additive group, the permutation matrix of a shear of ZMod N × S.

theorem Cslib.Quantum.unitaryEquiv_addOracle_ket {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] [AddGroup S] (f : ZMod N → S) (x : ZMod N) (s : S) :
(unitaryEquiv (addOracle f)) (ket (x, s)) = ket (x, s + f x)

With the standard oracle and s₀ = 0, Fourier transform and oracle prepare N^{-1/2} ∑_x |x⟩|f x⟩.

The final state and its measurement #

noncomputable def Cslib.Quantum.periodState {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] (f : ZMod N → S) :

The state (F_N ⊗ I) N^{-1/2} ∑_x |x⟩|f x⟩ before the final measurement (Shor, eq. (5.4)).

theorem Cslib.Quantum.norm_periodState {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] (f : ZMod N → S) :
theorem Cslib.Quantum.periodState_apply {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] (f : ZMod N → S) (y : ZMod N) (s : S) :
(periodState f).ofLp (y, s) = (↑N)⁻¹ * ∑ x : ZMod N, if f x = s then ZMod.stdAddChar (x * y) else 0

The amplitude of |y, s⟩ in periodState f is N⁻¹ ∑_{x : f x = s} ω^{xy}.

theorem Cslib.Quantum.periodState_apply_of_forall_ne {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] {f : ZMod N → S} {s : S} (hs : ∀ (x : ZMod N), f x ≠ s) (y : ZMod N) :

Values that f never takes have amplitude 0.

theorem Cslib.Quantum.HasExactPeriod.periodState_apply {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] {f : ZMod N → S} {r : ℕ} (h : HasExactPeriod f r) (x₀ y : ZMod N) :
(periodState f).ofLp (y, f x₀) = if N / r ∣ y.val then (↑r)⁻¹ * ZMod.stdAddChar (x₀ * y) else 0

The final amplitudes under exact period r, in the case r ∣ N: the amplitude of |y, f x₀⟩ is ω^{x₀ y} / r when N / r divides y, and 0 otherwise. Its squared modulus is the probability of Shor, eq. (5.5) (HasExactPeriod.norm_periodState_apply_sq).

theorem Cslib.Quantum.HasExactPeriod.norm_periodState_apply_sq {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] {f : ZMod N → S} {r : ℕ} (h : HasExactPeriod f r) (x₀ y : ZMod N) :
‖(periodState f).ofLp (y, f x₀)‖ ^ 2 = if N / r ∣ y.val then (↑r ^ 2)⁻¹ else 0

Under exact period r, each joint outcome (y, f x₀) has probability 1 / r² when N / r divides y, and 0 otherwise: the probability of Shor, eq. (5.5), in the case r ∣ N.

noncomputable def Cslib.Quantum.periodFindingOutcome {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] (f : ZMod N → S) :
PMF (ZMod N)

The distribution of the outcome of measuring the first register of periodState f: the marginal on ZMod N of the computational-basis measurement of both registers.

theorem Cslib.Quantum.HasExactPeriod.periodFindingOutcome_apply {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] {f : ZMod N → S} {r : ℕ} (h : HasExactPeriod f r) (y : ZMod N) :

Exact period finding (de Wolf, §5.3, the case where r divides q): measuring the first register gives y with probability 1 / r if N / r divides y, and 0 otherwise.

Exact period finding, as an equality of distributions: the measured value is uniformly distributed on the r multiples of N / r.

theorem Cslib.Quantum.HasExactPeriod.measure_period_circuit {N : ℕ} [NeZero N] {S : Type u_1} [Fintype S] [DecidableEq S] {f : ZMod N → S} {r : ℕ} (h : HasExactPeriod f r) (U : ↥(Matrix.unitaryGroup (ZMod N × S) ℂ)) (s₀ : S) (hU : ∀ (x : ZMod N), (unitaryEquiv U) (ket (x, s₀)) = ket (x, f x)) :

Exact period finding, end to end: for any oracle unitary U with U|x, s₀⟩ = |x, f x⟩, measuring the state (F_N ⊗ I) U (F_N ⊗ I)|0, s₀⟩ and reading the first register gives a uniformly distributed multiple of N / r (Shor, eqs. (5.1)–(5.5), in the case r ∣ N; de Wolf, §5.3).

Classical recovery (Shor, §5; de Wolf, §5.3): the reduced denominator N / gcd(N, y) of y / N is the period r with probability exactly φ(r) / r.

The recovery of Cslib.Quantum.HasExactPeriod.periodFindingOutcome_recover succeeds with probability at least 1 / (log₂ r + 1), by the elementary bound r ≤ φ(r) (log₂ r + 1) (Nat.le_totient_mul_log_add_one).