|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)=ax mod Nf(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 ax mod Na^x \bmod N asks: raise aa to the power xx, then take the remainder on dividing by NN. For example,

71 mod 15=7,72 mod 15=49 mod 15=4,73 mod 15=7⋅4 mod 15=28 mod 15=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,…,N−1)(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

ar≡1(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 ax mod Na^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(log⁡x)O(\log x) multiplications by exploiting the binary representation of xx:

a13=a8+4+1=a8⋅a4⋅a1.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:

result←1,base←a mod N.\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) mod N\text{result} \leftarrow (\text{result} \times \text{base}) \bmod N.
  2. Set base←base2 mod N\text{base} \leftarrow \text{base}^2 \bmod N.
  3. Set x←⌊x/2⌋x \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 | 72 mod 15=47^2 \bmod 15 = 4 | 1 | | x=2x=2 (even) | 0 | 42 mod 15=14^2 \bmod 15 = 1 | 1 | | x=1x=1 (odd) | 1 | 12 mod 15=11^2 \bmod 15 = 1 | (1×1) mod 15=1(1 \times 1) \bmod 15 = 1 |

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

Why this matters for quantum circuits

In the full Shor algorithm the quantum register holds a superposition ∑x=02n−1∣x⟩∣0⟩\sum_{x=0}^{2^n-1} |x\rangle |0\rangle. A controlled unitary then maps this to ∑x∣x⟩∣ax mod N⟩\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 74 mod 157^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.