Graph.gradient(tensor, variables, *, name=None)

Calculate the gradients for all the variables.

The gradient is a list containing all the first partial derivatives of the tensor with respect to the variables.

Parameters
• tensor (Tensor(real)) – The real tensor $$T$$ whose gradient vector you want to calculate. If the tensor is not scalar, each dimension belongs to a batch.

• variables (list[Tensor(real)]) – The list of real variables $$\{\theta_i\}$$ with respect to which you want to take the first partial derivatives of the tensor. If batching is used, each variable must have the same batch dimension as tensor (or must be broadcastable to it).

• name (str, optional) – The name of the node.

Returns

A list of gradients containing the first partial derivatives of the tensor $$T$$ with respect to the variables $$\{\theta_i\}$$.

Return type

Sequence[Tensor(real)]

Warning

This function currently doesn’t support calculating a gradient vector for a graph which includes an infidelity_pwc node if it involves a Hamiltonian with degenerate eigenvalues at any segment. In that case, the function returns a nan gradient vector.

Notes

The $$i$$ element of the gradient contains the partial derivative of the tensor with respect to the ith variables:

$(\nabla T)_{i} = \frac{\partial T}{\partial \theta_i}.$

The variables $$\{\theta_i\}$$ follow the same sequence as the input list of variables and each element has the same shape as the corresponding one in the variables list.