Shor's Algorithm: Order Finding and Factoring
Statement
For an odd composite integer N that is not a prime power, factoring is polynomial-time reducible to order-finding: given a random base a coprime to N, the multiplicative order r (least r > 0 with ar ≡ 1 (mod N)) is extracted by quantum phase estimation applied to the modular-multiplication unitary Ua |y〉 = |ay mod N〉, whose eigenphases are the fractions s/r; a classical continued-fraction step then recovers r, and with constant probability gcd(ar/2 ± 1, N) is a nontrivial factor.
Why it matters
Shor's algorithm is the flagship demonstration that a quantum computer can solve a problem believed classically intractable: the best known classical factoring algorithm (the general number field sieve) runs in sub-exponential but super-polynomial time exp(O( (log N)1/3(log log N)2/3)), whereas the quantum routine runs in O((log N)2 log log N log log log N) gates. The security of RSA rests entirely on the assumed hardness of this problem, so the algorithm is the reason post-quantum cryptography exists as a field.
Conceptually it is the archetype of the hidden-subgroup paradigm: order-finding is the abelian hidden-subgroup problem for the map k ↦ ak mod N, and the Fourier transform over ℤ is exactly the tool that exposes the hidden period. The same skeleton solves discrete logarithm, Pell's equation, and principal-ideal problems.
Assumptions
Derivation
Reading. Phase estimation on the modular-multiplication unitary reads off a random multiple s/r of the reciprocal period; continued fractions strip the random numerator s to expose r; and a single even, non-trivial order hands two proper factors of N to Euclid's algorithm. The favourable-a condition (r even and ar/2 ≢ −1) holds for at least half of the admissible bases, so a handful of random a suffices.
Units check. Every quantity is a dimensionless integer or a pure phase. Eigenphases s/r ∈ [0,1) are dimensionless fractions of a full turn; the measured x/2t is a bit-fraction, also dimensionless; and the output gcd(·, N) is an integer in {2, …, N−1}. The gate count O(n2 log log n) with n = ⌈log2N⌉ is a pure number, confirming polynomial scaling in the input length.
Limiting cases
- r = 1: means a ≡ 1, an uninformative base; discard and redraw a.
- r odd: ar/2 is not an integer, step 2 fails; redraw a (happens for at most half of bases).
- ar/2 ≡ −1 (mod N): then ar/2+1 ≡ 0, both gcds are trivial (1 or N); redraw a.
- N prime: no non-trivial factor exists; the algorithm is never run on primes (Miller–Rabin screens them out first).
- r ≈ N (large period): the routine is unaffected — t = 2n+1 qubits already resolve any r < N; this is the generic and hardest classical case where the quantum speedup is largest.
Breaks when
- Under-resolved control register (t < 2n+1). The estimate x/2t is no longer within 1/2r2 of s/r, so s/r need not appear among the continued-fraction convergents and r cannot be read out reliably.
- Persistently unlucky bases. If every sampled a gives odd r or ar/2 ≡ −1 (impossible in probability but adversarially constructible for special N such as prime powers, which are why they are excluded), no factor emerges. For genuine composites the failure probability per base is ≤ ½, so this is exponentially unlikely to persist.
- Decoherence over the O(n3) modular-exponentiation circuit. The controlled-Ua2j stage is by far the deepest part; if the coherence time is shorter than this depth, phase errors accumulate, the eigenphase kickback washes out, and the measured distribution flattens toward uniform noise — no period signal survives. This, not the algorithm's logic, is the present engineering barrier.
Failure modes
- Confusing the two registers. The t-qubit control register carries the phase; the n-qubit target register holds |ak mod N〉. The inverse QFT acts on the control register only.
- Reading r directly from the measurement. The raw outcome is x ≈ 2ts/r, not r; students who skip the continued-fraction step get a meaningless integer.
- Taking the convergent denominator as r when gcd(s, r) > 1. It is then r/gcd(s,r), a proper divisor; the fix is to test whether the candidate is a true order and, if not, combine denominators from two runs via lcm.
- Forgetting to verify ar ≡ 1. The recovered r is a guess; always confirm classically before using it, and take the least valid multiple.
- Assuming ar/2 ≡ −1 is impossible. It is a genuine failure branch (e.g. it can dominate for prime-power N), not a corner case to ignore.
- Using QFT instead of QFT†. Phase estimation ends with the inverse transform; the forward QFT sends the sharp phase register to the wrong basis.
Discussion
The heart of the algorithm is a change of viewpoint enabled by step 6: although the period r is unknown, the state |1〉 is automatically an equal superposition of all r eigenvectors of Ua. Phase estimation therefore samples the eigenphase spectrum {s/r} without ever preparing a single eigenstate. The quantum Fourier transform inside phase estimation is the abelian group Fourier transform that diagonalises the shift |ak〉 ↦ |ak+1〉; the “interference of amplitudes” that concentrates measurement probability onto x ≈ 2ts/r is precisely the constructive/destructive interference of the waves thread, and the uniform sampling of s is the chance thread supplying the randomness that continued fractions later tame.
The symmetry thread runs deeper: order-finding is the hidden-subgroup problem for the homomorphism f : ℤ → ℤN×, k ↦ ak, whose kernel is the subgroup rℤ. Fourier analysis over the domain group ℤ (approximated by ℤ2t) exposes the annihilator of that hidden subgroup — the multiples of 1/r. Every efficient quantum algorithm in the abelian hidden-subgroup family — Simon's problem, discrete log, the Deutsch–Jozsa promise — is this same representation-theoretic mechanism in a different group.
The division of labour between the quantum and classical parts is instructive and often misread. The quantum device does not compute r; it produces a single noisy sample s/r with s uniformly random. All the number theory — continued fractions to invert the sampling, the totient bound to guarantee a coprime numerator, gcd to convert order into factor — is classical. What is quantum is exclusively the ability to evaluate f in superposition and Fourier-transform the resulting periodic structure in O(n2) operations, where any classical Fourier read-out of the period would need exponentially many evaluations of f. The speedup is entirely in the period-extraction subroutine, not in arithmetic.
Common misconceptions. Shor's algorithm does not “try all factors in parallel” — no branch of the superposition ever holds a factor of N; it holds a value of ak mod N, and factoring emerges only after the classical gcd. Nor does it break all cryptography: it targets problems with hidden abelian periodicity (RSA, Diffie–Hellman, elliptic-curve discrete log), while lattice-based schemes have no known such structure and are the basis of post-quantum standards.
Worked examples
Reading. A single even order r = 4 with 72 ≡ 4 factors 15 completely. Units check. All integers: 3, 5 ∈ {2, …, 14} and 3 × 5 = 15 = N.
Reading. An 11-qubit control register resolves the period-6 signal, continued fractions strip the numerator, and the even order factors 21. Units check. Integers throughout: 3, 7 ∈ {2, …, 20}, 3 × 7 = 21 = N; the register widths n = 5, t = 11 are pure counts.
Problems
- For N = 15 and a = 2, find the order r by hand and determine whether it yields a factor.
Solution
21=2, 22=4, 23=8, 24=16≡1 (mod 15), so r = 4. It is even and ar/2 = 22 = 4 ≢ −1≡14. Then gcd(4−1,15)=gcd(3,15)=3 and gcd(4+1,15)=gcd(5,15)=5. Factors 3 and 5 — success.
- Choose a = 14 for N = 15. Show it is an unfavourable base and explain what the algorithm does next.
Solution
14 ≡ −1 (mod 15), so 142 ≡ 1 and r = 2. Then ar/2 = 141 = 14 ≡ −1 (mod 15), which is exactly the failure branch: gcd(14+1,15)=gcd(15,15)=15 (trivial) and gcd(14−1,15)=gcd(13,15)=1 (trivial). No factor. The algorithm discards this base and redraws a new random a; since at least half of admissible bases are favourable, few retries are expected.
- A phase-estimation run on N = 21 (so 2t = 2048) returns x = 1365. Recover the candidate order via continued fractions and check it.
Solution
1365/2048 = 0.66650…, close to 2/3 = 0.66667. Continued fraction: 2048 = 1·1365 + 683 (quotient 1), 1365 = 1·683 + 682 (quotient 1), giving convergents 0/1, 1/1, 1/2, 2/3, …. The first convergent with denominator < 21 matching to tolerance is 2/3, denominator 3. But gcd(2,3)=1 gives candidate r = 3, and 23 = 8 ≢ 1 (mod 21), so 3 is only a divisor of the true order (here s = 2, r = 6, so the run reported s/r = 2/6 = 1/3 — reduced). Combine with another run (e.g. one giving denominator 6 or 2) via lcm(3, 2) = 6 to obtain the true r = 6.
- Show that for any odd composite N that is not a prime power, a base a chosen uniformly from ℤN× is favourable (r even and ar/2 ≢ −1) with probability at least ½. (State the theorem; sketch why the prime-power exclusion matters.)
Solution
By the Chinese remainder theorem, ℤN× ≅ ∏i ℤpiei× across the distinct prime-power factors. The theorem (Nielsen & Chuang, Thm 5.3) states that for N with at least two distinct odd prime factors, a uniformly random a coprime to N is unfavourable with probability ≤ 1/2m−1 ≤ ½, where m ≥ 2 is the number of distinct prime factors. The proof pairs each unfavourable residue with a favourable partner across the CRT factors. If N = pe were a prime power there would be a single factor (m = 1) and the bound would not hold — indeed the order structure can force ar/2 ≡ −1 systematically — which is exactly why prime powers are stripped off by a classical N1/k test before the quantum routine runs.
- Estimate the number of control qubits t and the leading gate cost for factoring a 2048-bit RSA modulus, and comment on why this is not yet feasible.
Solution
Here n = 2048, so t = 2n + 1 = 4097 control qubits (logical), plus n = 2048 target qubits, i.e. of order 6000 logical qubits before ancillas for modular arithmetic (realistic circuits use ≈ 2n to 5n total). The dominant cost is modular exponentiation: naively O(n3) ≈ 8.6×109 gates, or O(n2 log n log log n) with fast multiplication. Infeasibility is not the logic but the error rate: a fault-tolerant run needs on the order of 1010 coherent operations, demanding physical error rates below ∼10−3 and, after surface-code overhead of 103–104 physical qubits per logical qubit, on the order of 106–107 physical qubits — far beyond present hardware of a few hundred noisy qubits.