Source code for pennylane.optimize.gradient_descent
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.# Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at# http://www.apache.org/licenses/LICENSE-2.0# Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License."""Gradient descent optimizer"""frompennylane._gradimportgradasget_gradient
[docs]classGradientDescentOptimizer:r"""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 .. 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` .. note:: When using ``torch``, ``tensorflow`` or ``jax`` interfaces, refer to :doc:`Gradients and training </introduction/interfaces>` for suitable optimizers. """def__init__(self,stepsize=0.01):self.stepsize=stepsize
[docs]defstep_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 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 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. """g,forward=self.compute_grad(objective_fn,args,kwargs,grad_fn=grad_fn)new_args=self.apply_grad(g,args)ifforwardisNone:forward=objective_fn(*args,**kwargs)# unwrap from list if one argument, cleaner returniflen(new_args)==1:returnnew_args[0],forwardreturnnew_args,forward
[docs]defstep(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 grad_fn (function): optional gradient function of the 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. """g,_=self.compute_grad(objective_fn,args,kwargs,grad_fn=grad_fn)new_args=self.apply_grad(g,args)# unwrap from list if one argument, cleaner returniflen(new_args)==1:returnnew_args[0]returnnew_args
[docs]@staticmethoddefcompute_grad(objective_fn,args,kwargs,grad_fn=None):r"""Compute the 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 evaluated and instead ``None`` will be returned. """g=get_gradient(objective_fn)ifgrad_fnisNoneelsegrad_fngrad=g(*args,**kwargs)forward=getattr(g,"forward",None)num_trainable_args=sum(getattr(arg,"requires_grad",False)forarginargs)grad=(grad,)ifnum_trainable_args==1elsegradreturngrad,forward
[docs]defapply_grad(self,grad,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: grad (tuple [array]): the gradient of the objective 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=0forindex,arginenumerate(args):ifgetattr(arg,"requires_grad",False):args_new[index]=arg-self.stepsize*grad[trained_index]trained_index+=1returnargs_new