qp.math.requires_grad

requires_grad(tensor, interface=None)[source]

Returns True if the tensor is considered trainable.

Warning

The implementation depends on the contained tensor type, and may be context dependent.

For example, Torch tensors and PennyLane tensors track trainability as a property of the tensor itself.

Parameters:
  • tensor (tensor_like) – input tensor

  • interface (str) – The name of the interface. Will be determined automatically if not provided.

Returns:

whether the tensor is trainable or not.

Return type:

bool

Example

Calling this function on a PennyLane NumPy array:

>>> x = np.array([1., 5.], requires_grad=True)
>>> requires_grad(x)
True
>>> x.requires_grad = False
>>> requires_grad(x)
False

PyTorch has similar behaviour.