|q⟩ Bad Qubits

beginner · Programming · Building & Reading Quantum Circuits

Debugging a Circuit

Even a one-gate mistake in a quantum circuit produces a completely wrong state. Learning to read the statevector output and trace it back to a gate error is one of the core debugging skills in quantum programming.

The target: the Bell state

The simplest entangled two-qubit state is the Bell state

Φ+=12(00+11).|\Phi^+\rangle = \tfrac{1}{\sqrt{2}}\bigl(|00\rangle + |11\rangle\bigr).

The standard circuit that produces it is:

  1. Apply HH to qubit 0 — this puts qubit 0 into the equal superposition 12(0+1)\tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle).
  2. Apply a CNOT with qubit 0 as control and qubit 1 as target.

After step 1 the two-qubit state is 12(0+1)0=12(00+10)\tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle)\otimes|0\rangle = \tfrac{1}{\sqrt{2}}(|00\rangle + |10\rangle). The CNOT flips qubit 1 whenever qubit 0 is 1|1\rangle, turning 1011|10\rangle \to |11\rangle while leaving 00|00\rangle unchanged:

CNOT[12(00+10)]=12(00+11)=Φ+.\text{CNOT}\bigl[\tfrac{1}{\sqrt{2}}(|00\rangle + |10\rangle)\bigr] = \tfrac{1}{\sqrt{2}}(|00\rangle + |11\rangle) = |\Phi^+\rangle. \checkmark

Both amplitudes are 12\tfrac{1}{\sqrt{2}}, and the probabilities are each 12\tfrac{1}{2}.

What a gate bug looks like

Suppose someone replaces HH with XX — the bit-flip gate. Then step 1 gives X0=1X|0\rangle = |1\rangle (a definite 1|1\rangle, not a superposition), so the two-qubit state before the CNOT is 10|10\rangle. The CNOT flips qubit 1 because the control is 1|1\rangle, leaving 11|11\rangle. The result is a product state, not a Bell state:

Buggy output:11(no superposition, no entanglement).\text{Buggy output}: |11\rangle \quad \text{(no superposition, no entanglement)}.

Reading the statevector immediately reveals this: a single amplitude of 1 on the 11|11\rangle component, instead of two equal amplitudes on 00|00\rangle and 11|11\rangle. The fix is obvious once you see it — the wrong gate is on qubit 0.

Try it

The starter code has the bug described above. Run it, read the statevector, identify the wrong gate, and replace it so the circuit produces Φ+|\Phi^+\rangle.

Run your code to see the quantum state.

After your fix, the Statevector panel should show two equal amplitudes 0.707=12\approx 0.707 = \tfrac{1}{\sqrt{2}} on 00|00\rangle and 11|11\rangle, with all other components zero. That is the signature of the Bell state.

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