quri_parts.algo.optimizer.interface module#

Params: TypeAlias = 'npt.NDArray[np.float64]'#

Represents a parameter vector subject to optimization. (A gradient vector is also represented as Params.)

CostFunction#

Cost function for optimization.

alias of Callable[[npt.NDArray[np.float64]], float]

GradientFunction#

Gradient function for optimization.

alias of Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]

class OptimizerStatus(value)#

Bases: Enum

Status of optimization.

SUCCESS = 1#

No error, not converged yet.

FAILED = 2#

The optimization failed and cannot be continued.

CONVERGED = 3#

The optimization converged.

class OptimizerState(params, cost=0.0, status=OptimizerStatus.SUCCESS, niter=0, funcalls=0, gradcalls=0)#

Bases: object

An immutable (frozen) dataclass representing an optimizer state.

Parameters:
  • params (algo.optimizer.interface.Params) –

  • cost (float) –

  • status (OptimizerStatus) –

  • niter (int) –

  • funcalls (int) –

  • gradcalls (int) –

params: Params#

Current parameter values.

cost: float = 0.0#

Current value of the cost function.

status: OptimizerStatus = 1#

Optimization status.

niter: int = 0#

Number of iterations.

funcalls: int = 0#

Number of cost function calls.

gradcalls: int = 0#

Number of gradient function calls.

property n_params: int#

Number of parameters.

class Optimizer(*args, **kwargs)#

Bases: Protocol

A protocol class for optimizers.

abstract get_init_state(init_params)#

Returns an initial state for optimization.

Parameters:

init_params (algo.optimizer.interface.Params) –

Return type:

OptimizerState

abstract step(state, cost_function, grad_function=None)#

Run a single optimization step and returns a new state.

Parameters:
  • state (OptimizerState) –

  • cost_function (algo.optimizer.interface.CostFunction) –

  • grad_function (algo.optimizer.interface.GradientFunction | None) –

Return type:

OptimizerState