qml.math.unwrap¶
- unwrap(values, max_depth=None)[source]¶
Unwrap a sequence of objects to NumPy arrays.
Note that tensors on GPUs will automatically be copied to the CPU.
- Parameters
values (Sequence[tensor_like]) – sequence of tensor-like objects to unwrap
max_depth (int) – Positive integer indicating the depth of unwrapping to perform for nested tensor-objects. This argument only applies when unwrapping Autograd
ArrayBox
objects.
Example
>>> values = [np.array([0.1, 0.2]), torch.tensor(0.1, dtype=torch.float64), torch.tensor([0.5, 0.2])] >>> math.unwrap(values) [array([0.1, 0.2]), 0.1, array([0.5, 0.2], dtype=float32)]
This function will continue to work during backpropagation:
>>> def cost_fn(params): ... unwrapped_params = math.unwrap(params) ... print("Unwrapped:", [(i, type(i)) for i in unwrapped_params]) ... return np.sum(np.sin(params)) >>> params = np.array([0.1, 0.2, 0.3]) >>> grad = autograd.grad(cost_fn)(params) Unwrapped: [(0.1, <class 'numpy.float64'>), (0.2, <class 'numpy.float64'>), (0.3, <class 'numpy.float64'>)] >>> print(grad) [0.99500417 0.98006658 0.95533649]
code/api/pennylane.math.unwrap
Download Python script
Download Notebook
View on GitHub