|q⟩ Bad Qubits

beginner · Programming · Building & Reading Quantum Circuits

Checkpoint: From Spec to Circuit

This checkpoint asks you to translate a written circuit specification into working code — exactly the skill you will use every time you implement a quantum algorithm from a textbook or paper description. Read the spec carefully, then build the circuit stage by stage.

The specification

The circuit operates on two qubits. Qubit 0 is the most significant bit, so the computational basis state q0q1|q_0 q_1\rangle is ordered with qubit 0 on the left.

Stage 1 — prepare: Apply the XX gate (bit-flip) to qubit 1. The two-qubit state starts as 00|00\rangle and becomes 01|01\rangle.

Stage 2 — superpose: Apply the Hadamard gate to qubit 0. Because H0=12(0+1)H|0\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle), the joint state becomes

H001=12(0+1)1=12(01+11).H_0 |01\rangle = \tfrac{1}{\sqrt{2}}\bigl(|0\rangle + |1\rangle\bigr)|1\rangle = \tfrac{1}{\sqrt{2}}\bigl(|01\rangle + |11\rangle\bigr).

Stage 3 — entangle: Apply a CNOT gate with control = qubit 0 and target = qubit 1. The CNOT flips the target when the control is 1|1\rangle and leaves it unchanged when the control is 0|0\rangle. Acting on the superposition:

CNOT0112(01+11)=12(01+10).\mathrm{CNOT}_{0 \to 1}\,\tfrac{1}{\sqrt{2}}\bigl(|01\rangle + |11\rangle\bigr) = \tfrac{1}{\sqrt{2}}\bigl(|01\rangle + |10\rangle\bigr).

The 01|01\rangle term is unchanged (control is 0), and 11|11\rangle becomes 10|10\rangle (control is 1, so target flips from 1 to 0). The resulting state is one of the four Bell states:

Ψ+=12(01+10).|\Psi^+\rangle = \tfrac{1}{\sqrt{2}}\bigl(|01\rangle + |10\rangle\bigr).

How to approach a spec

When translating any specification into a circuit, a reliable strategy is:

  1. Identify the stages. The spec breaks the work into named sections — read each one before writing any code.
  2. Trace the state. Write out the state vector after each stage (as done above) to verify your mental model before you code.
  3. Write gate calls in order. Each stage maps directly to one or more gate calls appended to the circuit object. The order of your calls must match the order in the spec.
  4. Check the output. After running, compare the simulator's state vector to the state you derived by hand.

This disciplined read-trace-code cycle scales to algorithms of any complexity — the same method works whether the spec has three stages or thirty.

Try it

Implement the three-stage circuit described in the spec. The grader compares the full state vector of your circuit with the reference output Ψ+|\Psi^+\rangle, so the order of your gate calls matters.

Run your code to see the quantum state.

After running, inspect the State Vector tab: the two non-zero amplitudes should each read 0.707=12\approx 0.707 = \tfrac{1}{\sqrt{2}}, corresponding to 01|01\rangle and 10|10\rangle.

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