|q⟩ Bad Qubits

intermediate · Programming · Algorithm Projects & Benchmarking

Project: Benchmark Grover

Grover's algorithm is not just a one-shot circuit — it is a loop. You apply an oracle-then-diffuser block kk times, and the success probability oscillates with kk. Knowing where the peak is and confirming it experimentally is the first step in benchmarking any quantum search.

How success probability varies with iterations

For a search space of N=2nN = 2^n items with exactly one marked item, the probability of finding that item after kk Grover iterations is

P(k)=sin2 ⁣ ⁣((2k+1)θ),θ=arcsin ⁣(1N).P(k) = \sin^2\!\!\left(\,(2k+1)\,\theta\,\right), \qquad \theta = \arcsin\!\left(\frac{1}{\sqrt{N}}\right).

The circuit rotates the initial equal superposition toward the marked state by 2θ2\theta each iteration. After k=π4θk^* = \bigl\lfloor \tfrac{\pi}{4\theta} \bigr\rfloor iterations the state is as close to the marked item as the rotation can take it.

For n=2n = 2 (four computational basis states, N=4N = 4):

θ=arcsin ⁣(12)=π6.\theta = \arcsin\!\left(\frac{1}{2}\right) = \frac{\pi}{6}.

The probability after kk iterations is therefore

P(k)=sin2 ⁣ ⁣((2k+1)π6).P(k) = \sin^2\!\!\left(\frac{(2k+1)\pi}{6}\right).

| kk | (2k+1)π/6(2k+1)\pi/6 | P(k)P(k) | |-----|--------------|--------| | 0 | π/6\pi/6 | 0.250.25 | | 1 | π/2\pi/2 | 1.001.00 | | 2 | 5π/65\pi/6 | 0.250.25 | | 3 | 7π/67\pi/6 | 0.250.25 |

The algorithm peaks at k=1k = 1 with certainty and then falls back — running too many iterations is worse than running too few. A benchmark must find this peak.

Circuit structure for two qubits

A single Grover iteration on n=2n = 2 consists of:

  1. Superposition layer — apply H2H^{\otimes 2} to start from the uniform state s=12(00+01+10+11)|s\rangle = \tfrac{1}{2}(|00\rangle + |01\rangle + |10\rangle + |11\rangle).
  2. Oracle — the phase-kickback oracle for the marked state 11|11\rangle is a controlled-ZZ: CZ11=11CZ\,|11\rangle = -|11\rangle, leaving all other states unchanged.
  3. Grover diffuser — reflection about s|s\rangle, implemented as H2X2CZX2H2H^{\otimes 2}\,X^{\otimes 2}\,CZ\,X^{\otimes 2}\,H^{\otimes 2}.

The diffuser sandwiches the "flip about zero" operation between two changes of basis. In the s|s\rangle basis "flip about zero" maps every amplitude aia_i to ai-a_i except asa_s, which stays positive, producing the mean-inversion that amplifies the oracle-marked state.

Try it

Complete the circuit by adding the oracle and the diffuser. The grader measures the output distribution — all probability should land on 11|11\rangle (the "11" outcome) after exactly one optimal iteration.

Run your code to see the quantum state.

After grading, try mentally replacing the one-iteration structure with two iterations: the table above predicts P(2)=0.25P(2) = 0.25, identical to the zero-iteration baseline. That is the overshoot phenomenon every Grover benchmark must guard against.

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