Skip to content

SPSAStoppingConfig

from cascaqit import SPSAStoppingConfig

SPSAStoppingConfig

SPSAStoppingConfig(
    window_size: int = 3,
    min_iterations: int = 3,
    objective_range_tolerance: float = 0.001,
    update_norm_tolerance: float = 0.001,
    gradient_norm_tolerance: float = 0.01,
    gradient_standard_error_norm_tolerance: float
    | None = None,
    sampled_standard_error_tolerance: float | None = None,
    schema_version: str = ALGORITHM_SCHEMA_VERSION,
)

Configure online stability stopping for SPSA. window_size must be at least 2 and min_iterations at least the window length. Every enabled tolerance must be finite and positive. After the minimum iterations, all enabled checks must pass in the latest window.

Tolerance Compared quantity
objective_range_tolerance Range of the per-iteration proxy formed from mean plus/minus objectives; this is not a direct center-point energy sequence
update_norm_tolerance Maximum parameter-update norm in the window
gradient_norm_tolerance Maximum estimated gradient norm in the window
gradient_standard_error_norm_tolerance Optional maximum gradient standard-error norm; SPSAConfig requires at least two directions per iteration
sampled_standard_error_tolerance Optional maximum standard error across plus/minus objective estimates; requires sampled objectives and at least 2 objective_repeats

Passing establishes stability under these thresholds in this window, not global optimality. Sampling budgets can stop execution before thresholds are met; inspect termination reasons and the individual checks.

from cascaqit import SPSAConfig, SPSAStoppingConfig

stopping = SPSAStoppingConfig(window_size=2, min_iterations=3,
                              gradient_standard_error_norm_tolerance=0.01)
config = SPSAConfig(directions_per_iteration=2, stopping=stopping)
assert config.stopping.window_size == 2
assert SPSAStoppingConfig.from_json(stopping.to_json()) == stopping

from_dict

from_dict(data: Mapping[str, Any]) -> SPSAStoppingConfig

Restore SPSAStoppingConfig from a dictionary. Omitted fields use defaults; nested configurations are restored and validated. Missing required fields or invalid values can raise KeyError, TypeError or ValueError.

from_json

from_json(text: str) -> SPSAStoppingConfig

Parse a JSON object and call from_dict(), returning SPSAStoppingConfig. Invalid JSON raises a parsing error; a non-object root raises TypeError.

to_dict

to_dict() -> dict[str, Any]

Return a JSON-compatible dictionary, serializing nested objects and converting tuples to arrays. This stores the declaration, not an execution result.

to_json

to_json(*, indent: int | None = None) -> str

Return a JSON string without writing a file. indent=None uses compact formatting; supply an indentation width for readable output.

stable_hash

stable_hash() -> str

Return the SHA-256 hex digest of canonical JSON. Fields, identifiers and metadata can affect it. Use it to compare saved content, not to decide physical equivalence.