Cost Functions and Measurement
In quantum machine learning the loss is built from expectation values of observables, and its structure decides both what is learned and whether training is possible.
Loss from measurement
A quantum model's output is an expectation value
Local versus global cost functions
A crucial distinction is whether the observable acts on a few qubits or on all of them. A global cost, such as the projector onto the all-zeros state, compares the full state to a target and produces barren plateaus even at shallow depth. A local cost, built from single-qubit or few-qubit observables summed over the register, provably keeps gradients measurable at shallow depth. Preferring local costs is one of the strongest trainability levers; see trainability.
- Global cost: sensitive but flat, gradients vanish exponentially, hard to train.
- Local cost: less sensitive to global structure but trainable, the near-term default.
- Weighted mixtures: start local for gradient signal, add global terms as training progresses.
Choosing the observable
The observable encodes the task. For binary classification a single Pauli-Z read off one qubit gives a value in the range minus one to one, mapped to a class. For regression the same expectation is scaled to the target range. For state learning the loss is one minus the fidelity to a reference state. The observable must be efficiently measurable, which favors sums of local Pauli terms over dense operators.
# Local cost as a sum of single-qubit Z expectations (schematic)
def local_cost(theta, data, labels):
total = 0.0
for x, y in zip(data, labels):
pred = expval_Z(theta, x, qubit=0) # local observable
total += (pred - y) ** 2
return total / len(data)
Statistical reality
Every expectation is estimated from finite shots, so the cost and its gradient are random variables. The number of shots sets the precision, and near a barren plateau the required precision can exceed any feasible budget. The cost function's design and the shot allocation together determine whether the optimizer sees signal or noise.