|q⟩ Bad Qubits

intermediate · Programming · Quantum Walks

Checkpoint: Simulate a Quantum Walk

This checkpoint consolidates everything from Module 8. You will build a complete one-step discrete-time quantum walk from scratch: a coin toss followed by a coherent conditional shift. The result will show the walker's position spread in a single step — the hallmark of quantum ballistic transport.

Recap: the structure of one walk step

A discrete-time quantum walk on a line operates on a joint register

ψHcoinHpos.|\psi\rangle \in \mathcal{H}_{\text{coin}} \otimes \mathcal{H}_{\text{pos}}.

A single step U=S(HIpos)U = S \cdot (H \otimes I_{\text{pos}}) applies two operations in sequence.

Step 1 — Coin toss. The Hadamard gate acts on the coin qubit alone:

H0=12(0+1),H1=12(01).H|0\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle), \qquad H|1\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle - |1\rangle).

After the coin toss the walker is in a superposition of moving left and moving right. No position update has happened yet.

Step 2 — Conditional shift. The shift operator SS moves the position register left when the coin reads 0|0\rangle and right when it reads 1|1\rangle:

Sdx=dx+2d1modN.S\,|d\rangle\,|x\rangle = |d\rangle\,|x + 2d - 1 \bmod N\rangle.

For d=0d = 0 this decrements the position; for d=1d = 1 it increments. On a 4-node cycle (N=4N = 4, two position bits q1q2q_1 q_2) both operations are implemented with a Toffoli and a CNOT, each conditioned on the coin qubit q0q_0.

What the circuit does, step by step

Start with coin 0|0\rangle at position 1=011 = |01\rangle, so the initial joint state is 001|001\rangle.

After the Hadamard coin toss:

001HI12(001+101).|001\rangle \xrightarrow{H \otimes I} \tfrac{1}{\sqrt{2}}\bigl(|001\rangle + |101\rangle\bigr).

The 0|0\rangle branch still has position 01|01\rangle (will move left); the 1|1\rangle branch still has position 01|01\rangle (will move right).

The conditional decrement moves the 0|0\rangle branch from position 11 to position 00:

001decrement000.|0\rangle|01\rangle \xrightarrow{\text{decrement}} |0\rangle|00\rangle.

The conditional increment moves the 1|1\rangle branch from position 11 to position 22:

101increment110.|1\rangle|01\rangle \xrightarrow{\text{increment}} |1\rangle|10\rangle.

The final state before measurement is:

12(000+110)=12(000+110).\tfrac{1}{\sqrt{2}}\bigl(|0\rangle|00\rangle + |1\rangle|10\rangle\bigr) = \tfrac{1}{\sqrt{2}}\bigl(|000\rangle + |110\rangle\bigr).

Measuring all three qubits yields bitstring 000 (coin =0= 0, position =0= 0) and bitstring 110 (coin =1= 1, position =2= 2), each with probability 12\tfrac{1}{2}.

Encoding the shift as a controlled binary increment/decrement

For a 2-bit position register, a cyclic increment adds 1 modulo 4. The binary ripple-carry circuit is:

q1q1(q0q2),q2q2q0.q_1 \leftarrow q_1 \oplus (q_0 \land q_2), \qquad q_2 \leftarrow q_2 \oplus q_0.

Reading left to right: first propagate the carry into the high bit with CCX(q0,q2,q1)\text{CCX}(q_0, q_2, q_1), then flip the low bit with CX(q0,q2)\text{CX}(q_0, q_2). Both gates trigger only when q0=1q_0 = 1, so the operation is controlled on the coin.

A cyclic decrement reverses the carry order — first flip the low bit, then propagate the carry — and wraps the result in XX gates on the coin:

X(q0)    CX(q0,q2)    CCX(q0,q2,q1)    X(q0).X(q_0)\;\cdot\;\text{CX}(q_0, q_2)\;\cdot\;\text{CCX}(q_0, q_2, q_1)\;\cdot\;X(q_0).

Flipping the coin converts "active when q0=1q_0 = 1" into "active when q0=0q_0 = 0", which is precisely the left-move condition.

Try it

Complete the circuit: add the coin toss, the conditional decrement, and the conditional increment. Measure all qubits. The grader checks the output distribution — you should see equal probability on bitstrings 000 and 110 and zero probability everywhere else.

Run your code to see the quantum state.

After pressing Run, open the Probabilities tab. Two equal bars at 000 and 110 confirm that the walker has spread from position 1 to positions 0 and 2 in a single coherent step. This coherent superposition is what enables the quantum walk's ballistic spreading over many steps — a dramatic speedup over the diffusive (σt\sigma \propto \sqrt{t}) spreading of its classical counterpart.

Sign in on the full site to ask questions and join the discussion.