EarlyStopping

class EarlyStopping(patience=5, min_delta=0.0001, grad_norm_threshold=None, variance_window=20, variance_threshold=None)[source]

Bases: object

Early stopping controller for variational quantum algorithm optimization.

Tracks optimization progress and signals when to stop based on configurable criteria. A single instance is created before the optimization loop and check() is called once per iteration.

Parameters:
  • patience (int) – Number of consecutive iterations with no improvement (by at least min_delta) before stopping. Must be ≥ 1.

  • min_delta (float) – Minimum absolute decrease in best_loss that counts as an improvement. Must be ≥ 0.

  • grad_norm_threshold (float | None) – If not None, stop when the L2 norm of the gradient drops below this value. Only effective when the optimizer exposes gradient information (e.g. ScipyOptimizer with L_BFGS_B).

  • variance_window (int) – Number of recent cost values used to compute the rolling variance. Must be ≥ 2.

  • variance_threshold (float | None) – If not None, stop when the variance of the last variance_window cost values drops below this value.

Raises:

ValueError – If any parameter violates its constraints.

Methods Summary

check(current_loss, *[, grad_norm])

Evaluate all enabled stopping criteria.

reset()

Reset internal state so the instance can be reused.

Methods Documentation

check(current_loss, *, grad_norm=None)[source]

Evaluate all enabled stopping criteria.

Must be called once per iteration, after loss (and optionally gradient) computation.

Parameters:
  • current_loss (float) – The minimum loss value observed at this iteration.

  • grad_norm (float | None) – L2 norm of the current gradient vector, or None if gradient information is not available.

Return type:

StopReason | None

Returns:

A StopReason if any criterion triggered, otherwise None (meaning optimization should continue).

reset()[source]

Reset internal state so the instance can be reused.

Return type:

None