Training a variational quantum circuit means picking parameters (gate angles
θ) so that a cost functionC(θ) is minimised. The
classical optimizer that adjusts those angles relies on gradients — derivatives
∂C/∂θj that indicate which direction to move each angle.
Expectation values as cost functions
The natural cost function in quantum computing is the expectation value of an observable O
in the parametric state ∣ψ(θ)⟩:
C(θ)=⟨O⟩(θ)=⟨ψ(θ)∣O∣ψ(θ)⟩.
Because C is real-valued and bounded, it is a well-defined landscape over the parameter space.
The classical optimizer navigates that landscape by computing (or estimating) the gradient
∇θC.
A concrete one-parameter example
Consider the simplest non-trivial case: one qubit, one parameter. Apply Ry(θ) to
∣0⟩:
∣ψ(θ)⟩=Ry(θ)∣0⟩=cos2θ∣0⟩+sin2θ∣1⟩.
Measure in the Z basis. The expectation value of Z=∣0⟩⟨0∣−∣1⟩⟨1∣ is
⟨Z⟩(θ)=cos22θ−sin22θ=cosθ.
The gradient with respect to θ follows directly from calculus:
dθd⟨Z⟩=−sinθ.
At θ=π/3 we get ⟨Z⟩=cos(π/3)=1/2 and
d⟨Z⟩/dθ=−sin(π/3)=−3/2≈−0.866.
The loss landscape
Because every gate angle enters via a sine or cosine, C(θ) is a
trigonometric polynomial — smooth and periodic. This guarantees that gradients exist
everywhere, which is what makes gradient descent well-defined for variational quantum algorithms.
A common supervised loss is the mean squared error between the expected output and a target
label y:
L(θ)=(⟨O⟩(θ)−y)2.
By the chain rule:
∂θj∂L=2(⟨O⟩−y)⋅∂θj∂⟨O⟩.
The first factor comes from the loss shape; the second factor is the gradient of the expectation
value, which the parameter-shift rule provides.
Gradient descent update
Given a learning rate η the parameters are updated as
θj←θj−η∂θj∂C.
Iterating this loop — run circuit, estimate gradient, update angles — is the variational
quantum training loop. The outer loop is classical; only the expectation-value evaluations
require quantum resources.
Try it
Compute the expectation value ⟨Z⟩=cosθ and its gradient
d⟨Z⟩/dθ=−sinθ at θ=π/3. Return an object
{ E, grad } with both numbers. The grader checks that both values are numerically correct.
Run your code to see the quantum state.
Sign in on the full site to ask questions and join the discussion.