EarlyStopping¶
- class EarlyStopping(patience=5, min_delta=0.0001, grad_norm_threshold=None, variance_window=20, variance_threshold=None)[source]¶
Bases:
objectEarly 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 leastmin_delta) before stopping. Must be ≥ 1.min_delta (
float) – Minimum absolute decrease inbest_lossthat counts as an improvement. Must be ≥ 0.grad_norm_threshold (
float|None) – If notNone, stop when the L2 norm of the gradient drops below this value. Only effective when the optimizer exposes gradient information (e.g.ScipyOptimizerwithL_BFGS_B).variance_window (
int) – Number of recent cost values used to compute the rolling variance. Must be ≥ 2.variance_threshold (
float|None) – If notNone, stop when the variance of the lastvariance_windowcost 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:
- Return type:
- Returns:
A
StopReasonif any criterion triggered, otherwiseNone(meaning optimization should continue).