|q⟩ Bad Qubits

intermediate · Programming · Quantum Machine Learning (Intro)

Cost Functions and Gradients

Training a variational quantum circuit means picking parameters (gate angles θ\boldsymbol{\theta}) so that a cost function C(θ)C(\boldsymbol{\theta}) is minimised. The classical optimizer that adjusts those angles relies on gradients — derivatives C/θj\partial C / \partial \theta_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 OO in the parametric state ψ(θ)|\psi(\boldsymbol{\theta})\rangle:

C(θ)=O(θ)  =  ψ(θ)Oψ(θ).C(\boldsymbol{\theta}) = \langle O \rangle(\boldsymbol{\theta}) \;=\; \langle \psi(\boldsymbol{\theta}) | O | \psi(\boldsymbol{\theta}) \rangle.

Because CC 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\nabla_{\boldsymbol{\theta}} C.

A concrete one-parameter example

Consider the simplest non-trivial case: one qubit, one parameter. Apply Ry(θ)R_y(\theta) to 0|0\rangle:

ψ(θ)=Ry(θ)0=cos ⁣θ20+sin ⁣θ21.|\psi(\theta)\rangle = R_y(\theta)|0\rangle = \cos\!\tfrac{\theta}{2}|0\rangle + \sin\!\tfrac{\theta}{2}|1\rangle.

Measure in the ZZ basis. The expectation value of Z=0011Z = |0\rangle\langle 0| - |1\rangle\langle 1| is

Z(θ)=cos2 ⁣θ2sin2 ⁣θ2=cosθ.\langle Z \rangle(\theta) = \cos^2\!\tfrac{\theta}{2} - \sin^2\!\tfrac{\theta}{2} = \cos\theta.

The gradient with respect to θ\theta follows directly from calculus:

dZdθ=sinθ.\frac{d\langle Z \rangle}{d\theta} = -\sin\theta.

At θ=π/3\theta = \pi/3 we get Z=cos(π/3)=1/2\langle Z \rangle = \cos(\pi/3) = 1/2 and dZ/dθ=sin(π/3)=3/20.866d\langle Z\rangle/d\theta = -\sin(\pi/3) = -\sqrt{3}/2 \approx -0.866.

The loss landscape

Because every gate angle enters via a sine or cosine, C(θ)C(\boldsymbol{\theta}) 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 yy:

L(θ)=(O(θ)y)2.\mathcal{L}(\boldsymbol{\theta}) = \bigl(\langle O \rangle(\boldsymbol{\theta}) - y\bigr)^2.

By the chain rule:

Lθj=2(Oy)Oθj.\frac{\partial \mathcal{L}}{\partial \theta_j} = 2\bigl(\langle O \rangle - y\bigr) \cdot \frac{\partial \langle O \rangle}{\partial \theta_j}.

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 η\eta the parameters are updated as

θjθjηCθj.\theta_j \leftarrow \theta_j - \eta \,\frac{\partial C}{\partial \theta_j}.

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θ\langle Z \rangle = \cos\theta and its gradient dZ/dθ=sinθd\langle Z\rangle/d\theta = -\sin\theta at θ=π/3\theta = \pi/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.