merlin.algorithms.loss module

Specialized loss functions for QML

class merlin.algorithms.loss.NKernelAlignment

Bases: _Loss

Negative kernel-target alignment loss function for quantum kernel training.

Within quantum kernel alignment, the goal is to maximize the alignment between the quantum kernel matrix and the ideal target matrix given by \(K^{*} = y y^T\), where \(y \in \{-1, +1\}\) are the target labels.

The negative kernel alignment loss is given as:

\[\text{NKA}(K, K^{*}) = -\frac{\operatorname{Tr}(K K^{*})}{ \sqrt{\operatorname{Tr}(K^2)\operatorname{Tr}(K^{*2})}}\]
Parameters:

None

forward(input, target)

Compute the negative kernel-target alignment loss.

Parameters:
  • input (torch.Tensor) – Kernel matrix.

  • target (torch.Tensor) – Binary label vector in {-1, +1} or target kernel matrix.

Returns:

Scalar loss value.

Return type:

torch.Tensor

Raises:

ValueError – If input is not two-dimensional or if target contains values other than -1 and +1.