qml.numpy.tensor_wrapper¶
-
class
tensor_wrapper
(obj)[source]¶ Decorator that wraps callable objects and classes so that they both accept a
requires_grad
keyword argument, as well as returning a PennyLanetensor
.Only if the decorated object returns an
ndarray
is the output converted to atensor
; this avoids superfluous conversion of scalars and other native-Python types.Note
This wrapper does not enable autodifferentiation of the wrapped function, it merely adds support for
tensor
output.- Parameters
obj – a callable object or class
Example
By default, the
ones
function provided by Autograd constructs standardndarray
objects, and does not permit arequires_grad
argument:>>> from autograd.numpy import ones >>> ones([2, 2]) array([[1., 1.], [1., 1.]]) >>> ones([2, 2], requires_grad=True) TypeError: ones() got an unexpected keyword argument 'requires_grad'
tensor_wrapper
both enables construction oftensor
objects, while also converting the output.>>> from pennylane import numpy as np >>> ones = np.tensor_wrapper(ones) >>> ones([2, 2], requires_grad=True) tensor([[1., 1.], [1., 1.]], requires_grad=True)
code/api/pennylane.numpy.tensor_wrapper
Download Python script
Download Notebook
View on GitHub