Quantum Neural Network using PennyLane
Quantum Neural Network using PennyLane
A two-qubit QNN with data re-uploading for classifying "moons", with a numerical check of the parameter-shift ruleParameter-Shift Rule: The parameter-shift rule computes the exact derivative of an expectation value with respect to the angle of a gate e^-iθ P/2 from two evaluations of the same circuit at… and a plot of the decision boundary.
An educational research project on small data and quantum simulation: the results measure the model's behaviour in this setting only, and prove no quantum advantage.
The notebook and Google Colab
04-qnn-pennylane.ipynb — The notebook ran in full without errors (2026-11-20 08:11 UTC, 26 s on CPU)
The notebook has not yet been published in a public GitHub repository, so we do not give a direct Colab link. Download the notebook, then open it from Colab via "File → Upload notebook".
The notebook's twelve sections
- Project title
- Problem statement
- Installing the libraries
- Importing the libraries
- data loading
- Preprocessing
- Building the quantum model
- Training
- Evaluation
- Charts
- Results
- References
The notebook runs on a CPU without a GPU, and pins the versions it was tested with in its first cell.
Research problem
How does a small quantum model gain the ability to represent non-linear decision boundaries? And do parameter-shift gradients match automatic differentiation?
Objectives
- Build a QNN with all its components: encoding, parameters, entanglement, measurement, optimiser.
- Verify the parameter-shift rule.
- Train with mini-batches.
- Plot the model's output surface.
| Data | Moons (make_moons, 200 samples) — Generated programmatically in the notebook (scikit-learn or NumPy) No restrictions |
|---|---|
| Architecture | ($R_Y(x)$ ← StronglyEntanglingLayers) × 4 ← $\langle Z_0Z_1\rangle$ |
| Tools | PennyLane 0.45.1 · scikit-learn · Matplotlib |
| Evaluation metrics | Train/Test Accuracy · max |∇AD − ∇PS| · Parameter count · Training time |
Method
- Generate and split the data.
- Scale to [−π/2, π/2].
- Build the QNN and compare the two gradients.
- Train for 40 epochs with batches of 16.
- Evaluate and compare with a small MLP.
- Plot the decision surface.
Linked interactive experiment
References
- Maria Schuld; Ryan Sweke; Johannes Jakob Meyer (2021). Effect of data encoding on the expressive power of variational quantum-machine-learning models. Physical Review A 103, 032430 doi:10.1103/PhysRevA.103.032430
- Adrián Pérez-Salinas; Alba Cervera-Lierta; Elies Gil-Fuster; José I. Latorre (2020). Data re-uploading for a universal quantum classifier. Quantum 4, 226 doi:10.22331/q-2020-02-06-226
- Maria Schuld; Ville Bergholm; Christian Gogolin; Josh Izaac; Nathan Killoran (2019). Evaluating analytic gradients on quantum hardware. Physical Review A 99, 032331 doi:10.1103/PhysRevA.99.032331
- K. Mitarai; M. Negoro; M. Kitagawa; K. Fujii (2018). Quantum circuit learning. Phys. Rev. A 98:032309 doi:10.1103/PhysRevA.98.032309
