|q⟩ Bad Qubits

intermediate · Programming · Shor's Algorithm & Period Finding

Reduction to Period Finding

Shor's algorithm does not directly decompose a large integer. Instead it transforms the factoring problem into a completely different one — period finding — and then solves that with the quantum Fourier transform. This lesson traces that transformation step by step.

Why period finding breaks factoring

Let NN be the integer you want to factor, and pick any aa with 1<a<N1 \lt a \lt N and gcd(a,N)=1\gcd(a, N) = 1 (a is coprime to NN). Consider the sequence

a0modN,  a1modN,  a2modN,  a3modN,a^0 \bmod N,\; a^1 \bmod N,\; a^2 \bmod N,\; a^3 \bmod N, \ldots

Because there are only NN possible remainders, this sequence must eventually repeat. The smallest positive integer rr such that

ar1(modN)a^r \equiv 1 \pmod{N}

is called the order (or period) of aa modulo NN. It always exists when gcd(a,N)=1\gcd(a, N) = 1.

From the period to a factor

Once you know rr, a single algebraic identity yields a factor with high probability.

Step 1. If rr is odd, or if ar/21(modN)a^{r/2} \equiv -1 \pmod{N}, the argument fails — restart with a different aa. Both bad cases happen with probability at most 12\tfrac{1}{2} for any composite NN that is not a prime power, so on average two random choices of aa suffice.

Step 2. If rr is even and ar/2≢1(modN)a^{r/2} \not\equiv -1 \pmod{N}, write the identity

ar10(modN)a^r - 1 \equiv 0 \pmod{N}

and factor the left side as a difference of squares:

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

Since neither factor is 0(modN)0 \pmod{N} (by assumption), the product is divisible by NN but neither factor alone is. That means NN shares a non-trivial common factor with at least one of ar/21a^{r/2} - 1 and ar/2+1a^{r/2} + 1.

Step 3. Compute gcd(ar/21,N)\gcd(a^{r/2} - 1, N) and gcd(ar/2+1,N)\gcd(a^{r/2} + 1, N). At least one of these is a proper factor of NN.

Worked example: factoring 15

Choose a=7a = 7, N=15N = 15. The period is r=4r = 4 (even, and 72=494≢1(mod15)7^2 = 49 \equiv 4 \not\equiv -1 \pmod{15}).

Compute ar/2=72=494(mod15)a^{r/2} = 7^2 = 49 \equiv 4 \pmod{15}.

gcd(41,  15)=gcd(3,15)=3,gcd(4+1,  15)=gcd(5,15)=5.\gcd(4 - 1,\; 15) = \gcd(3, 15) = 3, \qquad \gcd(4 + 1,\; 15) = \gcd(5, 15) = 5.

Both GCDs are proper factors, confirming 15=3×515 = 3 \times 5.

The only remaining difficulty is computing rr efficiently. Classically, evaluating akmodNa^k \bmod N for all kk up to rr takes time exponential in the bit-length of NN. The quantum speedup comes entirely from the next step: using the quantum Fourier transform to read off the period from a superposition of all powers at once.

The full reduction in one sentence

Shor's algorithm replaces the classically hard question "what are the prime factors of NN?" with the quantumly easy question "what is the order of aa modulo NN?", then extracts factors from the order using three lines of classical arithmetic.

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