class grad(fun, argnum=None)[source]

Bases: object

Returns the gradient as a callable function of (functions of) QNodes.

By default, gradients are computed for arguments which contain the property requires_grad=True. Alternatively, the argnum keyword argument can be specified to compute gradients for function arguments without this property, such as scalars, lists, tuples, dicts, or vanilla NumPy arrays. Setting argnum to the index of an argument with requires_grad=False will raise a NonDifferentiableError.

When the output gradient function is executed, both the forward pass and the backward pass will be performed in order to compute the gradient. The value of the forward pass is available via the forward property.

Parameters
• func (function) – a plain QNode, or a Python function that contains a combination of quantum and classical nodes

• argnum (int, list(int), None) – Which argument(s) to take the gradient with respect to. By default, the arguments themselves are used to determine differentiability, by examining the requires_grad property.

Returns

The function that returns the gradient of the input function with respect to the differentiable arguments, or, if specified, the arguments in argnum.

Return type

function

 forward The result of the forward pass calculated while performing backpropagation.
forward

The result of the forward pass calculated while performing backpropagation. Will return None if the backpropagation has not yet been performed.

Type

float

 __call__(*args, **kwargs) Evaluates the gradient function, and saves the function value calculated during the forward pass in forward.
__call__(*args, **kwargs)[source]

Evaluates the gradient function, and saves the function value calculated during the forward pass in forward.