Exercises: Chapter 8 — Programming Quantum Computers with Qiskit: Your First Quantum Program on a Real Quantum Processor

Exercise 8.1 — Your First Hardware Run

Create a 3-qubit GHZ state circuit, simulate it locally, then run it on the least-busy available IBM Quantum processor. Compare the simulated and hardware results. Compute the fidelity of the hardware result relative to the ideal GHZ state.

Starter code:

from qiskit import QuantumCircuit
qc = QuantumCircuit(3)
qc.h(0)
qc.cx(0, 1)
qc.cx(1, 2)
qc.measure_all()

Exercise 8.2 — Noise Model Calibration

Using the calibration data from backend.properties(), construct a noise model that matches a real backend. Run a 4-qubit QFT circuit with this noise model and compare the output distribution to the ideal case. Quantify the degradation using the total variation distance:

$$\text{TVD}(p, q) = \frac{1}{2} \sum_x |p(x) - q(x)|$$

Exercise 8.3 — Parameterized Circuit Optimization

Implement a parameterized circuit with 6 parameters that prepares an arbitrary 2-qubit state. Use the Statevector class to compute the fidelity between your circuit's output and a target state. Write a simple gradient-free optimizer (e.g., random search or COBYLA) to find parameters that maximize fidelity.

Exercise 8.4 — Transpilation Deep Dive

Take a 5-qubit circuit with 20 random gates and transpile it for three different backends with different coupling maps (e.g., heavy-hex, linear, all-to-all). Compare the resulting depths, SWAP counts, and estimated success probabilities. Which topology is most efficient for your circuit?

Exercise 8.5 — Readout Error Mitigation

Implement a simple readout error mitigation scheme: characterize the measurement error matrix for 2 qubits by preparing and measuring all four computational basis states, then apply the inverse of the error matrix to correct raw measurement counts. Test it on a Bell state prepared on a noisy simulator.

Exercise 8.6 — Custom Gate Definition

(a) Define a custom gate that implements the square root of SWAP ($\sqrt{\text{SWAP}}$). The unitary matrix is:

$$\sqrt{\text{SWAP}} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \frac{1}{2}(1+i) & \frac{1}{2}(1-i) & 0 \\ 0 & \frac{1}{2}(1-i) & \frac{1}{2}(1+i) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}$$

(b) Use this gate to create an entangling circuit and verify that $\sqrt{\text{SWAP}} \cdot \sqrt{\text{SWAP}} = \text{SWAP}$.

Exercise 8.7 — Zero-Noise Extrapolation

Implement a manual ZNE scheme: (a) Run a GHZ state circuit at noise levels 1×, 2×, and 3× by repeating gate sequences. (b) Use linear extrapolation to estimate the zero-noise result. (c) Compare with the ideal result.

Exercise 8.8 — Circuit Equivalence Testing

Prove the following circuit identities by constructing both sides in Qiskit and comparing their unitaries:

(a) $HXH = Z$ (b) Two CNOTs in a row cancel: $\text{CNOT}^2 = I$ (c) $\text{SWAP} = \text{CNOT}_{0\to1} \cdot \text{CNOT}_{1\to0} \cdot \text{CNOT}_{0\to1}$

Exercise 8.9 — State Tomography

Implement quantum state tomography for a 2-qubit circuit. Measure in the XX, YY, ZZ, IX, XI, IY, YI, ZI, and IZ bases to reconstruct the density matrix. Compare the reconstructed state with the ideal state using fidelity.

Exercise 8.10 — Backend Comparison

Run the same 4-qubit random circuit on three different IBM backends (e.g., ibm_brisbane, ibm_osaka, ibm_kyoto). Compare the fidelity, execution time, and queue time for each. Which backend gives the best results for your circuit?