|q⟩ Bad Qubits

intermediate · Programming · Amplitude Amplification

The Amplification Operator

Grover's search and its generalization, amplitude amplification, both rely on two geometric primitives: a reflection about a "marked" subspace, and a reflection about the initial state. The composition of these two reflections — called the amplification operator or Grover iterate GG — rotates the quantum state toward the marked subspace.

The two reflections

Let ∣s⟩=H⊗n∣0n⟩|s\rangle = H^{\otimes n}|0^n\rangle be the uniform superposition over nn qubits, and let ∣τ⟩|\tau\rangle be the marked state (or subspace). Two unitary reflections are needed.

1. Oracle reflection UfU_f

The oracle flips the sign of every marked basis state and leaves unmarked states unchanged:

Uf∣x⟩={−∣x⟩if x is marked,∣x⟩otherwise.U_f |\mathbf{x}\rangle = \begin{cases} -|\mathbf{x}\rangle & \text{if } \mathbf{x} \text{ is marked,} \\ |\mathbf{x}\rangle & \text{otherwise.} \end{cases}

In matrix terms Uf=I−2∣τ⟩⟨τ∣U_f = I - 2|\tau\rangle\langle\tau|. The oracle is a reflection about the complement of the marked subspace.

2. Diffuser DD (reflection about ∣s⟩|s\rangle)

The diffuser reflects every amplitude about the mean, equivalently it reflects the state vector about ∣s⟩|s\rangle:

D=2∣s⟩⟨s∣−I.D = 2|s\rangle\langle s| - I.

For a uniform initial state on nn qubits we can implement DD with a standard circuit identity. Because ∣s⟩=H⊗n∣0n⟩|s\rangle = H^{\otimes n}|0^n\rangle, we can conjugate a reflection about ∣0n⟩|0^n\rangle by H⊗nH^{\otimes n}:

D=H⊗n(2∣0n⟩⟨0n∣−I)H⊗n.D = H^{\otimes n}\bigl(2|0^n\rangle\langle 0^n| - I\bigr)H^{\otimes n}.

The inner factor 2∣0n⟩⟨0n∣−I2|0^n\rangle\langle 0^n| - I flips the sign of every state except ∣0n⟩|0^n\rangle, which is the same as flipping the sign of ∣0n⟩|0^n\rangle alone (up to global phase):

2∣0n⟩⟨0n∣−I  =  −(I−2∣0n⟩⟨0n∣).2|0^n\rangle\langle 0^n| - I \;=\; -(I - 2|0^n\rangle\langle 0^n|).

For two qubits the circuit realization is straightforward. The sign of ∣00⟩|00\rangle is flipped by conjugating a CZCZ gate (which flips ∣11⟩|11\rangle) with XX on each qubit:

X⊗2  CZ  X⊗2.X^{\otimes 2}\;CZ\;X^{\otimes 2}.

The full two-qubit diffuser circuit is therefore

D=H⊗2  X⊗2  CZ  X⊗2  H⊗2.D = H^{\otimes 2}\;X^{\otimes 2}\;CZ\;X^{\otimes 2}\;H^{\otimes 2}.

The Grover iterate

Applying the oracle followed by the diffuser gives the Grover iterate G=D UfG = D\,U_f. Each application of GG rotates the state by 2θ2\theta toward the marked state, where sin⁡θ=M/N\sin\theta = \sqrt{M/N}, MM is the number of marked items, and N=2nN = 2^n. After k≈π4N/Mk \approx \tfrac{\pi}{4}\sqrt{N/M} iterations the amplitude on the marked state is maximized (Nielsen and Chuang, §6.1).

For N=4N = 4 and M=1M = 1, the initial angle satisfies sin⁡θ=1/4=1/2\sin\theta = \sqrt{1/4} = 1/2, so θ=π/6\theta = \pi/6. One application of GG rotates the state by 2θ=π/32\theta = \pi/3, placing the total angle at (2⋅1+1)θ=3θ=π/2(2\cdot 1+1)\theta = 3\theta = \pi/2 — exactly the marked state. A single iteration therefore suffices for certainty. For larger NN (where θ\theta is smaller) one iterate does not reach π/2\pi/2, so more are needed — up to the optimal k≈π4N/Mk \approx \tfrac{\pi}{4}\sqrt{N/M}.

Try it

Build the diffuser for two qubits. The grader checks the full unitary matrix, so the identity circuit will fail — you must implement the genuine reflection DD.

Run your code to see the quantum state.

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