Training a Quantum Classifier
Previous lessons introduced parameterized quantum circuits and feature maps. Now we ask: how do we actually train such a circuit to separate classes?
The classification task
Suppose we have a dataset of labeled points, each a real number . Points with belong to class 0; points with belong to class 1. We want a quantum circuit — parameterized by both the data and trainable weights — whose measurement on a designated label qubit tells us the class.
A natural circuit structure for one data point on two qubits is:
Qubit 0 is the data qubit (encoding ) and qubit 1 is the label qubit (read out after the circuit). The trainable parameter sits on the label qubit after the entangling gate.
How training works
Starting from , the circuit produces the state
After the on qubit 0:
The CNOT flips qubit 1 only when qubit 0 is , entangling the two qubits:
Finally, on qubit 1 rotates the component of each term. The probability of measuring qubit 1 in — the "class 1" output — becomes a function of both and .
Training means choosing so that this probability is small for class-0 points and large for class-1 points. A classical optimizer (gradient descent, COBYLA, etc.) adjusts by evaluating the circuit repeatedly and minimizing a cost function that penalizes misclassified points. Gradients can be computed exactly with the parameter-shift rule (covered in the next lessons).
The trained classifier
For our toy dataset, suppose the optimizer has already run and found the optimal weight . At this setting the probability of measuring on the label qubit grows monotonically with over , correctly ranking class-0 inputs below the classification threshold and class-1 inputs above it.
We can verify this concretely for the boundary point :
After the encoding step: .
After on qubit 1, the amplitude on picks up a rotation by . Using the general result , substituting and gives — exactly at the 50% threshold, as expected for the class boundary: inputs with produce (class 0) and inputs with produce (class 1).
Try it
Implement the trained circuit: encode the data point on qubit 0 with , entangle the two qubits with a CNOT, then apply the trained weight on the label qubit. The grader checks the exact statevector produced by your circuit.
Sign in on the full site to ask questions and join the discussion.