Simulate a quantum walk in code
A short NumPy program that reproduces the lab’s numbers for the Hadamard walk, what its output should be, and the mistakes that give a lopsided or non-normalised walk.
8 min read · reviewed December 2026
The idea in code
A quantum walk on a line needs one array of complex numbers with a row per position and a column per coin state. One step applies the 2 × 2 coin to every row, then shifts the left-coin column by one place one way and the right-coin column by one place the other. After t steps, the chance at each site is the sum of the squared sizes of its two amplitudes.
import numpy as np
def hadamard_walk(steps):
W = 2 * steps + 3
off = steps + 1
psi = np.zeros((W, 2), dtype=complex) # psi[x, c]; coin 0 steps left, coin 1 steps right
psi[off] = [1 / np.sqrt(2), 1j / np.sqrt(2)] # balanced start
H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
for _ in range(steps):
psi = psi @ H.T # coin on every site
new = np.zeros_like(psi)
new[:-1, 0] = psi[1:, 0] # coin 0: amplitude at x moves to x-1
new[1:, 1] = psi[:-1, 1] # coin 1: amplitude at x moves to x+1
psi = new
xs = np.arange(W) - off
p = (np.abs(psi) ** 2).sum(axis=1)
return xs, p
xs, p = hadamard_walk(100)
mean = (p * xs).sum()
sigma = np.sqrt((p * (xs - mean) ** 2).sum())
print("total chance:", round(float(p.sum()), 6))
print("mean:", round(float(mean), 6), "sigma:", round(float(sigma), 3), "sigma/t:", round(float(sigma / 100), 4))
print("classical sigma:", np.sqrt(100))
print("largest bar at:", int(xs[p.argmax()]))This script was run before publishing. Output: total chance 1.0; mean 0.0 (printed as -0.0); sigma 54.124; sigma/t 0.5412; classical sigma 10.0; largest bar at -68. The lab shows the same 0.5412 and the largest bars at -68 and +68 for 100 steps.
Reading the output
- Total chance 1.0 is the check that the walk is unitary. If it drifts from 1, the coin matrix or the shift is wrong.
- A mean of 0 means the balanced start did its job. Change the start to [1, 0] and the mean moves to about −29, matching the lab’s |L⟩ start.
- σ = 54.12 for 100 steps against 10 for a classical walk is the ballistic spread in one line.
- The largest bar sits at −68, since the argmax returns the first of the two equal peaks at ±68.
Common mistakes
- Shifting the wrong way: if both columns move in the same direction, the walker has a constant drift, and the distribution is a rigid translation of a one-sided pattern. The walker must move left for coin 0 and right for coin 1.
- Forgetting the imaginary unit in the start state: the walk then comes out lopsided with the Hadamard coin even though you wanted a symmetric start.
- Shifting before applying the coin: the first step then does nothing interesting, and the pattern is one step behind the lab’s.
- Using np.roll on the whole array: this wraps around, so the walker re-enters on the other side. That is fine for a ring and wrong for a line unless the array is big enough that nothing reaches the edges.
- Not copying the array: updating psi in place while reading from it mixes old and new amplitudes. Build a new array each step.
On a gate-based computer
A walk on a line of 2^k sites can be written as a circuit: the position is a k-qubit register, the coin is one qubit, the coin operation is a Hadamard, and the shift is an increment or decrement of the position register controlled by the coin. Increment circuits use a ladder of multi-controlled NOT gates, and the cost of each step grows with the register size. I have not run such a circuit for this page, so check the current documentation of your quantum software for how to build increments. The NumPy version above is the right place to start, and any circuit should be compared with it.
Check your walk against exact numbers. A wrong shift or a missing i is easy to make and easy to see in the distribution.
Try it
Questions
Why does the code use a width of 2 × steps + 3?
So the walker can reach t sites either way with room to spare, and the array edges are never touched.
Why print the total chance?
It should be exactly 1. It is the fastest check that every step is unitary.
Does this scale?
Linearly in the number of steps and sites, which is fine for hundreds of steps. For a graph with exponentially many vertices you would need a circuit on a quantum computer.
Read next
Sources and further reading
- Y. Aharonov, L. Davidovich and N. Zagury, “Quantum random walks” (1993)
- E. Farhi and S. Gutmann, “Quantum computation and decision trees” (1998)
- J. Kempe, “Quantum random walks: an introductory overview” (2003)
- N. Konno, work on limit distributions of the Hadamard walk (about 2002)
- A. Childs et al., “Exponential algorithmic speedup by quantum walk” (2003)
- N. Shenvi, J. Kempe and K. B. Whaley, “A quantum random walk search algorithm” (2003)
- A. Childs, “Universal computation by quantum walk” (2009)
Check the original sources for exact citations.