Variational circuits
Most quantum machine learning today is built on one idea: treat a quantum circuit as a model with adjustable parameters, and train it the way you would train a neural network. This lesson builds the smallest such model and trains it.
It assumes you know the material in the Quantum Information track up to single-qubit gates. Some familiarity with gradient descent helps.
Gates with a dial
Section titled “Gates with a dial”The gates you have met so far are fixed. A rotation gate has an angle you can choose:
At it does nothing, at it takes to , and in between it sweeps smoothly through superpositions.
A number out of a circuit
Section titled “A number out of a circuit”A model must output a number. A single measurement gives only a bit, so we use an average instead. Score the outcome 0 as and the outcome 1 as , run the circuit many times, and average the scores. The result is called the expectation value of , written :
For the state the Born rule gives
So this one-gate circuit computes the function , a smooth function of its parameter with values between and .
A circuit as a model
Section titled “A circuit as a model”A variational classifier has three stages:
- Encode the input into a quantum state, for example with .
- Apply trainable gates with parameters .
- Measure an expectation value and read it as the prediction.
Training happens in a loop between two machines. The quantum computer runs the circuit and reports expectation values. A classical computer compares them with the labels, computes a loss, and chooses new parameters. This is why these methods are called hybrid.
The simulator below is the smallest possible example. Two rotations about the same axis add up, so the circuit’s output for an input is . There are four training points, two labelled and two labelled , and the loss is the average squared error. First move the slider yourself and try to get all four right.
|0⟩ ── RY(x) ── RY(θ) ── measure Z
Gradients from the circuit itself
Section titled “Gradients from the circuit itself”Then press the gradient button a few times and watch the marker roll downhill. Where does the gradient come from? We cannot differentiate through hardware the way backpropagation differentiates through a network.
For rotation gates there is an exact formula, the parameter-shift rule:
where is the expectation value. You can verify it for : the right-hand side is . The rule holds for any circuit built from such rotations. Note that it is not a finite-difference approximation. The shifts are large and the result is exact.
The price is that each parameter needs two extra circuit evaluations, and each evaluation is itself an average over many runs.
A circuit has 20 trainable angles. How many circuit evaluations does the parameter-shift rule need to get the full gradient at one point?
Two shifted evaluations for each of the 20 parameters. Backpropagation gets all the gradients of a neural network for roughly the cost of one forward pass, so this is a real disadvantage at scale.
Does this beat classical machine learning?
Section titled “Does this beat classical machine learning?”Be careful with claims here. The model above is trivially easy to simulate, and so are many proposed quantum models. As of this writing there is no demonstration of a variational quantum model beating the best classical methods on a practical learning problem with ordinary data.
Three obstacles are well understood:
- Loading data is expensive. Getting a large classical dataset into a quantum state can cost as much as the computation it was meant to speed up.
- Gradients can vanish. For many families of large random circuits, the gradient shrinks exponentially with the number of qubits. This is the barren plateau problem.
- Classical simulation keeps improving. Several quantum models once thought hard have turned out to be simulable.
The more promising direction is learning from data that is quantum to begin with, such as states produced by a quantum sensor or simulator. The later lessons in this track go through the evidence on both sides.
Remember it
Section titled “Remember it”What is a variational quantum circuit?
A circuit whose gates have tunable angles. Its measured output depends on those angles, and a classical optimiser adjusts them.
What are the three stages of a variational classifier?
Encode the data into a state, apply the trainable gates, then measure an expectation value.
State the parameter-shift rule for a rotation gate with angle .
: an exact gradient from two more runs of the same circuit.
What is the expectation value of for the state ?
.