Exercises: Multi-Qubit Programming

Predict before you run, still. Two-qubit circuits are small enough to trace completely by hand, and this is the last chapter where that is comfortably true — so build the habit here.

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. Exercises marked 🌐 use a fake backend or real hardware.


Part A — Warm-ups ⭐

4.1 † How many complex amplitudes describe a 3-qubit state? A 10-qubit state? Why does the count double with each qubit?

4.2 In Qiskit, does qubit 0 appear first or last in the tensor product? Which character of the bitstring '011' corresponds to qubit 0?

4.3 † Write out the CNOT truth table for cx(0, 1) using Qiskit's bitstring convention. Which input produces '11'?

4.4 State, in one sentence each, what makes a state a product state and what makes it entangled.

4.5 † How many CNOTs does a SWAP cost? How many does a Toffoli cost? Why do you care about the first number even though you will rarely write swap yourself?

4.6 Name the four Bell states and give the circuit for each. Which pairs are distinguishable by computational-basis measurement alone?


Part B — Predict, Then Run ⭐⭐

4.7 † Apply H to qubit 0 of a 2-qubit circuit and print Statevector(qc).data. Then do the same for qubit 1. Which array index lights up in each case? Explain, and state why a test built on a symmetric state would not catch a mix-up.

4.8 Build both cx(0, 1) and cx(1, 0) and print their matrices. Which one matches the matrix in a typical textbook? Explain the discrepancy without using the word "wrong."

4.9 † For the product state H(0), H(1) and for the Bell state, compute the reduced state of qubit 0 and report purity, entanglement entropy, and Bloch-vector length. Explain what a Bloch length of zero means physically.

4.10 Build all four Bell states. Show that computational-basis measurement cannot distinguish $|\Phi^+\rangle$ from $|\Phi^-\rangle$. Then append the inverse preparation (cx then h) and show that all four become deterministic and distinct.

4.11 † Verify three identities with Operator.equiv: (a) H(1) CX(0,1) H(1) equals CZ, (b) three CNOTs equal SWAP, (c) two CNOTs do not. What does (c) establish that (b) alone does not?

4.12 Transpile CX, CCX, C3X, and C4X into a ["cx", "u"] basis at optimization level 3 and tabulate CNOT count and depth. By what factor does the cost grow from two controls to four? Now state what this implies for an oracle that tests a 5-bit condition.

4.13 † Build the 3-qubit GHZ and W states. For each, discard qubit 2 and report the entropy of the remaining pair and the eigenvalues of its density matrix. Which state leaves its survivors entangled? Explain from the eigenvalues, not from the entropy.


Part C — Deeper ⭐⭐⭐

4.14 † Build two circuits with identical computational-basis counts: a Bell state, and a circuit that measures qubit 0 and classically flips qubit 1 to match. Then add H to both qubits before measuring and run again. Report all four count dictionaries. Explain precisely what the second basis reveals and why one basis is never enough.

4.15 Prove computationally that $\tfrac{1}{\sqrt2}(|00\rangle + |11\rangle)$ cannot be written as $|a\rangle \otimes |b\rangle$. (Set up the four equations for the amplitudes and show they are inconsistent — you can do this symbolically or by exhaustive numerical search over $a$ and $b$.)

4.16 † Write schmidt_rank(sv, cut) that returns the number of non-negligible singular values of the state reshaped across a bipartition. Verify that product states give 1 and Bell states give 2. Then apply it to the 3-qubit GHZ and W states across the cut $\{0\}$ vs $\{1,2\}$ — do they differ? What does that tell you about whether Schmidt rank alone classifies multipartite entanglement?

4.17 Build a 4-qubit GHZ state two ways: a linear chain of CNOTs (0→1→2→3) and a "star" from qubit 0 (0→1, 0→2, 0→3). Compare the circuit depth of each. Then transpile both for FakeSherbrooke and compare depth and two-qubit gate count. Which is better before transpilation, and which is better after? Explain.

4.18 † The chapter claims a product state of $n$ qubits needs only $2n$ numbers. Write a function that takes a statevector and, if it factorizes, returns the two single-qubit states — and otherwise reports that it does not. Test on H(0) H(1), a Bell state, and ry(0.7, 0) ry(1.3, 1).

4.19 Construct a 3-qubit state that is entangled across the cut $\{0\}$ vs $\{1,2\}$ but not across $\{1\}$ vs $\{0,2\}$. Verify both claims numerically. What does this show about the phrase "the state is entangled"?


Part D — Hardware 🌐 ⭐⭐

4.20 † 🌐 Using FakeSherbrooke with seed_transpiler=42 and seed_simulator=1234, run GHZ states for $n = 2$ through $8$ at optimization level 1, and report the fraction of shots landing on all-zeros or all-ones. At which $n$ does the curve break, and by how much?

4.21 🌐 Diagnose the break. Query the backend's target for the readout error of physical qubits 0–7 and the ECR error of the pairs along that chain. Identify the culprit and state its two error figures.

4.22 † 🌐 Rerun 4.20 at optimization level 3 and tabulate the layouts chosen. Report the $n=8$ fidelity under both levels. What is the ratio? Write one sentence you would put in a methods section about layout selection.

4.23 🌐 Run a 2-qubit Bell state on the worst connected pair you can find on FakeSherbrooke (search the target for the highest ECR error among operational pairs) and on the best. Report both error fractions. How large is the spread on a single chip?


Part E — Project ⭐⭐

4.24 † Implement the Chapter 4 🧱 Project Checkpoint: extend vqelab/circuits.py with two_qubit_ansatz(), entanglement_entropy(), and is_entangled().

4.25 Add reachable_entanglement(ansatz, n_samples) which samples random parameter values and returns the maximum entanglement entropy the ansatz achieves. Run it on your two-qubit ansatz. Why is this the first diagnostic to run when a VQE will not converge?

4.26 ⭐⭐⭐ Build a variant ansatz with the CNOT removed and run reachable_entanglement on it. Then build one with two entangling layers. Tabulate the reachable entanglement of all three. Write two sentences on what this measurement tells you about ansatz design — and one on what it does not tell you. (Hint for the last part: an ansatz that can reach entangled states is not necessarily one that can reach the state you need.)