Modular arithmetic & roots of unity
Imagine a clock that never stops — numbers that wrap around forever. Now imagine dots perfectly spaced around a circle, like hour marks. These two ideas look unrelated. They're secretly the same thing. And that shared secret is exactly what Shor's algorithm exploits to break modern encryption.
A pattern hides inside numbers — and a quantum machine finds it.
Here's something that looks like magic at first. Take the number 2 and keep multiplying it by itself, but after each step, ask: what's the remainder when you divide by 5?
$2^1 \mod 5 = 2$
$2^2 \mod 5 = 4$
$2^3 \mod 5 = 3$
$2^4 \mod 5 = 1$
$2^5 \mod 5 = 2 \quad \leftarrow\text{ back to the start}$
$2^6 \mod 5 = 4$
The sequence 2, 4, 3, 1 repeats with period 4. It will repeat forever — never ending, never changing.
This isn't a coincidence. It's a consequence of arithmetic that wraps around like a clock — that's called modular arithmetic. That repeating period (the number 4 here) is exactly what Shor's algorithm is designed to find — and finding it efficiently is what makes it exponentially faster than any classical computer.
Finding the period of $a^k \bmod N$ classically takes exponential time for large $N$ — a problem so hard it underpins RSA encryption. Shor's algorithm finds that period in polynomial time using quantum interference: the periodic structure in mod arithmetic becomes a repeating pattern of complex phases (roots of unity), and the Quantum Fourier Transform amplifies exactly those phases into a measurable signal. This lesson gives you both mathematical tools Shor needs.
The clock idea — numbers that wrap around and repeat.
What "mod" means — start with the intuition
Think of a clock with $N$ positions, labeled 0 through $N-1$. You start at 0. You take $a$ steps forward. Where do you land? That's exactly what modular arithmetic computes. Formally, $a \bmod N$ is the remainder when $a$ is divided by $N$:
$$a \bmod N = r \quad \text{where} \quad a = q \cdot N + r, \quad 0 \le r < N$$
Here $q$ is the quotient — how many full loops you've completed — and $r$ is the remainder: how many extra steps you took after the last full loop. The remainder $r$ always falls between $0$ and $N-1$. It's your position on the clock.
Your phone shows 3:00. You add 15 hours. Does it show 18:00? No — it wraps to 6:00. That's $15 \bmod 12 = 3$, then $3 + 3 = 6$. Add 24 hours to anything and you're back where you started: $24 \bmod 12 = 0$. Modular arithmetic works exactly like this for any $N$: count forward, and when you hit $N$, wrap back to 0. Simple as that.
Try a few — the pattern will become instinct
$7 \bmod 3 = 1$ — because $7 = 2 \times 3 + \mathbf{1}$ (two full loops, 1 step left over)
$12 \bmod 5 = 2$ — because $12 = 2 \times 5 + \mathbf{2}$
$9 \bmod 4 = 1$ — because $9 = 2 \times 4 + \mathbf{1}$
$15 \bmod 15 = 0$ — because $15 = 1 \times 15 + \mathbf{0}$ (a perfect full loop!)
The key property — cycles are unavoidable
Here's what makes modular arithmetic so special for quantum computing: it always cycles. With only $N$ possible values (0 through $N-1$), any sequence must eventually repeat. Try starting at 0 and adding 3 each time, with $N = 7$:
$0, 3, 6, 2, 5, 1, 4, 0, 3, 6, 2, \ldots$
After 7 steps you're back at 0. The cycle has period 7. This isn't a coincidence — there are only $N$ possible remainders, so any sequence must repeat before taking more than $N$ steps. This guaranteed cycling is the core structure Shor's algorithm exploits.
Dots on a circle — the clock idea, dressed in complex numbers.
What is a root of unity?
A complex number $z$ is called an $N$-th root of unity if $z^N = 1$. There are exactly $N$ of them, and they all live on the unit circle (the circle of radius 1). The most important one is the primitive $N$-th root of unity:
$$\omega_N = e^{2\pi i / N}$$
Think of it as a rotation: $\omega_N$ starts at the point $(1, 0)$ on the complex plane and is rotated $\tfrac{2\pi}{N}$ radians counterclockwise — exactly one $N$-th of a full turn. Its powers step through $N$ equally spaced positions:
$$\omega_N^k = e^{2\pi i k / N}$$
These $N$ points divide the circle into $N$ equal arcs — like hour marks on a clock, but on the complex unit circle. Together they form the complete set of $N$-th roots of unity: every complex number $z$ satisfying $z^N = 1$.
Geometric picture
Picture a circular clock with $N$ marks. You start at 12 o'clock (that's $\omega_N^0 = 1$). Multiply by $\omega_N$: you rotate by $\tfrac{2\pi}{N}$ and land at the next mark ($\omega_N^1$). Multiply again and you advance one more notch ($\omega_N^2$). After $N$ multiplications you've gone all the way around — a full rotation of $2\pi$ — and you're back at the start. This is exactly the mod $N$ clock cycle, just drawn on the complex unit circle instead of a number line.
Proving it: $\omega_N^N = 1$ (step by step, no gaps)
$\omega_N^N = \left(e^{2\pi i / N}\right)^N$
$= e^{2\pi i / N \cdot N} = e^{2\pi i}$
$e^{2\pi i} = \cos(2\pi) + i\sin(2\pi) = 1 + 0 \cdot i = 1$
$$\boxed{\omega_N^N = 1}$$
After $N$ multiplications you've rotated a full $2\pi$ — back to where you started. Roots of unity are periodic with period $N$, exactly like mod $N$ arithmetic.
The beautiful connection — clock arithmetic and circle rotations are the same thing
Here's the insight that ties everything together. Both systems — mod $N$ arithmetic and $N$-th roots of unity — are the same abstract cycle, just written in different languages.
In mod $N$: add 1 to move forward; add $N$ to wrap back to start. In roots of unity: multiply by $\omega_N$ to move forward; multiply by $\omega_N^N = 1$ to wrap back. The dictionary between them is simple: the integer $k$ on the clock corresponds to the complex number $\omega_N^k$ on the circle.
The Quantum Fourier Transform encodes the integer $k$ as the phase $\omega_N^k = e^{2\pi ik/N}$. When the input has period $r$, only phases at multiples of $N/r$ add up constructively — everything else cancels. The periodic structure of mod $N$ arithmetic becomes an interference pattern in complex phases. The QFT reads that pattern by measuring which phases survived. This is Shor's algorithm in one sentence: turn period-finding into phase interference, then measure.
Micro practice — three conceptual checks
Mod $N$ is wrap-around arithmetic. The answer to $a \mod N$ is always between 0 and $N-1$. Think of a clock with $N$ positions: after reaching $N$, you return to 0. The value $a \mod N$ tells you your position after counting $a$ steps forward on that clock. Modular arithmetic is finite and cyclic — it never escapes the range $\{0, 1, \ldots, N-1\}$.
$9 = 2 \times 4 + 1$, so $9 \mod 4 = 1$. You can verify by counting on a 4-position clock: 0, 1, 2, 3, 0, 1, 2, 3, 0, 1 — the 9th step lands on 1. Alternatively: the remainder when 9 is divided by 4 is 1 (since $4 \times 2 = 8$ and $9 - 8 = 1$).
Each root $\omega_N^k = e^{2\pi ik/N}$ has angle $2\pi k / N$ on the unit circle. As $k$ steps from 0 to $N-1$, the angle steps by $2\pi/N$ each time — a constant increment. Equal angle increments mean equal arc lengths, which means equal spacing. This is because multiplication by $\omega_N$ is always the same rotation: the spacing is built into the definition of $\omega_N$ as a rotation of $2\pi/N$.
Three tools to make it real — feel the clock, see the circle, watch the cycle.
Start with Tool 1 to feel modular arithmetic with your own numbers. Then use Tool 2 to see how those numbers place points on a circle. Finally, use Tool 3 to watch multiplication by $\omega_N$ step through the cycle one position at a time.
Three ideas, now yours — the foundation Shor's algorithm stands on.
-
Modular arithmetic is wrap-around counting
$a \mod N$ is the remainder when $a$ is divided by $N$, always in the range $\{0, 1, \ldots, N-1\}$. Think of a clock with $N$ positions: you advance $a$ steps and read off where you land. Because there are only $N$ possible values, every sequence must eventually repeat — and that period is the key quantity quantum algorithms detect.
-
Roots of unity are $N$ equally spaced points on the unit circle
The primitive $N$-th root of unity is $\omega_N = e^{2\pi i/N}$. Its powers $\omega_N^k = e^{2\pi ik/N}$ for $k = 0, \ldots, N-1$ are $N$ equally spaced points on the unit circle separated by angle $2\pi/N$. After $N$ multiplications by $\omega_N$, you complete a full circle and return to 1: $\omega_N^N = e^{2\pi i} = 1$.
-
Both structures encode the same periodicity — and QFT exploits it
Mod $N$ arithmetic and the $N$-th roots of unity are two faces of the same cyclic structure of size $N$. The Quantum Fourier Transform encodes inputs as phases $\omega_N^k$. When the input has a period $r$, those phases interfere constructively only at multiples of $N/r$ — making the period visible as an interference peak. This is Shor's secret weapon.
You now understand why periodic structure matters.
The next step: how the Quantum Fourier Transform
uses these phases to extract that period at quantum speed.
- Nielsen, M. A. & Chuang, I. L. — Quantum Computation and Quantum Information, Cambridge, 2000. §5.2: Quantum Fourier Transform and periodicity. §5.3: Shor's algorithm and order finding.
- Preskill, J. — Lecture Notes for Physics 219/Computer Science 219, Caltech. Chapter 6: Quantum algorithms — order finding and modular exponentiation. Available online
- Shor, P. W. — "Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer," SIAM J. Comput. 26(5), 1997. The original paper. arXiv:quant-ph/9508027
- Artin, M. — Algebra, 2nd ed., Pearson, 2011. Chapter 11: Rings — Roots of unity, cyclotomic polynomials, and cyclic groups.