qml.GradientDescentOptimizer¶
- class GradientDescentOptimizer(stepsize=0.01)[source]¶
Bases:
object
Basic gradient-descent optimizer.
Base class for other gradient-descent-based optimizers.
A step of the gradient descent optimizer computes the new values via the rule
\[x^{(t+1)} = x^{(t)} - \eta \nabla f(x^{(t)}).\]where \(\eta\) is a user-defined hyperparameter corresponding to step size.
- Parameters
stepsize (float) – the user-defined hyperparameter \(\eta\)
Note
When using
torch
,tensorflow
orjax
interfaces, refer to Gradients and training for suitable optimizers.Methods
apply_grad
(grad, args)Update the variables to take a single optimization step.
compute_grad
(objective_fn, args, kwargs[, ...])Compute the gradient of the objective function at the given point and return it along with the objective function forward pass (if available).
step
(objective_fn, *args[, grad_fn])Update trainable arguments with one step of the optimizer.
step_and_cost
(objective_fn, *args[, grad_fn])Update trainable arguments with one step of the optimizer and return the corresponding objective function value prior to the step.
- apply_grad(grad, args)[source]¶
Update the variables to take a single optimization step. Flattens and unflattens the inputs to maintain nested iterables as the parameters of the optimization.
- Parameters
grad (tuple [array]) – the gradient of the objective function at point \(x^{(t)}\): \(\nabla f(x^{(t)})\)
args (tuple) – the current value of the variables \(x^{(t)}\)
- Returns
the new values \(x^{(t+1)}\)
- Return type
list [array]
- static compute_grad(objective_fn, args, kwargs, grad_fn=None)[source]¶
Compute the gradient of the objective function at the given point and return it along with the objective function forward pass (if available).
- Parameters
objective_fn (function) – the objective function for optimization
args (tuple) – tuple of NumPy arrays containing the current parameters for the objection function
kwargs (dict) – keyword arguments for the objective function
grad_fn (function) – optional gradient function of the objective function with respect to the variables
args
. IfNone
, the gradient function is computed automatically. Must return the same shape of tuple [array] as the autograd derivative.
- Returns
NumPy array containing the gradient \(\nabla f(x^{(t)})\) and the objective function output. If
grad_fn
is provided, the objective function will not be evaluated and insteadNone
will be returned.- Return type
tuple (array)
- step(objective_fn, *args, grad_fn=None, **kwargs)[source]¶
Update trainable arguments with one step of the optimizer.
- Parameters
objective_fn (function) – the objective function for optimization
*args – Variable length argument list for objective function
grad_fn (function) – optional gradient function of the objective function with respect to the variables
x
. IfNone
, the gradient function is computed automatically. Must return atuple[array]
with the same number of elements as*args
. Each array of the tuple should have the same shape as the corresponding argument.**kwargs – variable length of keyword arguments for the objective function
- Returns
the new variable values \(x^{(t+1)}\). If single arg is provided, list [array] is replaced by array.
- Return type
list [array]
- step_and_cost(objective_fn, *args, grad_fn=None, **kwargs)[source]¶
Update trainable arguments with one step of the optimizer and return the corresponding objective function value prior to the step.
- Parameters
objective_fn (function) – the objective function for optimization
*args – variable length argument list for objective function
grad_fn (function) – optional gradient function of the objective function with respect to the variables
*args
. IfNone
, the gradient function is computed automatically. Must return atuple[array]
with the same number of elements as*args
. Each array of the tuple should have the same shape as the corresponding argument.**kwargs – variable length of keyword arguments for the objective function
- Returns
the new variable values \(x^{(t+1)}\) and the objective function output prior to the step. If single arg is provided, list [array] is replaced by array.
- Return type
tuple[list [array], float]