Exercises: Quantum Classifiers
These need pennylane and scikit-learn. Solutions to starred exercises are in
Answers to Selected Exercises.
A standing rule for this chapter: no accuracy comparison counts until it has been run on at least five independent datasets and splits, with an error bar. Case Study 1 is what happens otherwise.
Choosing a dataset
33.1 ★ Fit four classical baselines to iris-binary and confirm all reach 1.0000. Explain why a quantum result on that dataset carries no information.
33.2 ★ Sweep make_moons noise from 0.05 to 0.45 and report the best classical accuracy at each.
At what noise level does the problem stop being trivially solved?
33.3 ★ Do the same for make_circles. Which dataset family leaves more room, and why?
33.4 ★★ LogisticRegression underperforms kNN by 0.18 on moons at noise 0.30. Explain in terms of the decision boundary, and say what that gap represents for a quantum model.
33.5 ★★ Construct a dataset where kNN performs badly and a linear model does well. What structure did you need?
33.6 ★★★ Design a dataset that would genuinely favour a quantum model. What property would it need, and is that property natural or constructed? (Compare Chapter 32 §32.6's separations.)
Data re-uploading
33.7 ★ Implement the single-qubit re-uploading circuit. Confirm it has 3 parameters per layer.
33.8 ★ Train it on moons at noise 0.30 with 2, 4, and 8 layers. Report accuracy for each.
33.9 ★★ Show that a single-qubit circuit without re-uploading — encode once, then rotate — cannot represent a non-linear boundary. Measure its accuracy.
33.10 ★★ Trace the composition: write out what a 2-layer re-uploading circuit computes as a function of $x$. Where does the non-linearity come from?
33.11 ★★ Does accuracy improve monotonically with layers? Plot it, and explain any plateau.
33.12 ★★★ Implement the original paper's version, which uses a richer per-layer encoding $R(\theta + w \odot x)$. Does it outperform the simple version measured here?
33.13 ★★★ How many layers does the universality theorem require for a given approximation error? Look it up, and compare with the number that actually helps here.
Replication
33.14 ★ Train the 1-qubit and 4-qubit models on random_state=0 and reproduce the 0.13 gap.
33.15 ★ Now run ten seeds. Report mean, std, min, and max for each model.
33.16 ★ Confirm that split 0 is the 1-qubit model's maximum and the 4-qubit model's minimum. What does that tell you?
33.17 ★★ Compute the paired difference across splits with a standard error. Is it significant at 2σ? At 1σ?
33.18 ★★ How many splits would you need to resolve a true difference of 0.02, given the observed spread? Compute it.
33.19 ★★ Compare re-splitting one dataset ten ways against generating ten datasets. Do the error bars differ? Which is the right protocol and why?
33.20 ★★★ Write compare_models(scores_a, scores_b, ...) that refuses fewer than five replicates,
and make the refusal message quote the measurement that motivates it.
The classical bar
33.21 ★ Compare the 1-qubit model against kNN across ten splits. Report the difference and its significance.
33.22 ★ Compare it against LogisticRegression. What does the result say about the universality theorem?
33.23 ★★ Tune kNN's k and SVC's hyperparameters by grid search. Does the gap widen?
33.24 ★★★ Give the quantum model more layers, more steps, and a better optimizer. How much of the 0.0626 gap to kNN can you close, and what does it cost?
The decision rule and the bills
33.25 ★ Compute $|\langle Z\rangle|$ for every test prediction and report the distribution. How many points sit within 0.05 of the boundary?
33.26 ★ Sweep the shot count from 100 to 100,000 and report mean accuracy, standard deviation, and label flips against the exact prediction.
33.27 ★★ Write shots_for_decision(margin_profile, tolerated_flips) deriving the shot count from
the margins. Compare its answer with the measured sweep.
33.28 ★★ Construct a dataset where many points sit near the boundary. How does the required shot count change?
33.29 ★★ Compute the inference cost for 1 million predictions at 100, 1,000, and 10,000 shots.
Then measure kNN.predict on 1 million points and state the ratio.
33.30 ★★★ For a model serving $P$ predictions per month over $M$ months, at what $P \cdot M$ does inference cost exceed training cost? Solve it symbolically and evaluate for this chapter's classifier.
Project
33.31 ★★ (Project Checkpoint) Build vqelab/classifiers.py with compare_models,
SplitComparison, margin_profile, shots_for_decision, training_cost, inference_cost, and
ClassifierReport. Write tests asserting:
compare_modelsreturnsINSUFFICIENT_REPLICATESfor a single split, and the reason quotes what replication showed.- The refusal holds for every count below the minimum.
- The single-split gap is 0.1313 and the ten-split difference is $+0.0202 \pm 0.0170$, not significant.
- Split 0 is the 1-qubit maximum AND the 4-qubit minimum.
- kNN beats the quantum model significantly, at more than five standard errors.
- The quantum model ties with LogisticRegression.
- Comparison requires paired lengths; the σ threshold is adjustable.
margin_profilereports the boundary distribution and rejects empty input.shots_for_decisiondemands more shots when points sit nearer the boundary.- Tolerating one flip substantially lowers the count.
- This chapter's data needs under 2,000 shots.
- Inference costs 27.8 QPU hours per million predictions at 1,000 shots, and 277.8 at 10,000.
training_costreproduces Chapter 32's 2,058,000,000 shots and 2.38 QPU-days.ClassifierReportis not deployable-claim-supported without an inference cost, and says so.- Nor without enough replicates.
- The cheap model really does use a quarter of the parameters and qubits.
Tests 1, 3, 4, 12 and 14 are the ones this chapter exists to encode.
33.32 ★★★ Extend the module with lifetime_cost(report, predictions_per_month, months) reporting
the training and inference totals side by side, and the month at which inference overtakes training.
Going further
33.33 ★★ Multi-class classification needs a different readout. Implement one-vs-rest and a multi-qubit measurement scheme, and compare on three-class iris.
33.34 ★★★ Class imbalance interacts with shot noise: the minority class has fewer samples and its decision margins matter more. Construct an imbalanced dataset and measure how the shot requirement changes.
33.35 ★★★ Apply Chapter 32 §32.8's seven-question checklist to this chapter's own results. Which questions does this chapter answer, and where is it weakest?