|q⟩ Bad Qubits

intermediate · Programming · Shor's Algorithm & Period Finding

Modular Exponentiation

Shor's algorithm factors an integer NN by finding the period of the function f(x)=axmodNf(x) = a^x \bmod N for a randomly chosen base aa. Before the quantum part can shine, a classical circuit must evaluate f(x)f(x) for every xx in superposition. That building block is called modular exponentiation.

What modular arithmetic means

The expression axmodNa^x \bmod N asks: raise aa to the power xx, then take the remainder on dividing by NN. For example,

71mod15=7,72mod15=49mod15=4,73mod15=74mod15=28mod15=13.7^1 \bmod 15 = 7, \quad 7^2 \bmod 15 = 49 \bmod 15 = 4, \quad 7^3 \bmod 15 = 7 \cdot 4 \bmod 15 = 28 \bmod 15 = 13.

Because there are only NN possible remainders (0,1,,N1)(0, 1, \ldots, N-1), the sequence of values f(0),f(1),f(2),f(0), f(1), f(2), \ldots must eventually repeat. The length of that repeating block is the period rr, defined by

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

For a=7a = 7, N=15N = 15 you can check that 74=2401=160×15+17^4 = 2401 = 160 \times 15 + 1, so r=4r = 4.

Repeated squaring

Computing axmodNa^x \bmod N directly by multiplying aa together xx times is far too slow for large xx. Repeated squaring (also called fast modular exponentiation) reduces the cost to O(logx)O(\log x) multiplications by exploiting the binary representation of xx:

a13=a8+4+1=a8a4a1.a^{13} = a^{8+4+1} = a^8 \cdot a^4 \cdot a^1.

The algorithm squares the running base and takes its remainder mod NN at each step, multiplying it into the running result whenever the current low bit of the exponent is 1:

result1,baseamodN.\text{result} \leftarrow 1, \quad \text{base} \leftarrow a \bmod N.

Then, while x>0x > 0:

  1. If xx is odd, set result(result×base)modN\text{result} \leftarrow (\text{result} \times \text{base}) \bmod N.
  2. Set basebase2modN\text{base} \leftarrow \text{base}^2 \bmod N.
  3. Set xx/2x \leftarrow \lfloor x/2 \rfloor.

For a=7a = 7, x=4x = 4, N=15N = 15:

| Step | exponent bit | base | result | |------|-------------|------|--------| | start | — | 7 | 1 | | x=4x=4 (even) | 0 | 72mod15=47^2 \bmod 15 = 4 | 1 | | x=2x=2 (even) | 0 | 42mod15=14^2 \bmod 15 = 1 | 1 | | x=1x=1 (odd) | 1 | 12mod15=11^2 \bmod 15 = 1 | (1×1)mod15=1(1 \times 1) \bmod 15 = 1 |

Result: 74mod15=17^4 \bmod 15 = 1.

Why this matters for quantum circuits

In the full Shor algorithm the quantum register holds a superposition x=02n1x0\sum_{x=0}^{2^n-1} |x\rangle |0\rangle. A controlled unitary then maps this to xxaxmodN\sum_{x} |x\rangle |a^x \bmod N\rangle in one step — but the gate that performs this mapping is exactly the quantum version of modular exponentiation. Understanding the classical algorithm is the first step to understanding how that controlled unitary is constructed.

Try it

Implement the classical modpow(a, x, N) function using repeated squaring and return 74mod157^4 \bmod 15. The grader checks that your answer equals 1.

Run your code to see the quantum state.

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