Losses

Module name: qmlt.numerical.losses

Code author: Maria Schuld <maria@xanadu.ai>

A collection of loss functions for numpy.

Summary

trace_distance(rho, sigma) Trace distance \(\frac{1}{2}\tr \{ \sqrt{ (\rho - \sigma)^2} \}\) between quantum states \(\rho\) and \(\sigma\).
expectation(rho, operator) Expectation value \(\tr\{ \rho O\}\) of operator \(O\) with respect to the quantum state \(\rho\).
square_loss(outputs, targets) Mean squared loss \(0.5 \sum\limits_{m=1}^M |y^m - t^m|^2\) between outputs \(y^m\) and targets \(t^m\) for \(m = 1,...,M\).
_softmax(logits) Softmax function, turns a vector of real values into a vector of probabilities.
cross_entropy_with_softmax(outputs, targets) Cross-entropy loss that measures the probability error in discrete classification tasks (with mutually exclusive classes).

Code details

qmlt.numerical.losses.trace_distance(rho, sigma)[source]

Trace distance \(\frac{1}{2}\tr \{ \sqrt{ (\rho - \sigma)^2} \}\) between quantum states \(\rho\) and \(\sigma\).

Parameters:
  • rho (ndarray or list) – 2-dimensional square matrix representing the state \(\rho\).
  • sigma (ndarray or list) – 2-dimensional square matrix of the same dimensions and dtype as rho, representing the state \(\sigma\)
Returns:

Scalar trace distance.

Return type:

float

qmlt.numerical.losses.expectation(rho, operator)[source]

Expectation value \(\tr\{ \rho O\}\) of operator \(O\) with respect to the quantum state \(\rho\).

Parameters:
  • rho (ndarray or list) – 2-dimensional array representing the state \(\rho\).
  • operator (ndarray or list) – 2-dimensional array of the same dimensions and dtype as rho, representing the operator \(O\)
Returns:

Scalar expectation value.

Return type:

float

qmlt.numerical.losses.square_loss(outputs, targets)[source]

Mean squared loss \(0.5 \sum\limits_{m=1}^M |y^m - t^m|^2\) between outputs \(y^m\) and targets \(t^m\) for \(m = 1,...,M\).

Parameters:
  • outputs (ndarray or list) – array of dimension M x 1 containing the 1-dimensional outputs.
  • targets (ndarray or list) – array of the same dimension and type as outputs, containing the targets.
Returns:

Scalar mean squared loss.

Return type:

float

qmlt.numerical.losses._softmax(logits)[source]

Softmax function, turns a vector of real values into a vector of probabilities.

Parameters:logits (ndarray 1-d) – Real 1-d vector of model outputs
Returns:Vector of probabilities
Return type:ndarray
qmlt.numerical.losses.cross_entropy_with_softmax(outputs, targets)[source]

Cross-entropy loss that measures the probability error in discrete classification tasks (with mutually exclusive classes). Useful for one-hot-encoded vectors.

Parameters:
  • outputs (ndarray) – Real 2-dim array representing a batch of model outputs. Also called logits.
  • targets (ndarray) – Real 2-dim array representing a batch of target outputs.
Returns:

Scalar loss.

Return type:

float