Exercises: Chapter 21 — Quantum Machine Learning — Variational Circuits, Quantum Kernels, and the Search for Quantum Advantage in ML

Exercise 21.1: Data Encoding Comparison

Implement three data encoding strategies (angle encoding, amplitude encoding, and ZZ feature map) for a 4-dimensional dataset. For each, build a QNN with the same variational ansatz and train on a binary classification task. Compare: (a) number of qubits required, (b) circuit depth, (c) training convergence speed, and (d) test accuracy. Discuss the trade-offs.

Exercise 21.2: Barren Plateau Detection

Implement a function that estimates the gradient variance for a given QNN architecture. For a hardware-efficient ansatz with $n = 2, 4, 6, 8, 10$ qubits and random parameters, compute $\text{Var}[\partial \mathcal{L} / \partial \theta_k]$ for 100 random parameter sets. Plot the variance vs. $n$ on a log scale. Does it decay exponentially? At what $n$ does the variance become smaller than $10^{-6}$?

Exercise 21.3: Quantum Kernel vs. Classical Kernel

For the moons dataset (Section 21.4.4), compare the performance of: (a) QSVM with ZZ feature map, (b) classical SVM with RBF kernel, (c) classical SVM with polynomial kernel (degree 3). Use 5-fold cross-validation to compute mean and standard deviation of test accuracy. Is the quantum kernel statistically significantly better than the classical kernels?

Exercise 21.4: Classical Shadows for Fidelity Estimation

Given the Bell state $|\Phi^+\rangle = (|00\rangle + |11\rangle)/\sqrt{2}$, use classical shadows to estimate the fidelity with itself (should be 1.0) and with the state $|\Psi^+\rangle = (|01\rangle + |10\rangle)/\sqrt{2}$ (should be 0.0). Plot the estimated fidelity vs. number of snapshots $T \in [10, 10000]$. How many snapshots are needed for 0.01 precision?

Exercise 21.5: QCNN for Phase Recognition

Implement a 4-qubit QCNN to classify states from two quantum phases: (a) the paramagnetic phase ($H = -\sum_i X_i$) and (b) the ferromagnetic phase ($H = -\sum_i Z_i Z_{i+1}$). Generate training data by preparing ground states of these Hamiltonians at various points in the phase diagram. Train the QCNN and evaluate its ability to identify the phase of unseen ground states.

Exercise 21.6: Parameter-Shift Rule Implementation

Implement the parameter-shift rule for a general parameterized quantum circuit. For a 3-qubit hardware-efficient ansatz with 2 layers, compute the full gradient vector $\nabla_{\boldsymbol{\theta}} \langle Z_0 \rangle$ at a random parameter point. Verify your result against numerical finite differences with step sizes $h = 10^{-3}, 10^{-5}, 10^{-7}$. At what step size does the finite difference approximation break down?

Exercise 21.7: Kernel Alignment Analysis

For the ZZ feature map with 2 features and 2 repetitions, compute the kernel matrix $K_{ij} = k(\mathbf{x}_i, \mathbf{x}_j)$ for a synthetic dataset with two well-separated clusters. Compute the kernel-target alignment $A(k, y)$. Repeat with a random unitary as the feature map. Compare the alignments. What does this tell you about when quantum kernels are useful?

Exercise 21.8: QCBM Training

Implement a 4-qubit Quantum Circuit Born Machine and train it to learn a bimodal distribution over $\{0, 1, 2, \ldots, 15\}$. Use the MMD loss with a Gaussian kernel. Plot the learned distribution vs. the target distribution at iterations 0, 50, 100, and 200. How many training iterations are needed for convergence?

Exercise 21.9: Data Re-Uploading Universal Approximation

Prove that a single qubit with data re-uploading can approximate any continuous function $f: [0, 2\pi] \to [-1, 1]$. Specifically, show that for any target function $f$ and $\epsilon > 0$, there exist parameters $\boldsymbol{\theta}$ such that $|f(x) - \langle Z \rangle_{U(x, \boldsymbol{\theta})}| < \epsilon$ when $U(x, \boldsymbol{\theta})$ consists of $L$ alternating data encoding and variational layers.

Exercise 21.10: Comparison of QML and Classical ML on Iris

Implement both a QSVM (with ZZ feature map) and a classical SVM (with RBF kernel) on the Iris dataset (4 features, 3 classes). Use stratified 5-fold cross-validation. Report mean accuracy, standard deviation, and training time. Is the quantum advantage statistically significant? Under what conditions would you expect the QSVM to outperform the classical SVM?

Exercise 21.11: Quantum Transfer Learning

Implement quantum transfer learning using a pre-trained classical model. (a) Train a classical neural network (e.g., a small CNN or MLP) on the MNIST dataset (or a subset) and freeze its weights. (b) Replace the final classification layer with a 4-qubit variational quantum circuit using angle encoding of the classical features. (c) Train only the quantum parameters on a binary classification subtask (e.g., distinguishing digits 0 and 1). (d) Compare test accuracy against: (i) the frozen classical model with a new classical classification layer of the same parameter count, (ii) a full quantum model trained from scratch. Does the quantum layer provide any advantage? Under what conditions might it?

Exercise 21.12: Kernel Alignment Calculation

For the dataset from Section 21.4.7 (four 2D points with labels $y \in \{+1, +1, -1, -1\}$), compute the kernel-target alignment for: (a) the ZZ feature map with reps=1, (b) the ZZ feature map with reps=3, (c) a feature map using only single-qubit rotations (no entangling gates). Plot the alignment as a function of reps for each feature map. How does entanglement affect alignment? At what point does increasing repetitions stop improving alignment?

Exercise 21.13: Classical Shadows — Convergence and Scaling

Implement the classical shadows protocol from Section 21.7. For an $n$-qubit GHZ state $|0...0\rangle + |1...1\rangle)/\sqrt{2}$: (a) Estimate $\langle Z_1 Z_2 \rangle$ using $T \in \{100, 500, 1000, 5000, 10000\}$ snapshots. Plot the absolute error vs. $T$ on a log-log scale. Verify the $O(1/\sqrt{T})$ scaling. (b) Estimate $\langle X_1 \rangle$ and $\langle Z_1 Z_2 ... Z_n \rangle$ for $n = 4, 6, 8$. How does the number of snapshots needed for precision $\epsilon = 0.01$ scale with $n$ for each observable? (c) Explain why the global observable $\langle Z^{\otimes n} \rangle$ requires exponentially more snapshots than local observables.