Exercises: Qubit Manipulation in Code

Everything here runs on a simulator — free, instant, exact. Predict before you run. For single-qubit circuits you can compute the answer by hand in under a minute, and the discipline of doing so is what makes Chapter 26 possible.

Difficulty: ⭐ warm-up · ⭐⭐ standard · ⭐⭐⭐ deeper. Solutions: worked answers to the daggered (†) and odd-numbered problems are in appendices/answers-to-selected.md; runnable code in code/exercise-solutions.py.


Part A — Warm-ups ⭐

3.1 † What state does a qubit start in, and how many complex numbers describe a single-qubit state? How many real free parameters does it have after accounting for normalization and global phase?

3.2 Give the $2\times2$ matrix for X, H, and Z from memory. Which one has a negative entry, and what does that entry do?

3.3 † What is the difference between Statevector(qc).data and Statevector(qc).probabilities(), and which one contains strictly more information? Give a concrete pair of states that share probabilities and differ in data.

3.4 Why can Statevector never be used on real hardware? Name the physical principle.

3.5 † Name the six cardinal points of the Bloch sphere and give a one- or two-gate circuit that prepares each from $|0\rangle$.

3.6 Why must every quantum gate be unitary? What would go wrong if a gate were not?


Part B — Predict, Then Run ⭐⭐

3.7 † For each of X, Y, Z, H, S, T, S†, T†, predict whether applying it to $|0\rangle$ changes the state, then verify with Statevector. Explain the pattern geometrically in one sentence.

3.8 Prepare $|+\rangle$ and $|-\rangle$. Show that (a) their probabilities are identical, (b) they are not equal as Statevectors, and (c) equiv also reports them as different. What kind of phase distinguishes them?

3.9 † Compute the Bloch vector of all six cardinal states and verify that each has length exactly 1. What lies inside the sphere, and which chapter covers it?

3.10 Compute $T^n$ for $n = 1$ through $8$ using Operator. At which $n$ do you get $Z$? At which do you get the identity? Explain the name "$\pi/8$ gate" and why it is confusing.

3.11 † Run the two circuits H H and H Z H, each with 1,024 shots and a fixed seed. Report both count dictionaries. Then compute Operator for the second circuit and identify which named gate it equals. Explain in two sentences why the answer is deterministic when the intermediate state was a 50/50 superposition.

3.12 Compute H X H, H Y H, and H Z H. Identify each result. What geometric operation does conjugating by H perform on the Bloch sphere? (Careful with the Y case — check whether you are comparing exactly or up to global phase, and say which is happening.)

3.13 † Sweep $R_y(\theta)$ over $\theta \in [0, 2\pi]$ and tabulate $P(0)$ against $\cos^2(\theta/2)$. Then examine the state at $\theta = 2\pi$: is it $|0\rangle$? Explain the discrepancy between the probability and the state.


Part C — Deeper ⭐⭐⭐

3.14 † Build a circuit implementing $-I$ (four gates suffice). Confirm with Operator and show that its state is physically indistinguishable from $|0\rangle$. Then control it on a qubit in superposition, apply H to the control, and measure the control. What happens, and what is this phenomenon called? State the practical rule this implies about editing subcircuits.

3.15 † Transpile H, Y, S, and T into the basis ["rz", "sx", "x"]. For each, report the gate counts, the global phase, and the number of real pulses (recall that rz is virtual). Which of these four gates is free on hardware? What does that suggest about how to write circuits?

3.16 Find a sequence of at most four gates from $\{H, S, T, X, Z\}$ that prepares a state with Bloch vector approximately $(0.5, 0.5, 0.707)$. Verify numerically. (Hint: get the $z$ coordinate first, then rotate about $z$.)

3.17 † The general single-qubit gate is u(θ, φ, λ). Determine, empirically, the parameters that reproduce H, X, and S. Then state how many real parameters a general single-qubit unitary has, and reconcile that with the two free parameters of a state. Why the difference?

3.18 Write is_unitary(m) that checks whether a $2\times2$ complex matrix is a legal gate. Test it on X, H, a random matrix, and the non-unitary $\begin{pmatrix}1 & 1\\ 0 & 1\end{pmatrix}$. Then explain what physically goes wrong if you try to apply the last one.

3.19 † Two states are indistinguishable if no measurement can tell them apart. Prove computationally that $|\psi\rangle$ and $e^{i\phi}|\psi\rangle$ are indistinguishable for several $\phi$, by comparing probabilities in three different measurement bases (computational, X, and Y). Why is checking one basis insufficient as an argument?

3.20 Implement gate_from_bloch_rotation(axis, angle) that returns a circuit rotating the Bloch sphere by angle about a unit vector axis. Verify against rx, ry, and rz for the coordinate axes, then use it for the axis $(1,1,1)/\sqrt3$.


Part D — Project ⭐⭐

3.21 † Implement the Chapter 3 🧱 Project Checkpoint: vqelab/circuits.py with single_qubit_ansatz() and state_of(). Verify that $P(0) = \cos^2(\theta/2)$ across a sweep.

3.22 Extend state_of() to raise a clear, actionable error when the circuit contains measurement instructions — because a measured circuit has no statevector, and the default error message does not say so.

3.23 ⭐⭐⭐ Add assert_state(circuit, expected_label, **bindings) to circuits.py, which raises with a useful message on failure: the expected amplitudes, the actual amplitudes, and the fidelity between them. Compare up to global phase. Test it with a deliberately wrong expectation and read your own error message — if it does not tell you what went wrong, rewrite it. This is the first piece of the test infrastructure that Chapter 27 completes.