
A Quantum Walk in Qiskit: Let a Qubit Steer Motion Around a Ring
A coin qubit directs a walker around a four-position cycle. Build three coherent steps in Qiskit and see how interference changes the outcome.
Imagine a walker moving around a small ring. At each step, a coin chooses clockwise or counterclockwise. A classical simulation flips an ordinary coin and tracks probabilities. A quantum walk uses a qubit as that coin, and the two possible directions stay in a coherent superposition. The paths are amplitudes, so they can reinforce or cancel when they meet again.
That difference matters. In a classical random walk, probabilities from alternative paths add. In a quantum walk, complex amplitudes combine first and probabilities are measured afterward. Relative phase can change which positions are likely to appear.
A four-position cycle
We will use one coin qubit and a two-qubit position register. The position is p = p0 + 2p1, so it can represent 0, 1, 2, or 3. The ring wraps around modulo 4. In each round, a Hadamard gate acts on the coin. If the coin is |0>, the walker moves one position forward. If it is |1>, the walker moves one position backward. The coin is not measured between rounds, so the phase information is kept.
Build three coherent steps
In the code, q0 is the coin, q1 stores the low position bit p0, and q2 stores the high bit p1. The first controlled block increments the position when the coin is |0>. We temporarily flip the coin so Qiskit gates with a control on |1> can implement that condition. The second block decrements the position when the coin is |1>.
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
coin, p0, p1 = 0, 1, 2
qc = QuantumCircuit(3, 2)
for _ in range(3):
qc.h(coin)
# coin = 0: increment p modulo 4
qc.x(coin)
qc.ccx(coin, p0, p1)
qc.cx(coin, p0)
qc.x(coin)
# coin = 1: decrement p modulo 4
qc.cx(coin, p0)
qc.ccx(coin, p0, p1)
qc.measure(p0, 0)
qc.measure(p1, 1)
counts = AerSimulator().run(qc, shots=1024).result().get_counts()
print(counts)
On an ideal simulator, the only measured position is 1, so the result is {'01': 1024}. Qiskit prints classical bit c1 on the left and c0 on the right. Since c0 holds p0 and c1 holds p1, 01 means p1 = 0 and p0 = 1. Real hardware noise can add other outcomes.
Why does this differ from tossing a classical coin? After three classical steps around the same four-position ring, the walker has equal probability of landing at 1 or 3. In the quantum circuit, amplitudes from different paths carry phase. For this setup, paths to position 3 cancel and those leading to position 1 reinforce.
This tiny example demonstrates interference, not a general speedup. Quantum walks can be useful algorithmic tools on carefully chosen graphs, but any advantage depends on the problem and the cost of implementing its walk. The key lesson is that a quantum walker does not merely sample a direction. It keeps path amplitudes coherent long enough for them to interfere.