Simon’s algorithm step by step: finding a hidden period

Each run of the circuit yields a random string orthogonal to the secret. Collect about n of them, solve a small linear system, and the secret appears. Classically this takes exponentially many queries.

11 min read · reviewed December 2026

The problem

A function f takes n bits to n bits. It is promised that there is a hidden non-zero string s such that f(x) = f(y) exactly when y = x or y = x ⊕ s. So f is two-to-one, and the two inputs that share an output differ by s. Find s.

Classically the only way in is to find a collision: two inputs with the same output. Their XOR is s. With N = 2^n possible inputs, a random search needs on the order of 2^(n/2) queries before a collision appears, the same effect as the birthday problem.

A hidden two-to-one function with s = 101 (seed 4).
xf(x)x ⊕ s
000110101
001011100
010101111
011100110
100011001
101110000
110100011
111101010
Possible readings y: chance and the check y·s mod 2.
yy·s mod 2Chance
000025.0%
00110.0%
010025.0%
01110.0%
10010.0%
101025.0%
11010.0%
111025.0%

The circuit

  • Two registers of n qubits, both starting at 0.
  • Hadamard on the first register: an equal superposition of all x.
  • Call the oracle, which writes f(x) into the second register: Σ_x |x⟩|f(x)⟩.
  • Hadamard on the first register again, then measure it. (The second register can be measured or ignored. The lab ignores it, and the result for the first register is the same.)

Why every reading is orthogonal to s

Imagine measuring the second register first and seeing some value v. The first register collapses to the two inputs that give v, which are x and x ⊕ s: the state (|x⟩ + |x ⊕ s⟩)/√2. The Hadamard layer turns this into a sum over y of ((−1)^(x·y) + (−1)^((x⊕s)·y)) |y⟩, up to normalisation. The two terms cancel when s·y is 1, and add when s·y is 0.

So only strings y with s·y = 0 (mod 2) can appear, and each of the N/2 such strings appears with chance 2/N. The right-hand table above shows this for n = 3 and s = 101: four readings are possible, each with 25 percent, and the other four never occur.

only y with y·s ≡ 0 (mod 2) can be read, each with chance 2/N

Solving for s

Each reading y gives one linear equation over the two-element field: y·s = 0. A reading of all zeros carries no information. You need n − 1 independent equations to pin s down to one non-zero value. Gaussian elimination over bits does it, and the lab shows the shortcut that is feasible at small n: check every non-zero candidate against the equations and list the survivors.

How many runs are needed? The readings are uniform on an (n − 1)-dimensional space, so the expected number of runs until they span it is the sum over i from 0 to n − 2 of 1/(1 − 2^(i − (n − 1))). That sum is about n − 1 plus a small constant, and the Query counts tab prints it. This page’s own arithmetic: for n = 4 it is about 4.48 runs.

Why this matters historically

Simon’s 1994 result is an exponential separation in query count between quantum and bounded-error classical algorithms, for a problem with a clean mathematical form. Its structure, a period hidden in a function and found by Fourier-style interference, directly inspired Shor’s algorithm, which finds periods of a different function and so factors integers.

Try it: set n = 4, press Run once five times, and watch the candidate list shrink. Then press New hidden function and see that the pattern of readings stays orthogonal to the same s.

Try it

Questions

Why is the classical cost about 2^(n/2)?

You need to find two inputs that collide. With k queries you have about k²/2 pairs, and each pair collides with chance about 1/2^n, so you need k near 2^(n/2).

Does Simon’s algorithm break encryption?

No. It solves an artificial problem with an oracle. It matters because the same idea, period finding, powers Shor’s algorithm for factoring.

Read next

Sources and further reading

Check the original sources for exact citations.