12 min read
Order-Finding and Factoring
The classical number-theory reduction that turns factoring into order-finding, and why a unitary built from modular multiplication encodes the order as an eigenvalue.
Suggested before this lesson
Shor's algorithm (next lesson) is famous for factoring integers, but the quantum part of it doesn't touch factoring directly. It solves a completely different-looking problem, order-finding, and this lesson is the classical number theory that connects the two: a reduction that was already known before Shor, his contribution was making order-finding itself efficient quantumly, using exactly the QPE circuit from last lesson.
For integers and with , the order of modulo , written , is the smallest positive integer such that
Such an always exists when (a consequence of Euler's theorem), and the powers cycle with period exactly .
Suppose is composite (not prime, not a prime power) and you've found the order of some modulo . If is even and , then both
are nontrivial factors of (neither 1 nor itself). Why: means divides , but (given the two conditions above) divides neither factor on its own, so 's prime factors must be split between the two, and the gcd with each side pulls exactly that split out. For a randomly chosen coprime to , this works (gives a valid , even, with ) with probability at least , so a few random retries make success overwhelmingly likely.
Compute powers of 7 mod 15: , , , . So (even ✓). Then , and ✓, both conditions hold:
. Factored, using nothing but the order and two classical gcd computations, both efficient (Euclid's algorithm).
Switch and see the sequence cycle back to 1 after a different number of steps each time, that step count is .
Powers of a, mod N
4
Switch and watch the sequence of remainders repeat with a different period every time. That period is exactly the number Quantum Phase Estimation is used to extract: the sequence is periodic with period for any coprime to 15r$, the classical post-processing in the next lesson turns it into 15's factors.
Finding classically (trying powers of one at a time) takes exponentially many steps in the number of digits of ; this is the actual hard part real-world factoring relies on. Quantum Phase Estimation can find efficiently, given the right unitary. Define on a register large enough to hold values :
Modular multiplication by , as a reversible gate: feed in , get out . It's unitary because is a bijection on whenever : a permutation matrix, which is automatically unitary regardless of how complicated the permutation looks.
Black Box
is unitary for the same reason is: it's a permutation of basis states (no two different map to the same , since is invertible mod ), and every permutation matrix is automatically unitary, its inverse is just running the permutation backwards, .
's eigenvectors turn out to be
with eigenvalue (check: applying shifts every to , i.e. relabels the sum's index, which pulls out exactly a factor of thanks to the phases already built into 's definition). Feed and any single (for ) into QPE from last lesson, and it returns : a fraction with sitting in the denominator, recoverable from by a classical algorithm called continued fractions, without ever needing to know up front. Next lesson covers the one remaining wrinkle, preparing without already knowing , and puts every piece from this and the last three lessons together into the complete algorithm.