Losses#

Pass a loss by name or hand Trainer a callable:

import pyqit
import pennylane.numpy as pnp

pyqit.Trainer(max_epochs=30, loss_fn="cross_entropy")

def weighted_mse(preds, targets):
    return pnp.mean((preds - targets) ** 2 * (1 + targets))

pyqit.Trainer(max_epochs=30, loss_fn=weighted_mse)

Available losses#

The name you pass is the class’s name tag, given on its page along with the targets it expects.

MSELoss

Mean squared error, the Trainer's default loss.

HingeLoss

Hinge loss for binary labels encoded as 0/1.

CrossEntropyLoss

Cross entropy over class probabilities.

get_loss_fn

Resolve a loss name to a callable bound to backend.

Models emit probabilities, not logits#

This trips up anyone porting a torch loss. F.cross_entropy applies its own log-softmax, so feeding it probabilities scores the wrong thing quietly rather than raising. CrossEntropyLoss takes the log directly instead. Write new torch losses the same way.

PennyLane ships no ML losses of its own, so the pnp implementations here are hand written by design.

Adding a loss#

Subclass BaseLoss and tag it. Nothing hand-registers it, because loss_registry() discovers classes by walking the package for an object_type of "loss". The backends tag declares which backends the class implements, and a backend listed there needs the matching _pennylane or _torch method. target_dtype tells the Lightning adapter whether targets are class indices.

One caveat costs people an afternoon. skbase’s class walk skips modules whose name starts with an underscore, so a loss defined in a private module never registers and never says why.

Add the class name to the list above. See the contributing guide.

BaseLoss

Base class for backend-dispatching losses.