Skip to content

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.

The gates you have met so far are fixed. A rotation gate has an angle you can choose:

RY(θ)=(cos⁡θ2−sin⁡θ2sin⁡θ2cos⁡θ2),RY(θ)∣0⟩=cos⁡θ2 ∣0⟩+sin⁡θ2 ∣1⟩.R_Y(\theta) = \begin{pmatrix}\cos\frac\theta2 & -\sin\frac\theta2\\[2pt] \sin\frac\theta2 & \cos\frac\theta2\end{pmatrix}, \qquad R_Y(\theta)\ket{0} = \cos\tfrac\theta2\,\ket{0} + \sin\tfrac\theta2\,\ket{1}.

At θ=0\theta = 0 it does nothing, at θ=π\theta = \pi it takes ∣0⟩\ket{0} to ∣1⟩\ket{1}, and in between it sweeps smoothly through superpositions.

A model must output a number. A single measurement gives only a bit, so we use an average instead. Score the outcome 0 as +1+1 and the outcome 1 as −1-1, run the circuit many times, and average the scores. The result is called the expectation value of ZZ, written ⟨Z⟩\langle Z\rangle:

⟨Z⟩=P(0)−P(1).\langle Z\rangle = P(0) - P(1).

For the state RY(θ)∣0⟩R_Y(\theta)\ket{0} the Born rule gives

⟨Z⟩=cos⁡2θ2−sin⁡2θ2=cos⁡θ.\langle Z\rangle = \cos^2\tfrac\theta2 - \sin^2\tfrac\theta2 = \cos\theta.

So this one-gate circuit computes the function θ↦cos⁡θ\theta \mapsto \cos\theta, a smooth function of its parameter with values between −1-1 and +1+1.

A variational classifier has three stages:

  1. Encode the input xx into a quantum state, for example with RY(x)R_Y(x).
  2. Apply trainable gates with parameters θ\theta.
  3. 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 xx is cos⁡(x+θ)\cos(x + \theta). There are four training points, two labelled +1+1 and two labelled −1-1, and the loss is the average squared error. First move the slider yourself and try to get all four right.

SimulatorTrain a one-parameter circuit

|0⟩ ── RY(x) ── RY(θ) ── measure Z

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:

dEdθ=12[E ⁣(θ+π2)−E ⁣(θ−π2)],\frac{dE}{d\theta} = \frac12\left[E\!\left(\theta + \tfrac\pi2\right) - E\!\left(\theta - \tfrac\pi2\right)\right],

where E(θ)E(\theta) is the expectation value. You can verify it for E=cos⁡(x+θ)E = \cos(x + \theta): the right-hand side is −sin⁡(x+θ)-\sin(x + \theta). 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.

Check

A circuit has 20 trainable angles. How many circuit evaluations does the parameter-shift rule need to get the full gradient at one point?

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.

RecallAnswer from memory, then check
LessonFinish this lesson