A quantum neural network classifier in Qiskit Machine Learning
Training a small quantum-classical classifier on synthetic data.
Goal: Optimising the parameters of a quantum circuit by gradient.
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def circuit(w):
qml.RX(w[0], wires=0); qml.RY(w[1], wires=1)
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliZ(1))
w = np.array([0.1, 0.2], requires_grad=True)
opt = qml.GradientDescentOptimizer(stepsize=0.4)
print(f"Before: {circuit(w):.4f} | gradient: {np.round(qml.grad(circuit)(w), 4)}")
for _ in range(40):
w = opt.step(circuit, w)
print(f"After 40 steps: {circuit(w):.4f}")
We define a quantum node (QNode) on the default.qubit simulator and compute the expectation value of PauliZ. PennyLane computes the gradient with the parameter-shift rule, and we use an optimizer to drive the expectation value towards -1.
Before: 0.9752 | gradient: [-0.0978 -0.1977] After 40 steps: -1.0000
This example was actually run and tested on 21 November 2026 on a CPU — pennylane 0.45.1, python 3.13.18
Training a small quantum-classical classifier on synthetic data.
Sections
No section has this name. Press “Search” to search all site content.
Favourites
My results & certificates
My academy progressMy quiz resultsKids’ progressAcademy certificatesQuiz certificatesStart any lesson or quiz and it will show up here so you can get back to it quickly.
Settings
I am the AI Frameworks & Libraries Assistant. Describe your project or ask me about a library, a comparison or a code example, and I will answer from the encyclopedia's database with links and sources.
Answers from the encyclopedia's database only, without a language model and without sending your question to any third party.
The assistant can make mistakes, so check the attached sources.