Course Home
Shor's Algorithm Core

Quantum Period Finding

How superposition and interference reveal hidden periodicity in modular arithmetic

1. The Problem: Finding Periodicity

Given integers $a$ and $N$ with $\gcd(a, N) = 1$, consider the function:

$$f(x) = a^x \bmod N$$

This function is periodic. The order (or period) $r$ is the smallest positive integer such that:

$$a^r \equiv 1 \pmod{N}$$

Equivalently, $f(x+r) = f(x)$ for all $x \geq 0$. Finding $r$ efficiently is the key to factoring $N$.

Connection to Factoring

If $r$ is even and $a^{r/2} \not\equiv -1 \pmod{N}$, then at least one of $\gcd(a^{r/2} - 1,\; N)$ or $\gcd(a^{r/2} + 1,\; N)$ is a non-trivial factor of $N$.

ƒ Explore the Periodicity

2. Why Quantum?

Classically, finding the period $r$ of $a^x \bmod N$ by brute force requires up to $r$ evaluations — and $r$ can be nearly as large as $N$ itself. The best classical algorithms for the underlying factoring problem are sub-exponential but still super-polynomial in the number of digits:

AlgorithmComplexityType
Trial division$O(\sqrt{N})$Exponential in bit-length
Pollard's rho$O(N^{1/4})$Exponential in bit-length
General Number Field Sieve$O\!\left(e^{c(\ln N)^{1/3}(\ln\ln N)^{2/3}}\right)$Sub-exponential
Shor's algorithm (quantum)$O\!\left((\log N)^3\right)$Polynomial
Classical
~1030 ops
Quantum
~1010 ops

The quantum speedup comes from two ingredients:

  1. Quantum parallelism: A superposition lets us evaluate $f(x)$ for all $x$ simultaneously.
  2. Quantum Fourier Transform: The QFT extracts the hidden period from the entangled state via constructive interference at the right frequencies.
Scale matters. For a 2048-bit RSA modulus, the classical GNFS would need roughly $\sim 10^{30}$ operations. Shor's algorithm needs only $\sim (2048)^3 \approx 10^{10}$ quantum gate operations — a difference of $10^{20}$.

3. The Quantum Circuit

The period-finding algorithm uses two quantum registers and four stages:

  1. Initialize both registers to $|0\rangle$
  2. Hadamard gates create a uniform superposition in the counting register
  3. Controlled modular exponentiation entangles the two registers
  4. Inverse QFT on the counting register reveals the period

Walk through the circuit step-by-step for the example $N = 15$, $a = 7$ (period $r = 4$) with 4 counting qubits:

Circuit Walkthrough
Step 0: Initialization

Measurement Probabilities

Bar height = probability $|\alpha|^2$  |  Color = complex phase:  $0$ to $2\pi$

From Entanglement to Periodicity

After all the controlled operations, the full state is $\frac{1}{4}\sum_{x=0}^{15}|x\rangle|7^x \bmod 15\rangle$. Each counting register value $|x\rangle$ is paired with a specific work register value $|f(x)\rangle$. If we group the terms by their work register value, something remarkable appears — each group forms a periodic pattern in $x$ with the same period $r = 4$:

Entangled State Decomposition

Each bar is one term $|x\rangle|f(x)\rangle$ in the superposition, colored by the work register value $f(x) = 7^x \bmod 15$:

Now imagine measuring the work register. The state collapses to only those $|x\rangle$ values that share the same $f(x)$ — a periodic subset with period $r$. Click a button to see what happens:

4. How the QFT Reveals Periodicity

The Quantum Fourier Transform maps a computational basis state $|j\rangle$ to:

$$\text{QFT}|j\rangle = \frac{1}{\sqrt{Q}} \sum_{k=0}^{Q-1} \omega^{jk} |k\rangle, \qquad \omega = e^{2\pi i / Q}$$

The key insight: if the input state is periodic with period $r$, then the QFT output has amplitudes concentrated at multiples of $Q/r$ — and nearly zero everywhere else. Let's see exactly why this happens.

Why Cancellation Happens

After the modular exponentiation, the counting register holds a periodic superposition — equal amplitudes at evenly-spaced positions $x_0,\; x_0 + r,\; x_0 + 2r,\; \ldots$:

$$|\psi\rangle = \frac{1}{\sqrt{m}} \sum_{j=0}^{m-1} |x_0 + jr\rangle$$

where $m \approx Q/r$ is the number of terms. Applying the QFT, the amplitude at output $|k\rangle$ is:

$$\alpha_k = \frac{1}{\sqrt{Qm}} \sum_{j=0}^{m-1} \omega^{(x_0 + jr)k} = \frac{\omega^{x_0 k}}{\sqrt{Qm}} \underbrace{\sum_{j=0}^{m-1} e^{2\pi i \cdot jrk/Q}}_{\large S(k)}$$

Since $|\omega^{x_0 k}| = 1$, the probability $|\alpha_k|^2 = |S(k)|^2 / (Qm)$ depends only on the sum $S(k)$. Each term in this sum is a unit-length phasor (a complex number on the unit circle) with angle $\varphi_j = 2\pi j \cdot rk/Q$. Consecutive phasors differ by a fixed phase step:

$$\Delta\varphi = \frac{2\pi r k}{Q}$$

Constructive Interference

When $k$ is a multiple of $Q/r$, the phase step $\Delta\varphi$ is a multiple of $2\pi$. Every phasor points in the same direction. They add up: $|S(k)| = m$, giving probability $\approx 1/r$.

Destructive Interference

When $k$ is NOT a multiple of $Q/r$, the phasors spread around the unit circle and cancel each other out. The geometric series gives $|S(k)| \approx 0$. The more terms (larger $m$), the more complete the cancellation.

Explore this visually — drag the slider to change $k$ and watch the phasors align or cancel:

Phasor Cancellation Explorer

Phasor Diagram (tip-to-tail sum)

$|S(k)|^2$ for all k

Each colored arrow is one phasor $e^{2\pi i \cdot jrk/Q}$  |  The thick arrow is the resultant sum $S(k)$  |  Click a bar to jump to that $k$

QFT Matrix Visualization

Each entry $(\text{QFT})_{jk} = \frac{1}{\sqrt{Q}}\,\omega^{jk}$. The color encodes the phase of $\omega^{jk}$:

QFT Matrix
Hover a cell to see its value. Color = phase of $\omega^{jk}$.

See QFT in Action

Choose an input state and observe how the QFT transforms it. Periodic inputs produce sharp peaks in the output:

QFT Demo

Input State

After QFT

5. Parameter Explorer

Choose $N$ and $a$ to run the full quantum period-finding algorithm. Observe how the measurement probabilities cluster around multiples of $Q/r$, allowing us to extract $r$.

Parameter Explorer
8

6. Classical Post-Processing

After measuring the counting register and obtaining outcome $y$ from a register of size $Q = 2^n$, we need to extract the period $r$. The key idea: the measured $y$ satisfies:

$$\frac{y}{Q} \approx \frac{s}{r}$$

for some integer $s$ with $0 \leq s < r$. We use the continued fractions algorithm to find the best rational approximation $s/r$ from the decimal $y/Q$.

Continued Fractions

Any real number $\alpha$ can be written as $\alpha = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cdots}}$, denoted $[a_0; a_1, a_2, \ldots]$. The convergents $p_k/q_k$ are the best rational approximations with denominator $\leq q_k$.

Continued Fractions Extractor

7. Putting It All Together

The quantum period-finding algorithm combines quantum parallelism, entanglement, and Fourier analysis into a single coherent pipeline:

Σ Algorithm Pipeline
StageWhat Happens
1 SuperpositionHadamard gates create $\frac{1}{\sqrt{Q}}\sum_{x=0}^{Q-1}|x\rangle|0\rangle$
2 EntanglementControlled-$U_f$ maps to $\frac{1}{\sqrt{Q}}\sum_{x}|x\rangle|a^x \bmod N\rangle$
3 InterferenceQFT$^{-1}$ concentrates amplitudes at multiples of $Q/r$
4 MeasurementYields $y \approx sQ/r$ with high probability
5 Post-processingContinued fractions extract $r$ from $y/Q$
6 Factoring$\gcd(a^{r/2} \pm 1, N)$ yields non-trivial factors

Try It: Factor a Number

Run the complete Shor's algorithm pipeline end-to-end. Pick a number $N$ to factor, and watch each stage unfold — from choosing a random base $a$ through quantum period finding to extracting the factors:

End-to-End Factoring Simulator
Success probability. The algorithm succeeds with probability $\geq 1 - 1/2^k$ after $O(k)$ repetitions. Even a single run succeeds with probability $\Omega(1/\!\log\log N)$.

Further Reading

What's Next?

You now understand how quantum period finding works — from superposition and modular exponentiation through QFT interference and continued fractions. In the next module, we put all the pieces together into Shor's complete factoring algorithm, including error handling, success probability analysis, and what it means for real-world cryptography.

Continue to Shor's Algorithm

8. Self-Check

Test your understanding of quantum period finding. Select the best answer for each question.

navigate steps   Space play/pause
Previous
Factoring → Period Finding
Course
All Modules
Next
Shor's Algorithm