Exercises: Quantum Kernels
These need pennylane and scikit-learn. Solutions to starred exercises are in
Answers to Selected Exercises.
A standing rule for this chapter: every kernel result needs a feature-map sweep, ten splits, a tuned classical baseline, and a Gram-matrix budget. Anything less describes one arbitrary feature map.
The kernel
34.1 ★ Implement kernel_circuit(x1, x2) as feature map then adjoint feature map, reading
$P(|0\dots0\rangle)$. Confirm $K(x,x) = 1$ exactly and that $K$ is symmetric.
34.2 ★ Explain in two sentences why $P(|0\dots0\rangle)$ equals $|\langle\phi(x)|\phi(x')\rangle|^2$.
34.3 ★ Build a 201×201 Gram matrix by looping, then by batching. Report both times and the speedup.
34.4 ★★ Write validate_gram(K) checking unit diagonal, symmetry, and positive semi-definiteness.
Construct a matrix that fails each check and confirm an SVM fits it anyway.
34.5 ★★ Why must a kernel matrix be positive semi-definite? What does the SVM dual do if it is not?
34.6 ★★★ Implement the kernel by explicit state simulation — build $|\phi(x)\rangle$, take the overlap — and confirm it agrees with the circuit version. At what qubit count does the explicit version become impractical?
Convexity
34.7 ★ Fit SVC(kernel='precomputed') on your Gram matrix. Report the fit time and the number of
support vectors.
34.8 ★ Fit it ten times with different random seeds. Confirm the result is identical every time, and contrast with Chapter 33's variational model.
34.9 ★★ List every hyperparameter Chapter 33's model had that this one does not, and every one this one has that Chapter 33's did not.
34.10 ★★★ The SVM dual is convex. Write down the optimization problem and identify what makes it convex. What property of the Gram matrix is that convexity relying on?
The feature map
34.11 ★ Sweep qubit count, repetitions, and input scaling. Reproduce the 0.6364 → 0.8500 range.
34.12 ★ Which single hyperparameter mattered most? Compare its effect against the difference between the quantum kernel and the RBF kernel.
34.13 ★★ Try a feature map with no entangling gates. Is the resulting kernel still "quantum" in any meaningful sense, and can it be computed classically?
34.14 ★★ Implement Qiskit's ZZFeatureMap and compare against your hand-rolled version.
34.15 ★★★ Design a feature map for a dataset you choose, motivated by the data's structure rather than by convention. Does motivated design beat sweeping?
Concentration
34.16 ★ Measure the mean off-diagonal kernel value at 2, 4, 6, 8, and 10 qubits with one independent feature per qubit. Report the collapse factor.
34.17 ★ Repeat with two features spread redundantly. Compare the two curves.
34.18 ★★ Explain, in terms of information rather than qubits, why the two regimes differ.
34.19 ★★ Sweep qubit count on the moons data and report train accuracy, test accuracy, and support vector count. Does concentration cause memorization here?
34.20 ★★ Construct a dataset and feature map where it does. What did you have to change?
34.21 ★★ Write concentration_verdict(K, n_qubits, n_features) with no default for n_features,
and justify the absence in the docstring.
34.22 ★★★ At what ratio of features to qubits does concentration become severe? Sweep it and find the boundary.
34.23 ★★★ Kernel concentration and barren plateaus are both exponential losses of signal. Are they the same phenomenon? Argue both sides, with a measurement.
The bill
34.24 ★ Compute the unique Gram entries for $n = 201$, $2{,}010$, and $20{,}100$. Confirm the quadratic.
34.25 ★ For each qubit count in your concentration sweep, compute shots per entry as $1/K^2$ and the total Gram budget in QPU-days.
34.26 ★★ At what qubit count does the Gram matrix exceed one QPU-day for 201 samples? For 2,010?
34.27 ★★ Add the inference cost: $m$ test points against $s$ support vectors. How does it compare to Chapter 33 §33.6's figure?
34.28 ★★★ Compare the total lifetime cost of the kernel method against Chapter 33's variational model as a function of dataset size. Where do the curves cross?
Project
34.29 ★★ (Project Checkpoint) Build vqelab/kernels.py with batched_gram, validate_gram,
off_diagonal, concentration, concentration_verdict, gram_entries, gram_shot_budget, and
KernelReport. Write tests asserting:
- A real kernel matrix passes all three validity checks.
- A non-unit diagonal, an asymmetry, and a non-PSD matrix are each rejected, with the reason named.
validate_gramrejects a non-square input.- Concentration collapses several hundred fold with independent features.
- The redundant regime concentrates more than fifteen times more slowly.
concentration_verdicthas no default forn_features, and the same Gram matrix yields different verdicts under different regimes.- Test accuracy is non-decreasing in qubit count on the redundant encoding — the measurement that refuted the prediction.
- The overloaded regime is flagged separately.
gram_entriesuses symmetry: 20,301 unique for $n = 201$.- The Gram matrix at ten qubits costs about 38.6 QPU-days.
- Ten times the data is a hundred times the entries.
gram_shot_budgetrejects an impossible kernel value.- One feature map is not a method comparison, and the summary quotes the 0.6364 swing.
- Nor is a single split, nor a missing baseline.
- A complete report states the verdict and the Gram bill.
- This chapter's own result loses to the classical baseline.
Tests 2, 6, 7, 10 and 13 are the ones this chapter exists to encode.
34.30 ★★★ Extend the module with feature_map_sweep(circuits, X, y, splits) that runs a sweep and
returns the best map with the sweep attached, so KernelReport can be constructed honestly.
Going further
34.31 ★★ Read Havlíček et al. (2019). It introduces both the variational classifier and the kernel method. How does the paper frame the choice between them, and does this chapter's measurement agree?
34.32 ★★★ Read Liu, Arunachalam and Temme (2021) on the proven kernel separation. What exactly is the learning problem, what assumption does the hardness rest on, and how natural is the data?
34.33 ★★★ Schuld's "Supervised quantum machine learning models are kernel methods" argues that every variational model is secretly a kernel method. If that is right, what does it imply about Chapter 33's results and this chapter's tie between the two approaches?