qml.math.cast

cast(tensor, dtype)[source]

Casts the given tensor to a new type.

Parameters
  • tensor (tensor_like) – tensor to cast

  • dtype (str, np.dtype) – Any supported NumPy dtype representation; this can be a string ("float64"), a np.dtype object (np.dtype("float64")), or a dtype class (np.float64). If tensor is not a NumPy array, the equivalent dtype in the dispatched framework is used.

Returns

a tensor with the same shape and values as tensor and the same dtype as dtype

Return type

tensor_like

Example

We can use NumPy dtype specifiers:

>>> x = torch.tensor([1, 2])
>>> cast(x, np.float64)
tensor([1., 2.], dtype=torch.float64)

We can also use strings:

>>> x = tf.Variable([1, 2])
>>> cast(x, "complex128")
<tf.Tensor: shape=(2,), dtype=complex128, numpy=array([1.+0.j, 2.+0.j])>

Contents

Using PennyLane

Development

API

Internals