Shor's algorithm
Multiplying two large prime numbers is easy. Recovering the primes from their product is, as far as anyone knows, extremely hard for a classical computer: the best known methods take a time that grows faster than any polynomial in the number of digits. Much of the cryptography in use today relies on that gap.
In 1994 Peter Shor showed that a quantum computer could factor in polynomial time. This lesson explains how. Most of the algorithm is classical number theory. The quantum part is one subroutine, and you have already met it: phase estimation.
From factoring to a period
Section titled “From factoring to a period”Let be the number to factor. Arithmetic modulo means keeping only the remainder after dividing by . We write when and leave the same remainder.
Pick a number between 2 and that shares no factor with . (If it does share one, the greatest common divisor is already a factor, and you are finished.) Now look at the powers of modulo . For and :
The sequence repeats. The smallest with is called the order of , and it is the period of the sequence. Here .
The order gives the factors, by a piece of school algebra. Suppose is even. Then
So divides the product of two numbers. It cannot divide the first, because then and the order would be . If it does not divide the second either, then the prime factors of must be split between the two, and
are proper factors of . Greatest common divisors are quick to compute, by Euclid’s algorithm.
For and : , and , .
The method fails if is odd, or if . Then you pick another . For an odd that is not a power of a prime, at least half of all choices of work.
For and the powers are Which pair of numbers gives the factors?
The order is 6, so use . Then and .
Everything so far is classical and fast, except for one step: finding . The order can be nearly as large as itself. For a number with hundreds of digits, stepping through the sequence until it repeats is hopeless, and no efficient classical method for finding the order is known. This is the step given to the quantum computer.
Multiplication as a gate
Section titled “Multiplication as a gate”Consider the operation “multiply by modulo ”, acting on a register that holds a number :
Because shares no factor with , this operation can be undone, by multiplying by the inverse of . It shuffles the numbers below among themselves, and a reversible shuffle of basis states is a unitary gate.
Multiplying by 2 modulo 15 happens to be especially simple. On four bits it moves every bit one place to the left, and the top bit wraps around to the bottom, because .
The wires hold the bits of a number, with place values 8, 4, 2 and 1. The register starts at 1. Build a circuit that doubles it modulo 15, giving 2. Your circuit is also tested on 2, 4, 8 and 7, which should become 4, 8, 1 and 14.
This puzzle needs JavaScript.
Hint
Each wire must end up holding what the wire below it held, and the bottom wire must receive what was on the top one. Start with a swap of the top two wires.
Apply that circuit four times and every number comes back to where it started: . That is the statement that 2 has order 4 modulo 15. The order of is hidden in the gate , and specifically in its eigenvalues.
For a general and , the gate is not a few swaps. It is a full circuit for modular multiplication, built from quantum versions of adders. Those circuits account for nearly all the cost of Shor’s algorithm.
The phases of
Section titled “The phases of UUU”Since applying exactly times does nothing, every eigenvalue of is an -th root of 1. The eigenstates are superpositions of the numbers in the sequence with steadily winding phases:
for . You can check the eigenvalue by applying , which shifts every term along the sequence by one place.
The phase of is . Phase estimation on would read out a fraction whose denominator is the order we want.
Two problems remain, and both have neat answers.
We cannot prepare . Writing it down requires knowing . But add all of these eigenstates together and the phases cancel on every term except :
The plain basis state is an equal superposition of all the eigenstates. And as the last lesson showed, phase estimation on a superposition of eigenstates returns the phase of one of them at random. So we start the register in and get back for a random .
Phase estimation needs high powers of . With counting qubits it needs , and applying that many times would take exponentially long. But is just multiplication by the number , and a classical computer finds that number with squarings. Each controlled power costs one modular multiplication, however high the power. This is the shortcut that makes the whole algorithm efficient.
Why does the algorithm start the lower register in the state ?
, so is not an eigenstate. It is the sum of all eigenstates , and phase estimation returns the phase of one of them, chosen at random.
There is a second way to see the same circuit. The controlled powers compute for every in superposition. The values that share the same form a comb with period and an unknown offset. The Fourier transform turns that comb into peaks at the multiples of , where is the size of the counting register, and removes the offset. This is the picture from the Fourier transform lesson, and it describes the same computation.
From a measurement to the period
Section titled “From a measurement to the period”The counting register has values. Measuring it gives a number with
We know and and want . The tool is the continued fraction expansion, a classical method that lists the best approximations to a number by fractions with small denominators. Take to be at least . Then whenever is the whole number nearest to a multiple of , which is the likely case, the closest fraction to with denominator below is itself. Its denominator is the order.
One catch: fractions come out in lowest terms. If and share a factor, as in , the denominator is only a divisor of . A quick check that catches this, and trying small multiples or running the circuit again fixes it.
Run it
Section titled “Run it”The simulator below does not simulate the gates. It computes the outcome probabilities directly from the period, which is what a classical computer can afford to do for numbers this small, and then follows the classical steps exactly as the real algorithm would.
Things to try:
- With and , press Measure once several times. Which outcomes occur, and which of them reveal the period?
- Choose and . The period 6 does not divide . Look at the peaks, and check that the continued fraction still finds 6.
- Choose and , then . Why does each of these fail?
- Try . How often does a single run succeed?
The whole algorithm
Section titled “The whole algorithm”- Pick a random . If , you have a factor.
- Quantum step. Run phase estimation on multiplication by , starting from , and measure .
- Use continued fractions on to get a candidate for the order . Check it, and repeat step 2 if needed.
- If is odd or , go back to step 1.
- Compute .
For an -bit number the circuit uses on the order of gates with schoolbook arithmetic, and a few repetitions suffice. The best known classical algorithm takes a time that grows faster than any polynomial in . Nobody has proved that factoring is hard classically, so the speedup is over the best known method. Even so, it was the first large quantum speedup for a problem people already cared about, and it launched the field.
How close is this to breaking encryption?
Section titled “How close is this to breaking encryption?”Not close yet, and it is worth being exact about why.
What has been done. Quantum hardware has factored numbers such as 15 and 21. Those demonstrations used circuits that were simplified with knowledge of the answer, so they show the principle and are not evidence of a useful capability.
What is needed. Factoring a 2048-bit RSA modulus takes thousands of error-corrected logical qubits running billions of gates. Each logical qubit is built from many physical ones (see the error correction track). Published estimates of the total have fallen sharply as the methods improved, from around twenty million physical qubits in 2019 to under a million in 2025, under assumptions about hardware quality that have not yet been met at scale. Present machines have from hundreds to a few thousand physical qubits and cannot yet sustain computations of that length.
What it would break. A variant of the same algorithm solves the discrete logarithm problem, so it breaks RSA, Diffie–Hellman and elliptic-curve cryptography together: almost all public-key cryptography deployed before the 2020s. It does not break symmetric ciphers or hash functions, which face only the much weaker quadratic speedup of Grover’s search.
What is being done about it. Data encrypted today can be recorded and decrypted later, so the migration to post-quantum cryptography, which rests on problems with no known quantum shortcut, is already under way.
Shor’s algorithm works because factoring has hidden periodic structure for interference to find. Most problems have no such structure. The next lesson looks at what a quantum computer can do for a problem with no structure at all.
Remember it
Section titled “Remember it”To what problem does Shor's algorithm reduce factoring ?
Finding the order of a number modulo : the smallest with .
Given an even order of modulo , how do you get factors of ?
Compute and . This works unless .
What is the quantum step in Shor's algorithm?
Phase estimation on the gate , whose eigenvalues are .
Why does Shor's algorithm run phase estimation on the state ?
is an equal superposition of all the eigenstates of multiplication by , so the result is for a random .
In Shor's algorithm, how is the order recovered from the measured number ?
. The continued fraction expansion finds the closest fraction with denominator below , and its denominator is or a divisor of it.
Which cryptography does Shor's algorithm break, and which does it leave standing?
It breaks RSA, Diffie–Hellman and elliptic-curve cryptography. Symmetric ciphers and hash functions are not broken by it.