|q⟩ Bad Qubits

advanced · Programming · Quantum Machine Learning (Advanced)

Variational Classifiers

A variational quantum classifier (VQC) is a parameterized circuit trained to assign labels. It combines the feature map of the kernel lesson with a trainable ansatz, then reads out a prediction from a measured expectation value. Mitarai et al. (2018) and Havlíček et al. (2019) introduced this "quantum circuit learning" template.

Anatomy of the model

The circuit has three stages applied to 0n|0\rangle^{\otimes n}:

ψ(x,θ)=W(θ)UΦ(x)0n.|\psi(\boldsymbol{x},\boldsymbol{\theta})\rangle = W(\boldsymbol{\theta})\,U_\Phi(\boldsymbol{x})\,|0\rangle^{\otimes n}.
  1. Encoding UΦ(x)U_\Phi(\boldsymbol{x}) — the fixed feature map that loads the data (e.g. angle encoding RY(2xi)R_Y(2x_i)).
  2. Ansatz W(θ)W(\boldsymbol{\theta}) — trainable rotations (and entangling gates for more than one qubit) whose parameters are learned.
  3. Readout — measure an observable, typically Z\langle Z\rangle on one qubit, and map its sign to a class.

The prediction is

f(x)=ψ(x,θ)Zψ(x,θ)[1,1],f(\boldsymbol{x}) = \langle \psi(\boldsymbol{x},\boldsymbol{\theta})|\,Z\,|\psi(\boldsymbol{x},\boldsymbol{\theta})\rangle \in [-1, 1],

and the class is signf(x)\operatorname{sign} f(\boldsymbol{x}).

A one-qubit example

The smallest non-trivial VQC uses a single qubit. With a scalar feature xx encoded by RY(2x)R_Y(2x) and a two-angle trainable block RY(θ0)RZ(θ1)R_Y(\theta_0)\,R_Z(\theta_1), the prepared state is

ψ=RY(θ0)RZ(θ1)RY(2x)0.|\psi\rangle = R_Y(\theta_0)\,R_Z(\theta_1)\,R_Y(2x)\,|0\rangle.

Even this tiny model can separate simple 1-D data: training rotates the Bloch vector so that points of one class land in the upper hemisphere (Z>0\langle Z\rangle > 0) and the other class in the lower hemisphere.

Training the parameters

The classical loop minimizes a loss — for binary labels y{1,+1}y\in\{-1,+1\}, a common choice is the squared error (f(x)y)2\bigl(f(\boldsymbol{x}) - y\bigr)^2 averaged over the dataset. Gradients of ff with respect to each θk\theta_k come from the parameter-shift rule (Module 7): for a rotation generated by a Pauli,

fθk=12[f(θk+π2)f(θkπ2)].\frac{\partial f}{\partial \theta_k} = \tfrac12\Bigl[f(\theta_k + \tfrac{\pi}{2}) - f(\theta_k - \tfrac{\pi}{2})\Bigr].

These exact analytic gradients feed an ordinary optimizer (gradient descent, Adam) that updates θ\boldsymbol{\theta}.

Try it

Prepare the state of a one-qubit variational classifier for the input x=0.5x = 0.5 with the trained parameters θ0=0.8\theta_0 = 0.8 (an RYR_Y) and θ1=0.3\theta_1 = 0.3 (an RZR_Z). Apply the gates in the order encoding, then RZ(θ1)R_Z(\theta_1), then RY(θ0)R_Y(\theta_0) — i.e. build

RY(0.8)RZ(0.3)RY(20.5)0.R_Y(0.8)\,R_Z(0.3)\,R_Y(2\cdot 0.5)\,|0\rangle.

The grader checks the resulting state vector.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.