|q⟩ Bad Qubits

intermediate · Programming · Shor's Algorithm & Period Finding

Factoring 15

Shor's algorithm is the clearest demonstration that quantum computers can break the hardest problems in classical cryptography. Factoring N=15N = 15 is the smallest non-trivial example: it is small enough to trace by hand yet rich enough to expose every component of the algorithm.

The full Shor pipeline

Shor's algorithm combines classical number theory with a single quantum subroutine. For N=15N = 15 the five-step outline is:

  1. Choose a random base aa with 1<a<N1 \lt a \lt N and gcd(a,N)=1\gcd(a, N) = 1. We pick a=7a = 7.
  2. Detect a lucky shortcut. If gcd(a,N)1\gcd(a, N) \neq 1 you already have a factor — but with a composite NN that is not a prime power this happens with negligible probability.
  3. Run the quantum period-finding subroutine to find the order rr of aa modulo NN, i.e. the smallest positive rr with ar1(modN)a^r \equiv 1 \pmod{N}.
  4. Classical post-processing: from rr extract a factor using gcd(ar/2±1,N)\gcd(a^{r/2} \pm 1,\, N).
  5. Verify. If the GCD step fails (rare), restart with a different aa.

Step 3 in detail: the quantum subroutine

The quantum subroutine uses quantum phase estimation (QPE) applied to the unitary UaU_a defined by Uay=aymodNU_a |y\rangle = |ay \bmod N\rangle. Its eigenstates are the states

us=1rk=0r1e2πisk/rakmodN,s=0,1,,r1,|u_s\rangle = \frac{1}{\sqrt{r}} \sum_{k=0}^{r-1} e^{-2\pi i sk/r} |a^k \bmod N\rangle, \qquad s = 0, 1, \ldots, r-1,

with eigenvalue e2πis/re^{2\pi i s/r}. QPE estimates the phase s/rs/r, and the continued-fraction algorithm extracts the denominator rr from that rational approximation.

For N=15N = 15 with a=7a = 7 the period is r=4r = 4. You can verify this directly:

717,724,7313,741(mod15).7^1 \equiv 7,\quad 7^2 \equiv 4,\quad 7^3 \equiv 13,\quad 7^4 \equiv 1 \pmod{15}.

A practical QPE circuit for N=15N = 15 uses n=log215=4n = \lceil \log_2 15 \rceil = 4 work qubits in the target register and t=2n=8t = 2n = 8 counting qubits for sufficient precision. The full circuit has hundreds of gates; in this lesson we focus on the classical bookkeeping around the quantum call.

Step 4 in detail: classical post-processing

With the period r=4r = 4 in hand, the factoring step is pure arithmetic.

Check applicability. Is rr even? Yes, 44 is even. Is ar/21(modN)a^{r/2} \equiv -1 \pmod{N}? Compute 72mod15=49mod15=47^2 \bmod 15 = 49 \bmod 15 = 4. Since 4141(mod15)4 \neq 14 \equiv -1 \pmod{15}, the condition is satisfied.

Apply the difference-of-squares identity. Since ar1(modN)a^r \equiv 1 \pmod{N}, we have

(ar/21)(ar/2+1)0(modN).(a^{r/2} - 1)(a^{r/2} + 1) \equiv 0 \pmod{N}.

For a=7a = 7, r=4r = 4:

(721)(72+1)=48×50=2400=160×15.(7^2 - 1)(7^2 + 1) = 48 \times 50 = 2400 = 160 \times 15.

Extract factors with Euclid's algorithm.

gcd(721,  15)=gcd(48,  15)=gcd(3,  15)=3,\gcd(7^2 - 1,\; 15) = \gcd(48,\; 15) = \gcd(3,\; 15) = 3, gcd(72+1,  15)=gcd(50,  15)=gcd(5,  15)=5.\gcd(7^2 + 1,\; 15) = \gcd(50,\; 15) = \gcd(5,\; 15) = 5.

Both 33 and 55 are proper factors of 1515, and indeed 15=3×515 = 3 \times 5.

Why no classical algorithm matches this

The best classical factoring algorithm for a general integer NN is the general number field sieve (GNFS), which runs in time exp ⁣(O((logN)1/3(loglogN)2/3))\exp\!\bigl(O((\log N)^{1/3}(\log\log N)^{2/3})\bigr) — super-polynomial. Shor's quantum algorithm runs in O((logN)3)O((\log N)^3) gate operations, an exponential improvement. For N=15N = 15 the difference is invisible; for a 2048-bit RSA modulus it means the difference between a few hours on a fault-tolerant quantum computer and the age of the universe on any classical machine.

Try it

The quantum subroutine has already run and returned period r=4r = 4 for a=7a = 7, N=15N = 15. Your task is to complete the classical post-processing step: compute gcd(ar/21,N)\gcd(a^{r/2} - 1,\, N) using the provided helpers and return the factor.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.