Shor's algorithm: order, the modular-multiplication gate and the order-finding circuit
DefinitionShorWiki_Defsquantum-computingshor-algorithm
This file fixes the objects in terms of which all statements of the mission are phrased.
- Order. For integers and , is the multiplicative order of modulo : when it is the smallest positive integer with .
- Register size. is the smallest integer with ; the second register has qubits and the first register has qubits. Write .
- Basis states and tensor products. A state of a -dimensional register is a vector in ; is the -th standard basis vector, and is the product state on the two-register space .
- The modular-multiplication gate. The -qubit gate acts on basis states by
- Roots of unity and the states . , and with ,
- Gates of the phase estimation circuit. is the -fold tensor power of the Hadamard gate, with the number of bit positions where both and have a . is the quantum Fourier transform, , and is its conjugate transpose. The controlled gate applies to the second register exactly when bit of the first register is .
- Phase estimation circuit. With an -qubit first register, the circuit is: on the first register, then , then on the first register.
- Order-finding output. The output state of Shor's order-finding subroutine is the phase estimation circuit with applied to .
- Measurement. For a two-register state , the probability of outcome when the first register is measured in the computational basis is .
These are the objects of the article's description of the order-finding subroutine; every theorem of the mission is stated in terms of them.
Formalization Note States are plain coordinate vectors Fin d → ℂ (not normalized by fiat) and gates are matrices acting by Matrix.mulVec; the order is orderOf (a : ZMod N), and is Nat.clog 2 N. The controlled-power stage is the product of the individual controlled gates, exactly as drawn in the article's circuit diagram, rather than a single block-diagonal operator.
Definition code
import Mathlib
open Matrix Kronecker
namespace ShorWiki
/-- The multiplicative order of `a` modulo `N`: the smallest positive integer `r` with
`a ^ r ≡ 1 (mod N)` (when `a` is coprime to `N`). -/
noncomputable def ord (N a : ℕ) : ℕ := orderOf (a : ZMod N)
/-- The number of qubits `n` of the second register: the smallest `n` with `N ≤ 2 ^ n`,
i.e. `n = ⌈log₂ N⌉`. -/
def regBits (N : ℕ) : ℕ := Nat.clog 2 N
/-- The computational basis state `|m⟩` in a register of dimension `d`
(the zero vector if `m ≥ d`). -/
def ket (d m : ℕ) : Fin d → ℂ := fun i => if (i : ℕ) = m then 1 else 0
/-- The tensor product `|u⟩ ⊗ |v⟩` of two register states. -/
def tensorState {d₁ d₂ : ℕ} (u : Fin d₁ → ℂ) (v : Fin d₂ → ℂ) : Fin d₁ × Fin d₂ → ℂ :=
fun p => u p.1 * v p.2
/-- The `n`-qubit gate `U` of multiplication by `a` modulo `N`:
`U|k⟩ = |a k mod N⟩` for `0 ≤ k < N` and `U|k⟩ = |k⟩` for `N ≤ k < 2 ^ n`.
The entry `U i k` is the amplitude `⟨i|U|k⟩`. -/
noncomputable def mulModGate (N a n : ℕ) : Matrix (Fin (2 ^ n)) (Fin (2 ^ n)) ℂ :=
fun i k =>
if (k : ℕ) < N then (if (i : ℕ) = a * k % N then 1 else 0)
else (if i = k then 1 else 0)
/-- The primitive `r`-th root of unity `ω_r = e^{2πi/r}`. -/
noncomputable def omega (r : ℕ) : ℂ := Complex.exp (2 * Real.pi * Complex.I / r)
/-- The state `|ψ_j⟩ = r^{-1/2} ∑_{k=0}^{r-1} ω_r^{-kj} |a^k mod N⟩` of the `n`-qubit
second register, where `r = ord N a`. -/
noncomputable def eigState (N a n j : ℕ) : Fin (2 ^ n) → ℂ :=
fun i => (1 / Real.sqrt (ord N a) : ℂ) *
∑ k ∈ Finset.range (ord N a),
(omega (ord N a) ^ (k * j))⁻¹ * (if (i : ℕ) = a ^ k % N then 1 else 0)
/-- The `m`-fold tensor power `H^{⊗m}` of the Hadamard gate:
`⟨x|H^{⊗m}|y⟩ = 2^{-m/2} (-1)^{x·y}`, where `x·y` is the number of bit positions
`b < m` at which both `x` and `y` have a `1`. -/
noncomputable def hadamardTensor (m : ℕ) : Matrix (Fin (2 ^ m)) (Fin (2 ^ m)) ℂ :=
fun x y => (1 / Real.sqrt (2 ^ m) : ℂ) *
(-1) ^ ((Finset.range m).filter (fun b => (x : ℕ).testBit b ∧ (y : ℕ).testBit b)).card
/-- The quantum Fourier transform on `m` qubits:
`⟨y|QFT_{2^m}|x⟩ = 2^{-m/2} e^{2πi x y / 2^m}`. -/
noncomputable def qft (m : ℕ) : Matrix (Fin (2 ^ m)) (Fin (2 ^ m)) ℂ :=
fun y x => (1 / Real.sqrt (2 ^ m) : ℂ) *
Complex.exp (2 * Real.pi * Complex.I * (x : ℕ) * (y : ℕ) / 2 ^ m)
/-- The inverse quantum Fourier transform `QFT_{2^m}^{-1} = QFT_{2^m}^†`. -/
noncomputable def qftInv (m : ℕ) : Matrix (Fin (2 ^ m)) (Fin (2 ^ m)) ℂ := (qft m)ᴴ
/-- The controlled gate `C-U^{2^j}`: qubit `j` of the first (`m`-qubit) register controls
the application of `U^{2^j}` to the second (`n`-qubit) register, where `U = mulModGate N a n`.
On `|x⟩|k⟩` it applies `U^{2^j}` to `|k⟩` if bit `j` of `x` is `1`, and does nothing otherwise. -/
noncomputable def controlledPowGate (N a n m j : ℕ) :
Matrix (Fin (2 ^ m) × Fin (2 ^ n)) (Fin (2 ^ m) × Fin (2 ^ n)) ℂ :=
fun p q =>
if p.1 = q.1 then
(if (q.1 : ℕ).testBit j then (mulModGate N a n ^ (2 ^ j)) p.2 q.2
else (1 : Matrix (Fin (2 ^ n)) (Fin (2 ^ n)) ℂ) p.2 q.2)
else 0
/-- The modular-exponentiation stage of the circuit: the product of the controlled gates
`C-U^{2^0}, C-U^{2^1}, …, C-U^{2^{m-1}}`. -/
noncomputable def controlledPowers (N a n m : ℕ) :
Matrix (Fin (2 ^ m) × Fin (2 ^ n)) (Fin (2 ^ m) × Fin (2 ^ n)) ℂ :=
((List.range m).map (fun j => controlledPowGate N a n m j)).prod
/-- The quantum phase estimation circuit for `U = mulModGate N a n` with an `m`-qubit first
register: Hadamard gates on the first register, then the controlled powers of `U`, then the
inverse quantum Fourier transform on the first register. -/
noncomputable def phaseEstimationCircuit (N a n m : ℕ) :
Matrix (Fin (2 ^ m) × Fin (2 ^ n)) (Fin (2 ^ m) × Fin (2 ^ n)) ℂ :=
(qftInv m ⊗ₖ (1 : Matrix (Fin (2 ^ n)) (Fin (2 ^ n)) ℂ)) * controlledPowers N a n m *
(hadamardTensor m ⊗ₖ (1 : Matrix (Fin (2 ^ n)) (Fin (2 ^ n)) ℂ))
/-- The output state of Shor's order-finding circuit: phase estimation with a `2n`-qubit
first register, `n = regBits N`, applied to the input `|0⟩^{⊗2n} ⊗ |1⟩`. -/
noncomputable def orderFindingOutput (N a : ℕ) :
Fin (2 ^ (2 * regBits N)) × Fin (2 ^ regBits N) → ℂ :=
(phaseEstimationCircuit N a (regBits N) (2 * regBits N)) *ᵥ
tensorState (ket (2 ^ (2 * regBits N)) 0) (ket (2 ^ regBits N) 1)
/-- The probability of obtaining outcome `k` when measuring the first register of a
two-register state `Ψ` in the computational basis: `∑_i |⟨k, i|Ψ⟩|²`. -/
noncomputable def measureFirst {d₁ d₂ : ℕ} (Ψ : Fin d₁ × Fin d₂ → ℂ) (k : Fin d₁) : ℝ :=
∑ i : Fin d₂, ‖Ψ (k, i)‖ ^ 2
end ShorWiki
Source
Wikipedia, "Shor's algorithm", revision oldid=1378862216, https://en.wikipedia.org/w/index.php?title=Shor%27s_algorithm&oldid=1378862216, sections "Classical reduction", "Quantum order-finding subroutine" and "Quantum phase estimation" (PDF pp. 2–5)
Human review
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.