# Copyright 2018-2021 Xanadu Quantum Technologies Inc.

# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# Unless required by applicable law or agreed to in writing, software
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and

Base class for other gradient-descent-based optimizers.

A step of the gradient descent optimizer computes the new values via the rule

.. math::

x^{(t+1)} = x^{(t)} - \eta \nabla f(x^{(t)}).

where :math:\eta is a user-defined hyperparameter corresponding to step size.

Args:
stepsize (float): the user-defined hyperparameter :math:\eta
"""

def __init__(self, stepsize=0.01):
self.stepsize = stepsize

[docs]    def step_and_cost(self, objective_fn, *args, grad_fn=None, **kwargs):
"""Update trainable arguments with one step of the optimizer and return the corresponding
objective function value prior to the step.

Args:
objective_fn (function): the objective function for optimization
*args : variable length argument list for objective function
objective function with respect to the variables *args.
If None, the gradient function is computed automatically.
Must return a tuple[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:
tuple[list [array], float]: the new variable values :math:x^{(t+1)} and the objective
function output prior to the step.
If single arg is provided, list [array] is replaced by array.
"""

if forward is None:
forward = objective_fn(*args, **kwargs)

# unwrap from list if one argument, cleaner return
if len(new_args) == 1:
return new_args[0], forward
return new_args, forward

[docs]    def step(self, objective_fn, *args, grad_fn=None, **kwargs):
"""Update trainable arguments with one step of the optimizer.

Args:
objective_fn (function): the objective function for optimization
*args : Variable length argument list for objective function
objective function with respect to the variables x.
If None, the gradient function is computed automatically.
Must return a tuple[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:
list [array]: the new variable values :math:x^{(t+1)}.
If single arg is provided, list [array] is replaced by array.
"""

# unwrap from list if one argument, cleaner return
if len(new_args) == 1:
return new_args[0]

return new_args

[docs]    @staticmethod
r"""Compute gradient of the objective function at the given point and return it along with
the objective function forward pass (if available).

Args:
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. If None, the gradient function is computed automatically.
Must return the same shape of tuple [array] as the autograd derivative.

Returns:
tuple (array): NumPy array containing the gradient :math:\nabla f(x^{(t)}) and the
objective function output. If grad_fn is provided, the objective function
will not be evaluted and instead None will be returned.
"""
forward = getattr(g, "forward", None)

num_trainable_args = sum(getattr(arg, "requires_grad", False) for arg in args)

r"""Update the variables to take a single optimization step. Flattens and unflattens
the inputs to maintain nested iterables as the parameters of the optimization.

Args:
function at point :math:x^{(t)}: :math:\nabla f(x^{(t)})
args (tuple): the current value of the variables :math:x^{(t)}

Returns:
list [array]: the new values :math:x^{(t+1)}
"""
args_new = list(args)

trained_index = 0
for index, arg in enumerate(args):
args_new[index] = arg - self.stepsize * grad[trained_index]

trained_index += 1

return args_new


Using PennyLane

Development

API

Internals