The Period-Finding Subroutine
The previous lesson showed that factoring reduces to finding the period of the function for a randomly chosen base . This lesson walks through the quantum circuit that computes that period exponentially faster than any known classical algorithm.
The period-finding problem
Fix integers and with . The function is periodic: there exists a smallest positive integer — the order of modulo — such that
Classically, finding requires sub-exponential time: the best-known algorithms (based on the number-field sieve) run in , which is super-polynomial in the bit-length . Shor's quantum subroutine reduces this to elementary quantum gates — an exponential advantage over all known classical approaches.
Register sizes
The circuit uses two registers:
- Input register: qubits, where (enough to hold at least distinct values). For , qubits suffice since .
- Output (ancilla) register: qubits to store values of .
Together the combined state lives in a -dimensional Hilbert space, but we only use 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 to the input register, which starts in :
All candidate exponents now exist simultaneously in superposition. The output register remains .
Step 2 — Apply the modular-exponentiation oracle
A unitary maps . After this step the full state is
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 collapses the output register to some particular value . Because is periodic with period , the surviving terms in the input register are exactly those values where . These form the arithmetic progression The post-measurement input register state is
where is the number of surviving terms.
This is a uniform superposition of equally-spaced integers — a signal with a well-defined frequency 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 :
Each term in this sum carries a phase and points to the integer . The amplitudes concentrate at multiples of .
Step 5 — Measure the input register
Measuring the input register after the inverse QFT yields one of the values with high probability. Dividing by gives an approximation to the rational . Classical continued-fraction expansion (the topic of the next lesson) then extracts the denominator from this approximation.
Circuit diagram
Measure the bottom line and apply continued fractions to recover .
Why the QFT finds the period
Recall that a classical DFT of a sequence with period in a window of size has large coefficients at the frequencies for . The QFT does the same thing quantumly: it maps amplitude patterns with period to states that are localized near integer multiples of . 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 can be recovered) is , so a constant number of repetitions suffice.
Putting it together
The complete subroutine costs quantum gates: for the initial Hadamards, for , and for the inverse QFT (which dominates but not ). This contrasts with the best classical approach to order-finding, which runs in time — super-polynomial in .
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.