|q⟩ Bad Qubits

intermediate · Programming · Shor's Algorithm & Period Finding

The Period-Finding Subroutine

The previous lesson showed that factoring reduces to finding the period of the function f(x)=axmodNf(x) = a^x \bmod N for a randomly chosen base aa. This lesson walks through the quantum circuit that computes that period exponentially faster than any known classical algorithm.

The period-finding problem

Fix integers aa and NN with gcd(a,N)=1\gcd(a, N) = 1. The function f(x)=axmodNf(x) = a^x \bmod N is periodic: there exists a smallest positive integer rr — the order of aa modulo NN — such that

ar1(modN),hencef(x+r)=f(x) for all x.a^r \equiv 1 \pmod{N}, \quad \text{hence} \quad f(x + r) = f(x) \text{ for all } x.

Classically, finding rr requires sub-exponential time: the best-known algorithms (based on the number-field sieve) run in exp ⁣(O((logN)1/3(loglogN)2/3))\exp\!\bigl(O((\log N)^{1/3}(\log\log N)^{2/3})\bigr), which is super-polynomial in the bit-length n=log2Nn = \log_2 N. Shor's quantum subroutine reduces this to O((logN)3)O((\log N)^3) elementary quantum gates — an exponential advantage over all known classical approaches.

Register sizes

The circuit uses two registers:

Together the combined state lives in a 2n×N2^n \times N-dimensional Hilbert space, but we only use O(logN)O(\log N) qubits per register — a resource that grows polynomially with the size of the number being factored.

The circuit, step by step

Step 1 — Superpose all inputs

Apply HnH^{\otimes n} to the input register, which starts in 0n|0\rangle^{\otimes n}:

Hn0n=12nx=02n1x.H^{\otimes n}|0\rangle^{\otimes n} = \frac{1}{\sqrt{2^n}} \sum_{x=0}^{2^n - 1} |x\rangle.

All 2n2^n candidate exponents now exist simultaneously in superposition. The output register remains 0|0\rangle.

Step 2 — Apply the modular-exponentiation oracle

A unitary UfU_f maps x0xaxmodN|x\rangle|0\rangle \mapsto |x\rangle|a^x \bmod N\rangle. After this step the full state is

12nx=02n1xaxmodN.\frac{1}{\sqrt{2^n}} \sum_{x=0}^{2^n-1} |x\rangle |a^x \bmod N\rangle.

The function values are entangled with their inputs — neither register has a well-defined value on its own.

Step 3 — Measure the output register

Measuring axmodN|a^x \bmod N\rangle collapses the output register to some particular value v=ax0modNv = a^{x_0} \bmod N. Because ff is periodic with period rr, the surviving terms in the input register are exactly those xx values where axv(modN)a^x \equiv v \pmod{N}. These form the arithmetic progression x0,x0+r,x0+2r,x_0, x_0 + r, x_0 + 2r, \ldots The post-measurement input register state is

1Mk=0M1x0+kr,\frac{1}{\sqrt{M}} \sum_{k=0}^{M-1} |x_0 + kr\rangle,

where M2n/rM \approx 2^n / r is the number of surviving terms.

This is a uniform superposition of equally-spaced integers — a signal with a well-defined frequency 1/r1/r encoded in the amplitudes.

Step 4 — Inverse QFT on the input register

The Quantum Fourier Transform (QFT) is the quantum analogue of the discrete Fourier transform. Its inverse maps the equally-spaced superposition into a superposition of integer multiples of 2n/r2^n / r:

QFT(1Mk=0M1x0+kr)1rj=0r1e2πix0j/rj2nr.\text{QFT}^\dagger \left(\frac{1}{\sqrt{M}} \sum_{k=0}^{M-1} |x_0 + kr\rangle\right) \approx \frac{1}{\sqrt{r}} \sum_{j=0}^{r-1} e^{2\pi i x_0 j / r}\, \left|\frac{j \cdot 2^n}{r}\right\rangle.

Each term in this sum carries a phase e2πix0j/re^{2\pi i x_0 j/r} and points to the integer j2n/rj \cdot 2^n / r. The amplitudes concentrate at multiples of 2n/r2^n/r.

Step 5 — Measure the input register

Measuring the input register after the inverse QFT yields one of the values j2n/rj \cdot 2^n / r with high probability. Dividing by 2n2^n gives an approximation to the rational j/rj/r. Classical continued-fraction expansion (the topic of the next lesson) then extracts the denominator rr from this approximation.

Circuit diagram

0nHn12nxxUf12nxxaxmodNmeasure output1Mkx0+krQFT1rjj2nr\begin{array}{lll} |0\rangle^{\otimes n} & \xrightarrow{H^{\otimes n}} & \dfrac{1}{\sqrt{2^n}}\displaystyle\sum_x |x\rangle \\[8pt] & \xrightarrow{U_f} & \dfrac{1}{\sqrt{2^n}}\displaystyle\sum_x |x\rangle|a^x \bmod N\rangle \\[8pt] & \xrightarrow{\text{measure output}} & \dfrac{1}{\sqrt{M}}\displaystyle\sum_k |x_0 + kr\rangle \\[8pt] & \xrightarrow{\text{QFT}^\dagger} & \approx \dfrac{1}{\sqrt{r}}\displaystyle\sum_j \left|\dfrac{j \cdot 2^n}{r}\right\rangle \end{array}

Measure the bottom line and apply continued fractions to recover rr.

Why the QFT finds the period

Recall that a classical DFT of a sequence with period rr in a window of size Q=2nQ = 2^n has large coefficients at the frequencies j/rj/r for j=0,1,,r1j = 0, 1, \ldots, r-1. The QFT does the same thing quantumly: it maps amplitude patterns with period rr to states that are localized near integer multiples of Q/rQ/r. A single measurement then samples one of those frequencies — the analogy to reading off one peak in a classical Fourier spectrum.

The probability that a single measurement yields a "useful" outcome (one from which rr can be recovered) is Ω(1/loglogr)\Omega(1/\log \log r), so a constant number of repetitions suffice.

Putting it together

The complete subroutine costs O(n3)O(n^3) quantum gates: O(n)O(n) for the initial Hadamards, O(n3)O(n^3) for UfU_f, and O(n2)O(n^2) for the inverse QFT (which dominates HnH^{\otimes n} but not UfU_f). This contrasts with the best classical approach to order-finding, which runs in exp ⁣(O(n1/3(logn)2/3))\exp\!\bigl(O(n^{1/3}(\log n)^{2/3})\bigr) time — super-polynomial in n=log2Nn = \log_2 N.

The next lesson builds the inverse QFT explicitly and shows how it extracts a rational approximation from the measured value, completing the final bridge from quantum measurement to factored prime.

Sign in on the full site to ask questions and join the discussion.