Shor's Algorithm
Factoring is not the quantum part. Finding the period of a function is — and everything else is number theory you could do on a laptop.
Before this: Grover's Search
In this lesson you’ll learn
- Explain how factoring reduces to finding the period of a modular exponential
- Describe what the quantum Fourier transform reads off the phase ramp
- Recover the period from a measured value using continued fractions
- State honestly what breaking RSA would cost in physical qubits
The reduction: factoring is period-finding in disguise
Shor's algorithm is usually introduced as "the quantum algorithm that factors numbers", which is true and also slightly misleading, because the factoring is not where the quantum computer earns its keep. The algorithm is a classical reduction wrapped around one quantum subroutine, and the subroutine does not know what factoring is. It finds the period of a periodic function. That is all it does.
Here is the reduction. Suppose you want to factor N. Pick a random integer a with 1 < a < N. Compute gcd(a, N) — if that is not 1 you have stumbled onto a factor by luck and you are done, which essentially never happens for the semiprimes RSA uses. Otherwise, consider the function f(x) = a^x mod N. Because a and N are coprime, this function is periodic: there is some smallest r > 0 with a^r ≡ 1 (mod N), and thereafter f repeats with period r. That r is called the multiplicative order of a modulo N.
Now do a little algebra. If r happens to be even, then a^r − 1 ≡ 0 (mod N) factors as (a^{r/2} − 1)(a^{r/2} + 1) ≡ 0 (mod N). So N divides that product. Unless one of the two factors is itself a multiple of N — the failure case, which is what you get when a^{r/2} ≡ −1 (mod N) — the factors of N must be split between the two brackets. Compute gcd(a^{r/2} − 1, N) and gcd(a^{r/2} + 1, N) and at least one of them is a non-trivial factor of N.
That is the whole classical part, and it is genuinely elementary — Euclid's algorithm and one line of high-school algebra. The failure modes (r odd, or a^{r/2} ≡ −1) happen with probability at most about 1/2 for a random a, so you retry with a fresh a and succeed after a couple of attempts on average. Everything difficult has been pushed into one place: finding r. Classically, finding the order of a modulo N is believed to be as hard as factoring itself, and no sub-exponential algorithm for it is known. Quantumly, it is the natural thing a quantum computer does.
Predict before you read on
The quantum computer's one job is now to find the period r of f(x) = a^x mod N. A natural plan: prepare a superposition over all x, compute f into a second register, measure BOTH registers, and repeat until the collected (x, f(x)) samples reveal the period. Why does this plan fail?
Commit to an answer — the next section is the payoff.
The rest of this lesson is part of the Advanced track
You have read the opening of Shor's Algorithm, and 10 more sections are waiting — the part where the idea actually gets built. The Advanced track picks up exactly where Quantum Foundations stops, and Pro opens all of it.
- The other three Advanced lessons — error correction, VQE, and quantum cryptography
- Certification for the Advanced track, with a shareable certificate
- Runs on real IBM Quantum hardware, not just the simulator
- The live Claude-powered tutor, on every lesson and every circuit
The whole Quantum Foundations course stays free, forever, with no account required.