Exercises: Quantum Machine Learning Fundamentals
These need qiskit, pennylane, scikit-learn, and qiskit-ibm-runtime. Solutions to starred
exercises are in Answers to Selected Exercises.
A standing rule for this chapter: every quantum result must be accompanied by a tuned classical baseline on the same split, and a total shot count. Case Study 1 is what happens otherwise.
The input problem
32.1 ★ Encode a 4-, 8-, 16-, 32-, 64-, and 128-feature vector with StatePreparation and report
qubits and two-qubit gates for each.
32.2 ★ Confirm the gate counts match $N - \log_2 N - 1$ exactly.
32.3 ★ Transpile each to FakeSherbrooke and report the ecr count. Compute the survival
probability at Chapter 12's measured error rate.
32.4 ★★ How many features can you amplitude-encode before the loading circuit alone drops below 50% survival? Below 10%?
32.5 ★★ Compare angle and amplitude encoding for a 16-feature dataset: qubits, gates, depth, and survival. Which would you choose on a 127-qubit device, and why?
32.6 ★★ Compute the state preparations required to train on digits (1,797 samples) for 60 steps.
Multiply by the per-sample gate cost. What is the total gate count for one training run?
32.7 ★★★ The $\mathcal{O}(N)$ loading bound is information-theoretic: $N$ amplitudes need $N$ parameters specified. Construct a case where it does not apply — a state family preparable in $\mathcal{O}(\text{polylog}\,N)$ gates — and say what makes it special.
32.8 ★★★ Read about QRAM proposals. What would it take to build one, and what does Chapter 25's threshold imply about the error-correction overhead?
A classifier
32.9 ★ Load iris, restrict to two classes, split and standardize. Fit LogisticRegression, SVC,
and RandomForestClassifier. Report train time and test accuracy for each.
32.10 ★ Build a 4-qubit VQC with angle encoding and StronglyEntanglingLayers. Train it on the
exact simulator and report accuracy and wall-clock time.
32.11 ★ Compute the slowdown ratio. Why does it vary between runs?
32.12 ★★ The VQC reaches lower training accuracy than logistic regression while matching on test. What does that suggest, and is it a good sign or a bad one?
32.13 ★★ Find a dataset where the classical baselines do not reach 100%. Re-run both models. Does the comparison now carry information?
32.14 ★★★ Tune the classical baseline properly — grid search over SVC hyperparameters. Does the
quantum model still match?
32.15 ★★★ Construct a dataset where the VQC beats a tuned classical baseline. What structure did you have to build in, and does that structure occur naturally?
The training shot budget
32.16 ★ Compute $(2n+1) \times \text{samples} \times \text{steps} \times \text{shots}$ for the chapter's classifier. Convert to QPU hours and days.
32.17 ★ How does the budget scale with dataset size? With optimizer steps? With parameter count?
32.18 ★★ Recompute for the digits dataset (1,797 samples, 64 features, angle encoding on 64
qubits). How many QPU-years?
32.19 ★★ Chapter 24's VQE cost $3.9\times10^9$ shots for one molecule. Compare against this chapter's classifier and explain why they are the same order of magnitude.
32.20 ★★ Mini-batching reduces samples per step. Recompute the budget with batch size 8. Does it help enough to matter?
32.21 ★★★ Gradient-free optimizers (SPSA, COBYLA) avoid the $2n+1$ factor. Recompute the budget using SPSA's two evaluations per step. Does the saving survive the extra iterations SPSA needs?
Barren plateaus
32.22 ★ Measure gradient variance at 2, 4, 6, 8, and 10 qubits. Reproduce the collapse.
32.23 ★ Report the number of gradients excluded below $10^{-14}$. Why is that number so large, and what does Chapter 16 §16.6 say about not reporting it?
32.24 ★★ Which parameters have identically-zero gradients, and why? Trace one through the circuit.
32.25 ★★ Combine the two constraints: for each qubit count, compute shots-to-resolve from the measured gradient magnitude and multiply through the training budget.
32.26 ★★★ Does a problem-informed ansatz (Chapter 16 §16.6, Chapter 29 §29.2) reduce the collapse?
Compare StronglyEntanglingLayers against a linear-entanglement ansatz at the same sizes.
32.27 ★★★ Local cost functions are claimed to mitigate barren plateaus. Replace $\langle Z_0 \rangle$ with an average over single-qubit observables and re-measure the variance scaling.
Project
32.28 ★★ (Project Checkpoint) Build vqelab/qml.py with encoding_cost, EncodingCost,
state_preparations_per_run, training_shot_budget, ShotBudget, shots_to_resolve, and
compare_to_baseline. Write tests asserting:
EncodingCostreports qubits AND gates, andsummary()presents both.- The gate count matches $N-\log_2 N-1$ at every measured point.
- The gate count grows linearly in $N$.
- Loading 128 features alone survives with probability below 0.45.
- Angle encoding costs one qubit per feature and zero two-qubit gates.
- Amplitude encoding rejects a non-power-of-two; unknown schemes are rejected.
state_preparations_per_run(1797, 60) == 107_820.- The training budget for the chapter's classifier is 2,058,000,000 shots = 2.38 QPU-days.
- The budget is linear in samples, in steps, and roughly linear in parameters.
- The budget rejects non-positive inputs.
- A toy problem costs the same order as Chapter 24's molecule.
shots_to_resolveis $1/g^2$ and rejects zero.- The constraints multiply: 20 qubits exceeds $10^{12}$ shots for a 70-sample problem.
compare_to_baselinereturnsUNINFORMATIVE_TIEwhen the baseline is already perfect, and the reason names what to do instead.- The refusal fires even when the quantum model looks marginally better.
- A real comparison is reported normally, with the slowdown when timings are supplied.
perfect_thresholddefaults to 0.999 and is adjustable.GATE_ERRORandSHOT_RATEmatch the values earlier chapters measured.
Tests 2, 7, 8 and 14 are the ones this chapter exists to encode.
32.29 ★★★ Extend the module with feasibility_report(dataset, encoding, ansatz, backend)
producing a single verdict: can this experiment run on this device this year? Apply it to digits.
Going further
32.30 ★★ Read about dequantization (Ewin Tang's recommendation-systems result and successors). What class of quantum algorithms did it affect, and what was the common structure?
32.31 ★★★ Find a QML paper claiming an advantage. Apply §32.8's seven-question checklist. Which questions does the paper answer?
32.32 ★★★ The strongest surviving case for QML is quantum data. Sketch a concrete problem where the input is a quantum state rather than a classical vector, and say what changes in every calculation in this chapter.