← Quantum Store · Guides · Lessons · Glossary · Packages · About · Türkçe
Quantum machine learning and hardware course · Lesson 2 / 5 · 12 min
The variational classifier: training a circuit like a neural network
A parameterised quantum circuit can learn to classify data by adjusting its angles. Compute gradients with the parameter-shift rule and train a single-qubit classifier.
The idea of a quantum neural network
A variational classifier has three parts: an encoding circuit that loads the data, a parameterised circuit whose angles are trained, and a measurement. The measured expectation value, for example ⟨Z⟩, is the model’s output: positive means one class, negative the other.
Training is the same loop as in VQE: the quantum computer computes the output and a classical optimiser changes the angles to reduce the error. That is why these models are sometimes called quantum neural networks.
The parameter-shift rule
Adjusting the angles needs the derivative of the output with respect to an angle. On a quantum computer the derivative cannot be measured directly, but for rotation gates such as rx, ry and rz there is a neat rule: the derivative is half the difference of the two values measured with the angle shifted up and down by π/2. This is called the parameter-shift rule.
Below, ⟨Z⟩ = cos θ for ry(θ). The rule gives exactly −sin θ, the derivative of cos θ. The same rule is then used to train a single-qubit model that splits six data points into two classes.
import numpy as np
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorEstimator
from qiskit.quantum_info import SparsePauliOp
estimator = StatevectorEstimator()
Z = SparsePauliOp('Z')
def model(x, theta):
qc = QuantumCircuit(1)
qc.ry(x, 0) # load the data as an angle
qc.ry(theta, 0) # the trained parameter
return float(estimator.run([(qc, Z)]).result()[0].data.evs)
def shift_grad(x, theta): # the parameter-shift rule
return (model(x, theta + np.pi / 2) - model(x, theta - np.pi / 2)) / 2
print(round(shift_grad(0, 0.8), 4), round(-np.sin(0.8), 4)) # -0.7174 -0.7174
X = np.array([0.2, 0.5, 0.9, 2.3, 2.7, 3.0])
y = np.array([1, 1, 1, -1, -1, -1])
theta = 2.0
accuracy = lambda t: round(float(np.mean([np.sign(model(x, t)) == label for x, label in zip(X, y)])), 2)
print('before', accuracy(theta))
for _ in range(30):
grad = np.mean([2 * (model(x, theta) - label) * shift_grad(x, theta) for x, label in zip(X, y)])
theta -= 0.3 * grad
print('after', accuracy(theta))
# before 0.33
# after 1.0
Difficulties
In large, randomly initialised parameterised circuits, gradients shrink exponentially with the number of qubits; McClean and colleagues showed this “barren plateau” problem in 2018. If the gradient is too close to zero, training cannot make progress.
Also, the circuit must be run many times for every gradient. So far variational classifiers have not been shown to beat good classical models on real data; researchers are looking for the kinds of data where an advantage is possible.
Try it yourself
A single H gives the same result as ry(π/2): ⟨Z⟩ = 0, so the model is completely undecided between the two classes. When a classifier is trained, its angles are adjusted to push ⟨Z⟩ towards +1 or −1 for each data point. Remove the H: ⟨Z⟩ = +1, a definite “first class” decision.
Open the circuit in your browserThe circuit playground is in Turkish. Button guide: “1000 kez ölç” = measure 1000 times, “Durum vektörünü göster” = show the state vector, “Bu devrenin Qiskit kodu” = this circuit’s Qiskit code, “Geri al” = undo, “Temizle” = clear. Click a gate in the circuit to remove it.
Key points
- Variational classifier: data encoding + parameterised circuit + measurement; the output is the sign of ⟨Z⟩.
- The parameter-shift rule gives the exact derivative by measuring with the angle shifted by ±π/2.
- The training loop combines a quantum and a classical computer, as in VQE.
- Barren plateaus and measurement cost make large models hard to train.
Test yourself
What is the parameter-shift rule used to compute?
- The number of qubits
- The depth of the circuit
- The derivative (gradient) of the output with respect to an angle
Show the answer
The derivative (gradient) of the output with respect to an angle. For rotation gates, half the difference of the two values measured with the angle shifted up and down by π/2 gives the exact derivative.
← Loading data into qubits: basis, angle and amplitude encoding · Quantum kernels: comparing data with quantum states →