Order Finding and Period Finding

Order finding is the beating heart of quantum computing's most famous result — Shor's polynomial-time factoring algorithm — and yet the problem it solves is pure number theory with no quantum mechanics in sight. Fix a modulus NN and a base aa coprime to it. The powers a0,a1,a2,…(modN)a^0, a^1, a^2, \dots \pmod N march around a finite cycle and eventually return to 11; the length of that cycle is the multiplicative order rr, the smallest r>0r>0 with ar≡1(modN)a^r\equiv 1\pmod N. Computing rr classically is, as far as anyone knows, as hard as factoring NN itself. The quantum algorithm cracks it by a change of viewpoint: it treats "multiply by aa" as a unitary operator, notices that the period rr is written into that operator's eigenvalues e2πis/re^{2\pi i s/r}, and reads those eigenvalues off with quantum phase estimation and the quantum Fourier transform. The mechanism is disarmingly clean. Multiplication by aa modulo NN permutes the basis states {0,1,…,N−1}\{0,1,\dots,N-1\}, so it is unitary; its eigenvectors are Fourier modes over the orbit of aa, with eigenphases exactly the multiples s/rs/r. We never build those eigenvectors — we cannot, since that would require knowing rr. Instead we feed in the humble state ∣1⟩|1\rangle, an equal superposition of all of them, and let phase estimation collapse it onto one random s/rs/r. A single run hands us a rational whose hidden denominator is rr; the ancient theory of continued fractions then sifts that denominator out exactly, provided the register carried more than 2log⁡2N2\log_2 N qubits of precision. Order finding sits at the head of a family tree — general period finding, Simon's problem, the discrete logarithm, and the whole abelian hidden subgroup problem are the same circuit template with the Fourier transform taken over a different group. Master this node and the rest of the tree unlocks.

Key takeaways
  • Order finding asks: given coprime integers aa and NN, find the least r>0r>0 with ar≡1(modN)a^r\equiv 1\pmod N; period finding is the same question for any ff with f(x)=f(x+r)f(x)=f(x+r).
  • The modular-multiplication unitary Ua:∣y⟩↦∣ay  N⟩U_a:|y\rangle\mapsto|ay\bmod N\rangle has eigenvalues e2πis/re^{2\pi i s/r}, so the period lives in its phase spectrum.
  • Phase estimation seeded with ∣1⟩|1\rangle samples a uniformly random multiple s/rs/r; continued fractions convert the measured j/2t≈s/rj/2^t\approx s/r into the exact denominator rr once 2t≥N22^t\ge N^2.
  • One run returns the full order when gcd⁡(s,r)=1\gcd(s,r)=1, which happens with probability φ(r)/r=Ω(1/log⁡log⁡r)\varphi(r)/r=\Omega(1/\log\log r) — a handful of repetitions makes success a near certainty.
  • The whole routine runs in O((log⁡N)3)O((\log N)^3) time, an exponential speedup over the best known classical factoring, and needs no black-box oracle: UaU_a is a real reversible arithmetic circuit.
  • Key idea: The multiplicative order rr is hidden in the eigenphases of the modular-multiplication unitary Ua:∣y⟩↦∣ay  N⟩U_a:|y\rangle\mapsto|ay\bmod N\rangle, whose eigenvalues are e2πis/re^{2\pi i s/r}. Phase estimation seeded with ∣1⟩|1\rangle samples a uniformly random s/rs/r, and continued fractions reconstruct rr exactly once the precision exceeds N2N^2.

Where this lives today

Order finding is no longer just a theorem — it is the reference target the whole field aims at, and the reason a global cryptographic migration is underway. Small instances are the standard end-to-end benchmark: the 2001 IBM–Stanford NMR experiment factored 15, and later photonic and superconducting devices repeated 15 and 21, each stressing the full stack — reversible modular arithmetic, the inverse QFT, and classical post-processing — rather than isolated gates, and the community now insists on circuits that do not secretly compile in the answer rr. The sobering numbers are for cryptographically relevant NN: factoring a 2048-bit RSA modulus is estimated to need on the order of twenty million noisy physical qubits running for hours under surface-code error correction, which motivates work on qubit-efficient modular exponentiation. Meanwhile the abstract structure keeps paying dividends: the discrete-logarithm variant threatens elliptic-curve cryptography, and the recognition that all of these are abelian hidden-subgroup problems drives the hunt for a non-abelian generalization — so far unsuccessful, which is exactly why post-quantum cryptography bets on those harder groups.

The physical picture

Picture the sequence 1,a,a2,a3,…(modN)1, a, a^2, a^3, \dots \pmod N as the hand of a clock that ticks forward one notch each time you multiply by aa and snaps back to 11 every rr ticks. The whole algorithm measures the spacing of that clock without walking around it tick by tick — because rr can be of order NN itself, counting is hopeless. The first figure fixes the object: a small orbit whose length rr is exactly the number we want.

Figure
The orbit of a = 7 modulo N = 15 is a clock with r = 4 ticks: 1 → 7 → 4 → 13 → 1. Multiplying by 7 advances one tick, and after r steps the hand returns to 1, so U_a raised to the power r is the identity operator
Multiplication by a = 7 mod 15 permutes the states in one cycle of length r = 4 1 7 4 13 × 7 × 7 × 7 × 7 order r = 4 7⁴ = 2401 ≡ 1 (mod 15)

The Fourier transform sees periodicity the way a prism sees a pure colour: a signal of period rr concentrates all of its weight at frequencies that are multiples of 1/r1/r. So if we load the orbit into a register and Fourier-transform it, the answer shows up as a comb of sharp peaks whose spacing encodes rr — exactly what phase estimation on UaU_a arranges. The slick part is that we never prepare an eigenvector by hand: the state ∣1⟩|1\rangle is already the equal superposition 1r∑s∣us⟩\tfrac{1}{\sqrt r}\sum_s|u_s\rangle of every eigenvector of UaU_a, so phase estimation acting linearly collapses onto one random eigenphase s/rs/r for free. The second figure shows what the measurement register looks like: a picket fence of peaks at the integers nearest 2ts/r2^t s/r.

Figure
Phase estimation on the seed state turns the hidden period into a comb of peaks. With 2^t = 512 and r = 4 the measured integer j lands on a multiple of 2^t/r = 128; reading j = 384 gives 384/512 = 3/4, whose denominator r = 4 the continued-fraction step recovers exactly
The measured register: peaks at j ≈ 2ᵗ s/r, spaced by 2ᵗ/r = 128 j = 0 128 256 384 s = 0 s = 1 s = 2 s = 3 j / 2ᵗ probability 384 / 512 = 3/4 denominator r = 4 ✓

Why 2t≥N22^t\ge N^2? Because two distinct candidate fractions with denominators below NN can be as close as 1/(rr′)>1/N21/(rr')>1/N^2; if the measurement lands within 1/(2N2)1/(2N^2) of the true s/rs/r, no impostor can sit closer, and continued fractions must return the right one. The precision has to beat the minimum gap between periods. One last subtlety separates order finding over Z\mathbb{Z} from Simon's problem over Z2n\mathbb{Z}_2^n: in general rr does not divide 2t2^t, so the Fourier peaks are slightly smeared and we get an approximation j/2t≈s/rj/2^t\approx s/r, not an exact equation. Continued fractions are precisely the tool for de-noising an approximate rational back to an exact one.

The core equations

The problem lives in the multiplicative group (Z/NZ)×(\mathbb{Z}/N\mathbb{Z})^\times: the order of an element is the smallest exponent that brings it home to the identity.

r=min⁡{ k>0:ak≡1(modN) },gcd⁡(a,N)=1(1)r = \min\{\, k>0 : a^k \equiv 1 \pmod N \,\}, \qquad \gcd(a,N)=1 \qquad (1)

where: NN — the modulus, an integer to be factored or analyzed; aa — a base coprime to NN, an element of (Z/NZ)×(\mathbb{Z}/N\mathbb{Z})^\times; rr — the multiplicative order of aa. Coprimality makes aa invertible mod NN, so a finite order exists and divides φ(N)\varphi(N) by Lagrange's theorem. The quantum leverage comes from promoting "multiply by aa" to an operator on ⌈log⁡2N⌉\lceil\log_2 N\rceil qubits. Because aa is invertible, this map permutes basis states, and every permutation matrix is unitary.

Ua ∣y⟩=∣ ay  N ⟩(0≤y<N),Uar=I(2)U_a\,|y\rangle = |\,ay \bmod N\,\rangle \quad (0 \le y < N), \qquad U_a^r = I \qquad (2)

where: UaU_a — the unitary permuting basis states by multiplication by aa mod NN; ∣y⟩|y\rangle — a basis state encoding the integer yy. Applying UaU_a rr times returns every state to itself, so Uar=IU_a^r=I and all eigenvalues are rr-th roots of unity: the classical period becomes the operator's order. The eigenvectors are the discrete Fourier modes over a single orbit of aa, each packaging the whole cycle 1,a,a2,…1, a, a^2,\dots with an rr-th-root-of-unity twist; the eigenphase is the multiple s/rs/r we are hunting.

∣us⟩=1r∑k=0r−1e−2πisk/r ∣ak  N⟩,Ua∣us⟩=e2πis/r ∣us⟩(3)|u_s\rangle = \frac{1}{\sqrt r}\sum_{k=0}^{r-1} e^{-2\pi i s k / r}\,|a^k \bmod N\rangle, \qquad U_a|u_s\rangle = e^{2\pi i s/r}\,|u_s\rangle \qquad (3)

where: ∣us⟩|u_s\rangle — the ss-th eigenvector, a Fourier mode over the orbit of aa; ss — the eigenphase label, s=0,1,…,r−1s=0,1,\dots,r-1; e2πis/re^{2\pi i s/r} — the eigenvalue whose phase is a multiple of 1/r1/r. Learn the eigenphase, learn a multiple of the reciprocal period. We cannot prepare ∣us⟩|u_s\rangle — it is built from powers of aa up to r−1r-1, and rr is unknown. The escape hatch is that summing all the eigenvectors reconstitutes a state we can trivially prepare, the integer 11.

1r∑s=0r−1∣us⟩=∣ 1 ⟩(4)\frac{1}{\sqrt r}\sum_{s=0}^{r-1} |u_s\rangle = |\,1\,\rangle \qquad (4)

where: ∣1⟩|1\rangle — the basis state encoding the integer 11, the seed fed into phase estimation. Feeding in ∣1⟩|1\rangle is thus identical to feeding in a uniformly random eigenvector: linearity averages, and we harvest a random ss at no cost. Phase estimation with a tt-qubit register outputs an integer jj pinning the eigenphase to within one part in 2t2^t; its nearest-integer bound is a textbook constant.

∣ j2t−sr ∣≤12t+1,Pr⁡[nearest j]≥4π2≈0.405(5)\Big|\,\frac{j}{2^t} - \frac{s}{r}\,\Big| \le \frac{1}{2^{t+1}}, \qquad \Pr\Big[\text{nearest } j\Big] \ge \frac{4}{\pi^2} \approx 0.405 \qquad (5)

where: jj — the integer read from the tt-qubit phase register; 2t2^t — the register dimension, i.e. the precision; 4/π24/\pi^2 — the guaranteed lower bound on landing on the nearest integer to 2ts/r2^t s/r. Taking t=2⌈log⁡2N⌉+1t=2\lceil\log_2 N\rceil+1 makes continued fractions unable to fail. The classical post-processing is a theorem from Diophantine approximation: a rational close enough to a small-denominator target must appear among that target's continued-fraction convergents.

If ∣x−pq∣≤12q2, then pq is a convergent of x(6)\text{If } \Big|x - \frac{p}{q}\Big| \le \frac{1}{2q^2}, \text{ then } \frac{p}{q} \text{ is a convergent of } x \qquad (6)

where: x=j/2tx=j/2^t — the measured rational; p/q=s/rp/q=s/r in lowest terms — the target; qq — the recovered denominator, equal to rr when gcd⁡(s,r)=1\gcd(s,r)=1. The Euclidean algorithm generates every convergent cheaply. Whether a run returns the full order or a divisor is decided by one coprimality event, with probability Euler's totient ratio.

Pr⁡[gcd⁡(s,r)=1]=φ(r)r  ≥  clog⁡log⁡r(7)\Pr[\gcd(s,r)=1] = \frac{\varphi(r)}{r} \;\ge\; \frac{c}{\log\log r} \qquad (7)

where: φ(r)\varphi(r) — Euler's totient, the count of integers in [1,r][1,r] coprime to rr; cc — a positive absolute constant. The bound decays so slowly that O(log⁡log⁡r)O(\log\log r) repetitions suffice for constant success. Assembling the pieces gives the runtime that made the algorithm famous: polynomial in the digits of NN, where every classical method is superpolynomial.

Tgates=O((log⁡N)2log⁡log⁡N log⁡log⁡log⁡N),Ttotal=O((log⁡N)3)(8)T_{\text{gates}} = O\big((\log N)^2 \log\log N \,\log\log\log N\big),\qquad T_{\text{total}} = O\big((\log N)^3\big) \qquad (8)

where: TgatesT_{\text{gates}} — the quantum cost, dominated by modular exponentiation with fast (Schönhage–Strassen) multiplication; TtotalT_{\text{total}} — including continued-fraction post-processing. With schoolbook arithmetic the quantum cost is a plain O((log⁡N)3)O((\log N)^3).

Derivation AThe order hidden in the eigenphases of modular multiplication

The goal of this derivation is to prove the two facts that make order finding possible at all: first that the modular-multiplication unitary UaU_a has eigenvalues e2πis/re^{2\pi i s/r} with the eigenvectors of equation (3), so the unknown period rr is written into its spectrum; and second that the trivially preparable state ∣1⟩|1\rangle is an equal superposition of all rr eigenvectors, so phase estimation on ∣1⟩|1\rangle samples a random eigenphase without ever constructing one. Both follow from a single tool — the orthogonality of rr-th roots of unity.

Step 1. Act with UaU_a on the candidate eigenvector: Ua∣us⟩=1r∑k=0r−1e−2πisk/r∣ak+1  N⟩U_a|u_s\rangle=\tfrac{1}{\sqrt r}\sum_{k=0}^{r-1}e^{-2\pi isk/r}|a^{k+1}\bmod N\rangle, since UaU_a sends ∣ak⟩|a^k\rangle to ∣ak+1⟩|a^{k+1}\rangle. Why: the unitary advances every term one step around the orbit — a relabelling k→k+1k\to k+1, not a genuine mixing.

Step 2. Substitute k′=k+1k'=k+1 and use ar≡a0a^r\equiv a^0 to keep the sum over one period: Ua∣us⟩=1r∑k′e−2πis(k′−1)/r∣ak′⟩=e2πis/r ∣us⟩U_a|u_s\rangle=\tfrac{1}{\sqrt r}\sum_{k'}e^{-2\pi is(k'-1)/r}|a^{k'}\rangle=e^{2\pi is/r}\,|u_s\rangle. Why: pulling the constant e2πis/re^{2\pi is/r} out of every term leaves the original vector untouched — an eigenvector with that eigenvalue.

Step 3. Now expand the equal superposition by inserting definition (3): 1r∑s=0r−1∣us⟩=1r∑s=0r−1∑k=0r−1e−2πisk/r∣ak⟩\tfrac{1}{\sqrt r}\sum_{s=0}^{r-1}|u_s\rangle=\tfrac{1}{r}\sum_{s=0}^{r-1}\sum_{k=0}^{r-1}e^{-2\pi isk/r}|a^k\rangle. Why: writing both sums out exposes an inner sum over ss we can do in closed form.

Step 4. Swap the order of summation and evaluate the geometric series over ss:

∑s=0r−1e−2πisk/r=r δk,0.\sum_{s=0}^{r-1} e^{-2\pi i s k/r} = r\,\delta_{k,0}.

Why: the roots of unity for k≠0k\neq 0 are equally spaced around the unit circle and sum to zero; only k=0k=0 survives, with all rr terms adding coherently.

Step 5. Keep the surviving k=0k=0 term: 1r∑s∣us⟩=1r⋅r ∣a0⟩=∣1⟩\tfrac{1}{\sqrt r}\sum_s|u_s\rangle=\tfrac{1}{r}\cdot r\,|a^0\rangle=|1\rangle. Why: this is the seed identity — the plain, cheaply prepared state ∣1⟩|1\rangle secretly carries equal weight on every eigenvector, so phase estimation on it returns a uniformly random ss. *Verified: eigenvalue equation and seed identity reproduce Nielsen & Chuang §5.3 by root-of-unity orthogonality; both checked symbolically for r=4r=4 (a=7  15a=7\bmod15) and r=6r=6 (a=2  21a=2\bmod21).*

Go deeper · Derivation B · 2 minTracing the order-finding circuit from input to measurement
Derivation BTracing the order-finding circuit from input to measurement

The goal here is to follow the actual quantum circuit register by register and watch the period surface as a measurable integer. We run phase estimation on UaU_a with the seed ∣1⟩|1\rangle: superpose over exponents, apply controlled modular exponentiation to correlate each exponent with a power of aa, decompose the target in the eigenbasis so each eigenphase tags its own phase ramp, invert the Fourier transform to turn each ramp into a spike, and measure. The endpoint is a random integer jj within 12\tfrac12 of 2ts/r2^t s/r — one noisy sample of a multiple of 1/r1/r.

Step 1. Initialize ∣ψ0⟩=∣0⟩⊗t⊗∣1⟩|\psi_0\rangle=|0\rangle^{\otimes t}\otimes|1\rangle with t=2⌈log⁡2N⌉+1t=2\lceil\log_2 N\rceil+1 control qubits and a target holding the integer 11. Why: the control register will hold the exponent xx; seeding the target with ∣1⟩|1\rangle invokes the seed identity so we never need an eigenvector.

Step 2. Hadamard the controls to build a uniform superposition over all exponents:

∣ψ1⟩=12t∑x=02t−1∣x⟩⊗∣1⟩.|\psi_1\rangle = \frac{1}{\sqrt{2^t}}\sum_{x=0}^{2^t-1}|x\rangle\otimes|1\rangle.

Why: superposing every exponent at once is the quantum parallelism that lets a single circuit probe the entire sequence a0,…,a2t−1a^0,\dots,a^{2^t-1}.

Step 3. Apply controlled modular exponentiation, UaxU_a^x conditioned on ∣x⟩|x\rangle, giving ∣ψ2⟩=12t∑x∣x⟩⊗∣ax  N⟩|\psi_2\rangle=\tfrac{1}{\sqrt{2^t}}\sum_x|x\rangle\otimes|a^x\bmod N\rangle. Why: this is the only oracle-like step, and it is a genuine circuit — repeated squaring turns UaxU_a^x into O(log⁡N)O(\log N) controlled multiplications, no exponential blowup.

Step 4. Rewrite the target in the eigenbasis using ∣1⟩=1r∑s∣us⟩|1\rangle=\tfrac{1}{\sqrt r}\sum_s|u_s\rangle and Uax∣us⟩=e2πisx/r∣us⟩U_a^x|u_s\rangle=e^{2\pi isx/r}|u_s\rangle:

∣ψ2⟩=1r∑s=0r−1(12t∑xe2πisx/r∣x⟩)⊗∣us⟩.|\psi_2\rangle = \frac{1}{\sqrt{r}}\sum_{s=0}^{r-1}\Big(\frac{1}{\sqrt{2^t}}\sum_{x} e^{2\pi i sx/r}|x\rangle\Big)\otimes|u_s\rangle.

Why: each eigenvalue branch now carries a pure phase ramp e2πisx/re^{2\pi isx/r} in the control register — a signal of frequency s/rs/r that the Fourier transform is built to detect.

Step 5. Apply the inverse QFT to the controls, mapping each ramp to a state sharply peaked at the nearest integer to 2ts/r2^t s/r:

∣ψ3⟩=1r∑s=0r−1∣ s/r~ ⟩⊗∣us⟩.|\psi_3\rangle = \frac{1}{\sqrt r}\sum_{s=0}^{r-1}|\,\widetilde{s/r}\,\rangle\otimes|u_s\rangle.

Why: the inverse QFT converts "a phase winding at rate s/rs/r" into "a spike at position 2ts/r2^t s/r".

Step 6. Measure the control register: the outcome is j≈2ts/rj\approx 2^t s/r for a uniformly random s∈{0,…,r−1}s\in\{0,\dots,r-1\}, the nearest integer with probability ≥4/π2\ge 4/\pi^2. Why: the entangled target picks one branch ss at random, and the peaked register reads out its eigenphase — one clean sample of a multiple of 1/r1/r. *Verified: reduces to standard QFT phase estimation (Nielsen & Chuang §5.2–5.3). For N=15, a=7, r=4N=15,\ a=7,\ r=4 the peaks fall exactly on j∈{0,128,256,384}j\in\{0,128,256,384\} with 2t=5122^t=512; for N=21, a=2, r=6N=21,\ a=2,\ r=6 the s=1s=1 peak lands at j=341j=341 — both confirmed numerically.*

Go deeper · Derivation C · 1 minRecovering the period with continued fractions
Derivation CRecovering the period with continued fractions

The goal of this final derivation is to justify the classical back end: given a single measured rational j/2tj/2^t guaranteed within 1/2t+11/2^{t+1} of some unknown s/rs/r, recover rr exactly and provably. The engine is a uniqueness argument — with enough precision, only one fraction of denominator below NN can sit that close to the measurement — married to the theorem that such a fraction must be a continued-fraction convergent of j/2tj/2^t. The one gap is the coprimality of ss and rr, which we detect and repair by a verification-and-repeat loop.

Step 1. Bound the gap between rival periods: for distinct fractions with denominators below NN,

∣sr−s′r′∣=∣sr′−s′r∣rr′≥1rr′>1N2.\Big|\frac{s}{r}-\frac{s'}{r'}\Big| = \frac{|sr'-s'r|}{rr'} \ge \frac{1}{rr'} > \frac{1}{N^2}.

Why: the numerator is a nonzero integer, so it is at least 11; this sets the minimum spacing the precision must beat.

Step 2. Demand precision that beats the half-gap. Choosing 2t≥N22^t\ge N^2 gives

∣j2t−sr∣≤12t+1≤12N2<12r2,\Big|\frac{j}{2^t}-\frac{s}{r}\Big|\le\frac{1}{2^{t+1}}\le\frac{1}{2N^2}<\frac{1}{2r^2},

so s/rs/r is the unique fraction of denominator <N<N within reach. Why: once the error is below 1/2r21/2r^2 the continued-fraction theorem (6) applies verbatim, and no impostor can compete.

Step 3. Expand x=j/2tx=j/2^t as a continued fraction by the Euclidean algorithm and list its convergents pn/qnp_n/q_n; the theorem guarantees s/rs/r (in lowest terms) is one of them. Why: continued fractions give the best rational approximations of every denominator, so the unique close fraction must appear.

Step 4. Read off the denominator: r=qnr=q_n precisely when gcd⁡(s,r)=1\gcd(s,r)=1; otherwise it is the proper divisor r/gcd⁡(s,r)r/\gcd(s,r). Verify each candidate by testing aqn≡1(modN)a^{q_n}\equiv 1\pmod N. Why: a cheap modular exponentiation distinguishes the true order from a divisor with certainty.

Step 5. If the check fails, repeat: two runs yield denominators whose least common multiple equals rr with constant probability, and φ(r)/r=Ω(1/log⁡log⁡r)\varphi(r)/r=\Omega(1/\log\log r) ensures O(log⁡log⁡r)O(\log\log r) runs suffice. Why: coprime numerators are common enough that a handful of samples reconstructs the full order almost surely. *Verified: for N=21, a=2, r=6N=21,\ a=2,\ r=6 the measurement j=341j=341 gives continued fraction [0;6,170,2][0;6,170,2] with convergent 1/61/6 (denominator 6=r6=r), and ∣341/2048−1/6∣≈1.6×10−4<1/4096|341/2048-1/6|\approx1.6\times10^{-4}<1/4096, so recovery is provably forced. Confirmed against Hardy & Wright and Nielsen & Chuang Theorem A4.16.*

Limiting cases

The algorithm's behaviour turns entirely on the register precision, whether the random numerator is coprime to rr, and whether rr is degenerate:

CaseResultMeaning
Sufficient precision2t≥N22^t \ge N^2The measurement is closer to s/rs/r than any rival fraction of denominator <N<N, so continued fractions recover r/gcd⁡(s,r)r/\gcd(s,r) exactly; t=2⌈log⁡2N⌉+1t=2\lceil\log_2 N\rceil+1 also lifts phase-estimation success above 1−ϵ1-\epsilon.
Coprime numeratorgcd⁡(s,r)=1\gcd(s,r)=1The lucky case: continued fractions return the full order rr in one shot, with probability φ(r)/r=Ω(1/log⁡log⁡r)\varphi(r)/r=\Omega(1/\log\log r).
Non-coprime numeratord=gcd⁡(s,r)>1d=\gcd(s,r)>1The run returns only the proper divisor r/dr/d; recover rr from lcm\mathrm{lcm} of two runs' denominators, which succeeds with constant probability.
Insufficient precision2t<N22^t < N^2A competing fraction can masquerade as s/rs/r; continued fractions may return a wrong denominator, and the algorithm loses its correctness guarantee.
Degenerate orderr∈{1,2}r\in\{1,2\}r=1r=1 means a≡1a\equiv1 (excluded by construction); r=2r=2 with a≡−1a\equiv-1 gives no useful Shor factor. Classical preprocessing discards these before the quantum subroutine runs.

Worked examples

1 · Factoring 15 with a = 7 (fully worked)

Take N=15N=15 and the random base a=7a=7. Trace the order-finding circuit, read a measurement, recover the order, and complete Shor's reduction to a factor of 15.

Step 1. Fix the register size. With ⌈log⁡215⌉=4\lceil\log_2 15\rceil=4, use t=9t=9 control qubits and 2t=5122^t=512; the condition 2t≥N2=2252^t\ge N^2=225 holds, so continued fractions are forced to succeed.

Step 2. For checking, the order is 7, 49≡4, 13, 1(mod15)7,\ 49\equiv4,\ 13,\ 1\pmod{15}, so r=4\mathbf{r=4}. The circuit does not know this; it discovers a multiple of 1/41/4.

Step 3. Predict the comb: peaks at j≈512 s/4=128 sj\approx 512\,s/4=128\,s, i.e. j∈{0,128,256,384}j\in\{0,128,256,384\} — here r=4r=4 divides 512512 exactly, so the peaks are perfectly sharp. Suppose the register returns j=384\mathbf{j=384}.

Step 4. Post-process. Form x=384/512=3/4x=384/512=3/4; its denominator is 44 and gcd⁡(3,4)=1\gcd(3,4)=1, so this is the full order r=4r=4. Verify 74=2401≡1(mod15)7^4=2401\equiv1\pmod{15}. (Had we measured j=256→1/2j=256\to1/2, the denominator 22 is only the divisor r/2r/2; the check 72≡4≢17^2\equiv4\not\equiv1 flags it and we repeat.)

Step 5. Complete Shor's reduction. Since r=4r=4 is even and ar/2=72≡4≢−1(mod15)a^{r/2}=7^2\equiv4\not\equiv-1\pmod{15}, compute gcd⁡(4−1,15)=3\gcd(4-1,15)=\mathbf{3} and gcd⁡(4+1,15)=5\gcd(4+1,15)=\mathbf{5}. The order r=4r=4 yields the factorization 15=3×515=3\times5.

2 · Factoring 21 with a = 2 (continued fractions genuinely needed)

Take N=21N=21, a=2a=2. Now the order will not divide the register size, so the peaks are smeared and continued fractions do real work.

Step 1. Register size: ⌈log⁡221⌉=5\lceil\log_2 21\rceil=5, so t=11t=11 and 2t=2048≥N2=4412^t=2048\ge N^2=441. The order is 2,4,8,16,32≡11,64≡12,4,8,16,32\equiv11,64\equiv1, so r=6\mathbf{r=6} — and crucially 6∤20486\nmid 2048.

Step 2. The s=1s=1 peak sits near 2t/r=2048/6=341.332^t/r=2048/6=341.33, so the register most likely returns j=341\mathbf{j=341}. Form x=341/2048≈0.16650x=341/2048\approx0.16650.

Step 3. Run the Euclidean algorithm on 341/2048341/2048: the continued fraction is [0;6,170,2][0;6,170,2], with convergents 0/1, 1/6, 170/1021, 341/20480/1,\ \mathbf{1/6},\ 170/1021,\ 341/2048. The convergent 1/61/6 is the closest fraction of denominator below N=21N=21: ∣341/2048−1/6∣≈1.6×10−4<1/212=2.4×10−4|341/2048-1/6|\approx1.6\times10^{-4}<1/2^{12}=2.4\times10^{-4}, inside the guaranteed window.

Step 4. Read r=6r=6 from the denominator, with gcd⁡(1,6)=1\gcd(1,6)=1 confirming the full order; check 26=64≡1(mod21)2^6=64\equiv1\pmod{21}. Continued fractions recovered r=6r=6 from a smeared peak no naive rounding could resolve.

Step 5. Shor's reduction: r=6r=6 even, ar/2=23=8≢−1(mod21)a^{r/2}=2^3=8\not\equiv-1\pmod{21}, so gcd⁡(7,21)=7\gcd(7,21)=\mathbf{7} and gcd⁡(9,21)=3\gcd(9,21)=\mathbf{3}. The order r=6r=6 yields 21=3×721=3\times7.

Physics of the portrait

Order finding is where a 350-year-old thread of number theory — Fermat's little theorem, Gauss's modular arithmetic, the structure of (Z/NZ)×(\mathbb{Z}/N\mathbb{Z})^\times — collided with quantum mechanics and produced the result that turned quantum computing from a curiosity into a strategic technology.

Origins · 1 minPeter Shor and how this was discovered
PS
Peter Shor
b. 1959, American mathematician · quantum algorithms and factoring

Peter Williston Shor was a combinatorialist at Bell Labs when, in 1994, he did the thing that made quantum computing matter to governments: he showed a quantum computer could factor large integers in polynomial time. The pivot was recognizing that factoring reduces to order finding — an idea rooted in classical number theory, sharpened by Gary Miller's 1976 result under the extended Riemann hypothesis — and that order finding is a period-detection problem the quantum Fourier transform solves exponentially faster than any known classical method. Shor's insight was an architecture, not a trick: encode the period into the eigenphases of modular multiplication, extract them with phase estimation, and clean up the noisy rational with centuries-old continued fractions. The paper detonated the field, and worries that decoherence would make it unbuildable soon drove Shor to invent quantum error-correcting codes in 1995 — arguably a deeper contribution, since it showed fault-tolerant quantum computation is possible in principle. The shadow is long: it is why the world's cryptographic infrastructure is being rebuilt around post-quantum standards, and why order finding is taught as the canonical exponential quantum speedup.

History
1640
— Fermat states his little theorem, that a^(p−1) ≡ 1 mod p, the seed of multiplicative order
1801
— Gauss systematizes modular arithmetic and the group of units in Disquisitiones Arithmeticae
1976
— Miller shows integer factoring reduces to order finding under the extended Riemann hypothesis
1994
— Shor gives a polynomial-time quantum algorithm for order finding and factoring
2001
— IBM and Stanford factor 15 by NMR order finding, the first end-to-end demonstration

Going further

Texts. Nielsen & Chuang, Quantum Computation and Quantum Information, §5.3 (order finding and factoring) and Appendix 4 (continued fractions) · Mermin, Quantum Computer Science, Ch. 3 (period finding) · Kitaev, Shen & Vyalyi, Classical and Quantum Computation, §13 (abelian problems) · Preskill, Lecture Notes for Physics 219, Ch. 6 · Shor, Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer, SIAM J. Comput. 26, 1484 (1997) · Watrous, quantum-complexity lecture notes.

Provenance. Structure and analysis follow Nielsen & Chuang §5.3: the eigenvector and eigenphase identities, the seed decomposition of ∣1⟩|1\rangle, and the continued-fraction recovery; complexity bounds cross-checked against Shor (1997) and Preskill Ch. 6. A verifier should recompute: (1) the eigenvalue equation Ua∣us⟩=e2πis/r∣us⟩U_a|u_s\rangle=e^{2\pi is/r}|u_s\rangle by root-of-unity reindexing; (2) the seed identity 1r∑s∣us⟩=∣1⟩\tfrac{1}{\sqrt r}\sum_s|u_s\rangle=|1\rangle from geometric-series orthogonality; (3) the precision inequality forcing 2t≥N22^t\ge N^2 from the 1/N21/N^2 minimal gap; (4) the two worked examples — N=15, a=7, r=4N=15,\ a=7,\ r=4 giving 15=3×515=3\times5, and N=21, a=2, r=6N=21,\ a=2,\ r=6 with continued fraction [0;6,170,2][0;6,170,2] giving 21=3×721=3\times7; (5) the 4/π2≈0.4054/\pi^2\approx0.405 phase-estimation bound and the φ(r)/r\varphi(r)/r totient lower bound. All numerical results were checked programmatically.

Prerequisites. Quantum Phase Estimation · Quantum Fourier Transform · Hidden Subgroup Problem · Quantum Parallelism and the Oracle Model. Leads to. Shor's Factoring Algorithm · Quantum Discrete Logarithm Algorithm · Hidden Subgroup Problem · Simon's Algorithm.

Ask the tutor: Ask me to run the order-finding circuit on your own NN and aa — I will trace the peaks, do the continued fraction, and show exactly how the period falls out.

Is order finding the same thing as Shor's algorithm?

Not quite — order finding is the quantum engine, and Shor's algorithm is the full factoring machine around it. Shor's algorithm is mostly classical: pick a random aa coprime to NN, call order finding to get rr, and if rr is even with ar/2≢−1(modN)a^{r/2}\not\equiv-1\pmod N, read a factor from gcd⁡(ar/2±1,N)\gcd(a^{r/2}\pm1,N). The only genuinely quantum step is finding rr; everything else is classical number theory a laptop does instantly. So "Shor's algorithm" is order finding wrapped in a classical reduction that converts a period into a factor.

Why not just measure the target register after the modular exponentiation to read off the period?

You can, and it is illuminating, but it does not give rr directly. Measuring the target collapses it to some ax0  Na^{x_0}\bmod N, leaving the control register in an equal superposition over the progression x0,x0+r,x0+2r,…x_0, x_0+r, x_0+2r,\dots — periodic with period rr but shifted by an unknown offset x0x_0. Applying the QFT then yields a multiple of 2t/r2^t/r, because the Fourier transform is blind to the offset (it only shifts phases). This "measure first" picture is exactly equivalent to the eigenvalue picture above, which is just cleaner because it isolates one phase s/rs/r per branch.

What makes order finding hard for a classical computer?

No classical shortcut to the period is known that avoids essentially exponential work. You can compute ak  Na^k\bmod N quickly for any single kk, but finding the smallest kk with ak≡1a^k\equiv1 seems to require searching candidates or knowing φ(N)\varphi(N) — and computing φ(N)\varphi(N) is itself equivalent to factoring NN. The best classical factoring algorithm, the number field sieve, runs in time roughly exp⁡((log⁡N)1/3)\exp((\log N)^{1/3}), superpolynomial in the digits. The quantum advantage is that the Fourier transform reads global periodicity from a superposition in one shot, whereas classically you can only sample the sequence point by point, so the period of an exponentially long cycle stays hidden.

Where does the number-theoretic assumption enter that r is usually even and well-behaved?

It enters in Shor's classical reduction, not in order finding itself. Order finding returns whatever rr is; the reduction needs rr even and ar/2≢−1a^{r/2}\not\equiv-1. A theorem guarantees that for a random aa coprime to an odd composite NN that is not a prime power, both conditions hold with probability at least 1/21/2 (in fact ≥1−2−m\ge 1-2^{-m} for mm distinct odd prime factors). So if a run gives a useless rr, pick a fresh aa and try again; a constant expected number of attempts suffices. Prime powers and even NN are stripped off first by cheap classical tests.

Why continued fractions specifically — is there no simpler way to round j over two to the t to s over r?

Naive rounding fails because you do not know rr, so you cannot tell which nearby fraction the measurement intends. The measured x=j/2tx=j/2^t is close to s/rs/r but also to infinitely many fractions with larger denominators; you want the unique one with small denominator (below NN) near xx. Continued fractions produce exactly the best rational approximations ordered by denominator size, and the theorem that any fraction within 1/2q21/2q^2 of xx appears among its convergents turns "close" into "provably recovered." No simpler rounding rule carries that guarantee, and the smeared peaks when r∤2tr\nmid 2^t (as in the N=21N=21 example) are precisely why you need it.

How does order finding generalize to period finding and the hidden subgroup problem?

Order finding is the special case where f(x)=ax  Nf(x)=a^x\bmod N has period rr. The same circuit — uniform superposition, evaluate ff into a target, Fourier-transform the input, measure — finds the period of any efficiently computable periodic f:Z→Xf:\mathbb{Z}\to X. Recast group-theoretically, the periods form the hidden subgroup rZr\mathbb{Z} of Z\mathbb{Z}, and ff is constant on its cosets. Replacing Z\mathbb{Z} by other abelian groups and the QFT by the Fourier transform over that group solves Simon's problem (Z2n\mathbb{Z}_2^n), the discrete logarithm (a Z×Z\mathbb{Z}\times\mathbb{Z} period lattice), and the general finite abelian hidden subgroup problem — one template, many exponential speedups.

Before this

Where this leads