Skip to content

9.9

The idea behind Shor’s algorithm

Factoring → period finding → reading the period with the QFT: every leg of the triple jump hand-computed on N = 15; the gap between polynomial and sub-exponential, hanging over RSA.

Recommended first

After this section you should be able to

  • Recount the full chain: factoring N ⇒ finding the period r of a^x mod N ⇒ extracting factors via gcd(a^{r/2} ± 1, N)
  • Hand-compute the entire pipeline for N = 15, a = 7, verifying every link
  • Explain why the QFT turns a "period" into a "measurable peak": periodic superposition → interference concentration
  • Compare quantum O((log N)³) against classical sub-exponential complexity, and lay out the real threat to RSA and its likely timeline

Grover’s ending pointed the way: for exponential speedup, find a problem with structure. In 1994 Peter Shor found that problem — factoring large integers. Its structure is a hidden periodicity, and “finding periods with interference” has been quantum mechanics’ signature trade since the double-slit experiment. This section does not dive into number-theoretic proofs, but it works the full logical chain and the quantum part all the way through.

The problem at the door: multiplication is easy, division is deadly

3×5=153\times5=15 — mental arithmetic. The reverse, “15 = ? × ?”, is not hard either. But make the number 617 digits long (2048 bits): multiplying two thousand-digit primes takes a computer milliseconds; recovering the two factors from the product, using the fastest known classical algorithm (the number field sieve), takes time on the order of

exp ⁣[c(lnN)1/3(lnlnN)2/3],c1.9(9.9.1)\exp\!\left[c\,(\ln N)^{1/3}(\ln\ln N)^{2/3}\right],\qquad c\approx1.9\tag{9.9.1}

sub-exponential: far worse than polynomial. For a 2048-bit NN, all the computing power on Earth combined would need millennia. The security of RSA public-key cryptography is staked on exactly this one-way street: the public key displays NN in plain sight, and whoever can factor it can read every ciphertext.

First leg: factoring ⇒ period finding (classical number theory, results only)

Shor’s first move is to rewrite the problem. Pick any aa coprime to NN and look at the modular exponential

f(x)=axmodN(9.9.2)f(x)=a^x \bmod N\tag{9.9.2}

It is necessarily periodic: f(x+r)=f(x)f(x+r)=f(x), and the smallest positive period rr is called the order of aa. Number theory provides a bridge (stated without proof):

If rr is even and ar/2≢1(modN)a^{r/2}\not\equiv-1\pmod N, then gcd(ar/21,N)\gcd(a^{r/2}-1,\,N) and gcd(ar/2+1,N)\gcd(a^{r/2}+1,\,N) yield nontrivial factors of NN.

The intuition in one line: ar1a^r\equiv1 means (ar/21)(ar/2+1)0(modN)(a^{r/2}-1)(a^{r/2}+1)\equiv0\pmod NNN divides the product yet (under the stated conditions) divides neither factor, so NN‘s prime factors have been split between the two sides, and gcd\gcd (the Euclidean algorithm — blazingly fast) collects one from each. For a random aa the conditions hold with probability at least 1/21/2; if they fail, pick another aa and try again.

Hand-compute N=15N=15 with a=7a=7:

71=7,72=494,732813,74911(mod15)(9.9.3)7^1=7,\quad 7^2=49\equiv4,\quad 7^3\equiv28\equiv13,\quad 7^4\equiv91\equiv1\pmod{15}\tag{9.9.3}

The period is r=4r=4 — even, and 724≢17^2\equiv4\not\equiv-1. Therefore

gcd(721,15)=gcd(48,15)=3,gcd(72+1,15)=gcd(50,15)=5(9.9.4)\gcd(7^2-1,15)=\gcd(48,15)=3,\qquad \gcd(7^2+1,15)=\gcd(50,15)=5\tag{9.9.4}

15=3×515=3\times5 — success. The only hard step left in the entire algorithm is finding the period rr. Classically, period finding is no cheaper than factoring outright (you would have to grind ff through trillions of steps before seeing a single cycle); this is precisely the step quantum mechanics takes over.

Second leg: loading the period into a superposition

Open with the DJ recipe: hit the first register (tt qubits, Q=2tr2Q=2^t\gg r^2) with a row of HH‘s, compute the modular exponential for every xx in parallel, and write it into the second register:

1Qx=0Q1xaxmodN(9.9.5)\frac{1}{\sqrt Q}\sum_{x=0}^{Q-1}\ket{x}\ket{a^x\bmod N}\tag{9.9.5}

Now measure the second register (you don’t actually have to — it just makes the story easier to tell), obtaining some value f0f_0. The first register thereupon collapses to the equal-weight superposition of all the xx that produce that value — and these xx form exactly an arithmetic progression x0, x0+r, x0+2r,x_0,\ x_0+r,\ x_0+2r,\dots:

Ψ=1mj=0m1x0+jr,mQ/r(9.9.6)\ket{\Psi}=\frac{1}{\sqrt m}\sum_{j=0}^{m-1}\ket{x_0+jr},\qquad m\approx Q/r\tag{9.9.6}

The period is now etched into the state, but a direct measurement would just spit out one random x0+jrx_0+jr — the random starting point x0x_0 scrambles the period information (each rerun gives a different x0x_0). The old problem: the information went in but won’t come out. The old answer: interference. This time the interference machine is the quantum Fourier transform.

Third leg: the QFT turns the period into peaks

The quantum Fourier transform (QFT) is the deluxe version of DJ’s row of HH‘s — upgrading “bitwise phases” to “continuous phases”:

QFTx=1Qy=0Q1e2πixy/Qy(9.9.7)\mathrm{QFT}\ket{x}=\frac{1}{\sqrt Q}\sum_{y=0}^{Q-1} \ee^{2\pi\ii xy/Q}\ket{y}\tag{9.9.7}

(The phases of HnH^{\otimes n} are only ±1\pm1; the QFT’s phases take QQ ticks around the unit circle. It can be built from O(t2)O(t^2) gates — HH‘s plus controlled phase rotations; we skip the circuit here.)

What comes next

Shor needs trillions of gate operations to run back-to-back without a slip, and real qubits are delicate creatures: a one-percent gate error counts as excellent, and even left alone they quietly drift. Classical computers correct errors with redundant backups — but quantum states cannot be cloned (Section 9.5), and a measurement collapses them (Section 9.1) — so how do you back up, and how do you even notice an error? The next section is this chapter’s finale: quantum error correction. Its answer is unexpectedly beautiful, and it is a grand reunion of everything this chapter has built — entanglement, measurement, stable correlations.

Section 73 of 106 · use to turn the page