qml.math

This package contains unified functions for framework-agnostic tensor and array manipulation. Given the input tensor-like object, the call is dispatched to the corresponding array manipulation framework, allowing for end-to-end differentiation to be preserved.

Warning

These functions are experimental, and only a subset of common functionality is supported. Furthermore, the names and behaviour of these functions may differ from similar functions in common frameworks; please refer to the function docstrings for more details.

Internally, these functions dispatch by using the TensorBox class, a container and API for array-like objects that allows array manipulation to be performed in a unified manner for supported tensor/array manipulation frameworks.

The following frameworks are currently supported:

  • NumPy

  • Autograd

  • TensorFlow

  • PyTorch

Functions

T(tensor)

Returns the transpose of the tensor by reversing the order of the axes.

abs(tensor)

Returns the element-wise absolute value.

allclose(a, b[, rtol, atol])

Returns True if two arrays are element-wise equal within a tolerance.

allequal(tensor1, tensor2, **kwargs)

Returns True if two tensors are element-wise equal along a given axis.

angle(tensor)

Returns the element-wise angle of a complex tensor.

arcsin(tensor)

Returns the element-wise inverse sine of the tensor

block_diag(values)

Combine a sequence of 2D tensors to form a block diagonal tensor.

cast(tensor, dtype)

Casts the given tensor to a new type.

cast_like(tensor1, tensor2)

Casts a tensor to the same dtype as another.

concatenate(values[, axis])

Concatenate a sequence of tensors along the specified axis.

conj(tensor)

Conjugate a tensor.

convert_like(tensor1, tensor2)

Convert a tensor to the same type as another.

cov_matrix(prob, obs[, wires, diag_approx])

Calculate the covariance matrix of a list of commuting observables, given the joint probability distribution of the system in the shared eigenbasis.

diag(values[, k])

Construct a diagonal tensor from a list of scalars.

dot(tensor1, tensor2)

Returns the matrix or dot product of two tensors.

expand_dims(tensor, axis)

Expand the shape of an array by adding a new dimension of size 1 at the specified axis location.

flatten(tensor)

Flattens an N-dimensional tensor to a 1-dimensional tensor.

gather(tensor, indices)

Gather tensor values given a tuple of indices.

get_interface(tensor)

Returns the name of the package that any array/tensor manipulations will dispatch to.

marginal_prob(prob, axis)

Compute the marginal probability given a joint probability distribution expressed as a tensor.

ones_like(tensor[, dtype])

Returns a tensor of all ones with the same shape and dtype as the input tensor.

requires_grad(tensor)

Returns True if the tensor is considered trainable.

reshape(tensor, shape)

Gives a new shape to a tensor without changing its data.

scatter_element_add(tensor, index, value)

Adds a scalar value to a specific index of a tensor.

shape(tensor)

Returns the shape of the tensor.

sqrt(tensor)

Returns the element-wise square root.

squeeze(tensor)

Remove single-dimensional entries from the shape of an array.

stack(values[, axis])

Stack a sequence of tensors along the specified axis.

sum(tensor[, axis, keepdims])

TensorBox: Returns the sum of the tensor elements across the specified dimensions.

take(tensor, indices[, axis])

Gather elements from a tensor.

toarray(tensor)

Returns the tensor as a NumPy ndarray.

where(condition, x, y)

Returns elements chosen from x or y depending on a boolean tensor condition.

wrap_output(func)

Decorator to automate the wrapping of TensorBox method outputs.

Classes

TensorBox(tensor)

A container for array-like objects that allows array manipulation to be performed in a unified manner for supported tensor/array manipulation frameworks.