Computing Library › Quantum Ml
Quantum Ml

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 = of some observable M, estimated by averaging measurement outcomes over many shots. The cost function combines these expectations with the labels or the objective. Common choices include mean squared error between and a target, cross-entropy after mapping expectations to probabilities, and fidelity-based losses that measure closeness to a target state.

Local versus global cost functions

Kronos motion — training from sim

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.

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.

python
# 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.