|q⟩ Bad Qubits

intermediate · Programming · Multi-Qubit Registers & State Simulation

Checkpoint: Register Manipulation

Module 1 built up a toolkit for multi-qubit registers: allocating nn-qubit state vectors, applying single-qubit gates to individual qubits inside a register, encoding integers, building uniform superpositions with Hadamards, and controlling gates on multiple qubits. This checkpoint combines those ideas in a single state-preparation problem.

The target state

Your goal is to prepare

target=12(010+101).|\text{target}\rangle = \frac{1}{\sqrt{2}}\bigl(|010\rangle + |101\rangle\bigr).

Qubit 0 is the most significant bit, so 010|010\rangle means q0=0q_0 = 0, q1=1q_1 = 1, q2=0q_2 = 0, and 101|101\rangle means q0=1q_0 = 1, q1=0q_1 = 0, q2=1q_2 = 1. Each of the two terms has amplitude 1/21/\sqrt{2}, so the Born-rule probability of each outcome is 1/22=1/2|1/\sqrt{2}|^2 = 1/2.

Strategy: branch on the most significant qubit

A useful design pattern for state preparation is to create a superposition on one qubit and then fill in the remaining qubits conditioned on which branch you are in.

Step 1 — Hadamard on q0q_0. Apply HH to qubit 0. The register becomes

12(000+100).\frac{1}{\sqrt{2}}\bigl(|000\rangle + |100\rangle\bigr).

The two branches are now q0=0q_0 = 0 (top) and q0=1q_0 = 1 (bottom).

Step 2 — Handle the q0=0q_0 = 0 branch. In this branch, q1q_1 must become 11. We need a CNOT that fires when q0=0q_0 = 0 rather than when q0=1q_0 = 1. To invert the polarity of a control qubit, sandwich the controlled gate between two XX gates on the control:

X0CX01X0“CNOT controlled on q0=0.X_0 \cdot CX_{0 \to 1} \cdot X_0 \equiv \text{``CNOT controlled on } q_0 = 0\text{''}.

The first X0X_0 temporarily flips q0q_0, so the CXCX fires on the originally-zero branch. The second X0X_0 restores q0q_0. After this three-gate block the state is

12(010+100).\frac{1}{\sqrt{2}}\bigl(|010\rangle + |100\rangle\bigr).

Step 3 — Handle the q0=1q_0 = 1 branch. In this branch, q2q_2 must become 11. A standard CX(02)CX(0 \to 2) fires exactly when q0=1q_0 = 1, turning 100|100\rangle into 101|101\rangle:

12(010+101)=target.\frac{1}{\sqrt{2}}\bigl(|010\rangle + |101\rangle\bigr) = |\text{target}\rangle. \checkmark

The total circuit depth is five gates: H,X,CX,X,CXH, X, CX, X, CX. Each step uses gates from the standard universal set, and the control-on-zero trick is exactly the technique introduced in the multi-controlled gates lesson.

Try it

Build the circuit that prepares target|\text{target}\rangle. The grader checks the complete state vector against the reference solution, so every amplitude — including its sign and phase — must be correct. Leaving the circuit empty will fail.

Run your code to see the quantum state.

After a successful run you should see two amplitudes of 0.707=1/2\approx 0.707 = 1/\sqrt{2} at positions 010|010\rangle and 101|101\rangle, with all other amplitudes equal to zero.

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