quri_parts.algo.optimizer package#
- Params = '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:
- 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:
- class Adam(lr=0.05, betas=(0.9, 0.999), eps=1e-09, ftol=1e-05)#
Bases:
Optimizer
Adam optimization algorithm proposed in [1].
- Parameters:
lr (float) – learning rate.
betas (Sequence[float]) – coefficients used in the update rules of the moving averages of the gradient and its magnitude.
betas
represents the robustness of the optimizer. Hence, when using sampling, the higher values forbetas
are recommended.eps (float) – a small scaler number used for avoiding zero division.
ftol (Optional[float]) – If not None, judge convergence by cost function tolerance. See
ftol()
for details.
- Ref:
[1] Adam: A Method for Stochastic Optimization, Diederik P. Kingma, Jimmy Ba (2014). https://arxiv.org/abs/1412.6980.
- get_init_state(init_params)#
Returns an initial state for optimization.
- Parameters:
init_params (algo.optimizer.interface.Params) –
- Return type:
- 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:
- class AdaBelief(lr=0.001, betas=(0.9, 0.99), eps=1e-16, ftol=1e-05)#
Bases:
Adam
AdaBelief optimization algorithm proposed in [1].
- Parameters:
lr (float) – learning rate.
betas (Sequence[float]) – coefficients used in the update rules of the moving averages of the gradient and its magnitude.
betas
represents the robustness of the optimizer. Hence, when using sampling, the higher values forbetas
are recommended.eps (float) – a small scaler number used for avoiding zero division.
ftol (Optional[float]) – If not None, judge convergence by cost function tolerance. See
ftol()
for details.
- Ref:
[1] AdaBelief Optimizer: Adapting Stepsizes by the Belief in Observed Gradients, Juntang Zhuang, Tommy Tang, Yifan Ding, Sekhar Tatikonda, Nicha Dvornek, Xenophon Papademetris, James S. Duncan (2020). https://arxiv.org/abs/2010.07468.
- class LBFGS(c1=0.0001, c2=0.4, amin=1e-100, amax=1e+100, maxiter_linesearch=20, rho_const=1000.0, m=5, gtol=1e-06)#
Bases:
Optimizer
L-BFGS (Limited memory Bryden-Fletcher-Goldfarb-Shanno) optimizer. Partially inspired by SciPy implementation of BFGS optimizer [1]. For the details of algorithm, see [2].
- Parameters:
c1 (float) – coefficient in strong Wolfe condition. It determines the range of the cost function.
c2 (float) – coefficient in strong Wolfe condition. It determines the range of the derivative of the cost function.
amin (float) – lower bound of the value of the step size that is computed in line search.
amax (float) – upper bound of the value of the step size that is computed in line search.
maxiter_linesearch (int) – the maximum number of the iteration used in line search.
rho_const (float) – when computing \(1/x\), where \(x\) is a scaler, sometimes it returns zero division error. In that case \(1/x\) is replaced by
rho_cost
.m (int) – In parameters update of each step, it uses the info of the last
m
steps.gtol (Optional[float]) – If not None, it is used for determining if the opotimization has terminated successfully. If
gtol
is less than the infinity norm of the gradient of the cost function, the optimization is regarded to have terminated successfully. The infinity norm of the gradient \(g\) is defined as \(||g||_{\infty} = \max\{|g_1|, |g_2|, \ldots, |g_n|\}\)
- Refs:
[1]: https://github.com/scipy/scipy/blob/master/scipy/optimize/optimize.py [2]: Jorge Nocedal and Stephen J. Wright.
Numerical Optimization (Springer, New York, 2006).
- get_init_state(init_params)#
Returns an initial state for optimization.
- Parameters:
init_params (algo.optimizer.interface.Params) –
- Return type:
- 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:
- class NFT(randomize=False, reset_interval=32, eps=1e-32, ftol=1e-05)#
Bases:
NFTBase
Nakanishi-Fujii-Todo optimization algorithm proposed in [1]. The algorithms optimizes the cost function sequentially with respect to the parameters.
- Parameters:
randomize (bool) – If
True
, the order of the parameters over which the cost function is optimized will be random. IfFalse
, the order will be the same as the order of the array of initial parameters.reset_interval (Optional[int]) – minimum value of the cost function is evaluated by direct function-call only once in
reset_interval
times.eps (float) – a small number used for avoiding zero division.
ftol (Optional[float]) – If not None, judge convergence by cost function tolerance. See
ftol()
for details.
- Ref:
- [1]: Ken M. Nakanishi, Keisuke Fujii, and Synge Todo,
Sequential minimal optimization for quantum-classical hybrid algorithms, Phys. Rev. Research 2, 043158 (2020).
- class NFTfit(randomize=False, n_points=3, ftol=1e-05)#
Bases:
NFTBase
Basically the same as Nakanishi-Fujii-Todo optimization algorithm [1]. The difference from
NFT
class is thatNFTfit
uses SciPy fitting functioncurve_fit
for parameters update.- Parameters:
randomize (bool) – If
True
, the order of the parameters over which the cost function is optimized will be random. IfFalse
, the order will be the same as the order of the array of initial parameters.n_point – Number of values of cost function for function fitting using SciPy fitting function
curve_fit
.ftol (Optional[float]) – If not None, judge convergence by cost function tolerace. See
ftol()
for details.n_points (int) –
- Ref:
- [1]: Ken M. Nakanishi, Keisuke Fujii, and Synge Todo,
Sequential minimal optimization for quantum-classical hybrid algorithms, Phys. Rev. Research 2, 043158 (2020).
- class SPSA(a=0.6283185307179586, c=0.1, alpha=0.602, gamma=0.101, A=0.0, ftol=1e-05, rng_seed=None)#
Bases:
Optimizer
Simultaneous perturbation stochastic approximation (SPSA) optimizer. The implementation is heavily inspired by [1]. Given the parameters \(\theta_k\) at an iteration \(k\), the updated parameters \(\theta_{k+1}\) is given by
\[\theta_{k+1} = \theta_k - \alpha_k g_k(\theta_k), g_k(\theta_k) = \frac{f(\theta_k+c_k \Delta_k)-f(\theta_k-c_k \Delta_k)}{2 c_k} \Delta_k^{-1}, a_k = a / (A + k + 1)^\alpha, c_k = c / (k + 1)^\gamma,\]where \(f\) is the cost function to be minimized, and \(\Delta_k\) is a vector generated randomly. \(\Delta_k^{-1}\) is defined as the element-wise inverse of \(\Delta_k\). The dimension of \(\Delta_k\) is the same as that of \(\theta_k\). In this optimizer, \(\Delta_k\) is generated from a Bernoulli distribution. Note that \(g_k(\theta_k)\) works as an estimate of the first order gradient of the cost function \(f(\theta_k)\).
The main advantage of the SPSA optimizer is that it requires only 2 function evaluations to estimate the gradient in each iteration. Whereas the standard gradient-based optimizers require \(2p\) or more function evaluations (\(p\): the parameter length/size) in each iteration to compute the gradient. Hence the SPSA could be useful when performing VQE with sampling.
- Parameters:
a (float) – \(a\) in the parameter update rule that is defined above.
c (float) – \(c\) in the parameter update rule that is defined above.
alpha (float) – \(\alpha\) in the parameter update rule that is defined above.
gamma (float) – \(\gamma\) in the parameter update rule that is defined above.
A (float) – \(A\) in the parameter update rule that is defined above. A recommended choice is A = (10 or 100) multiplied by the maximum number of iterations that the optimizer runs.
ftol (Optional[float]) – If not None, judge convergence by cost function tolerance. See
ftol()
for details.rng_seed (Optional[int]) –
- Ref:
- [1]: J. C. Spall, “Implementation of the simultaneous perturbation algorithm
for stochastic optimization,” in IEEE Transactions on Aerospace and Electronic Systems, vol. 34, no. 3, pp. 817-823, July 1998, doi: 10.1109/7.705889.
- get_init_state(init_params)#
Returns an initial state for optimization.
- Parameters:
init_params (algo.optimizer.interface.Params) –
- Return type:
- 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:
Submodules#
- quri_parts.algo.optimizer.adam module
- quri_parts.algo.optimizer.interface module
- quri_parts.algo.optimizer.lbfgs module
- quri_parts.algo.optimizer.nft module
- quri_parts.algo.optimizer.spsa module
- quri_parts.algo.optimizer.tolerance module