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"
), anp.dtype
object (np.dtype("float64")
), or a dtype class (np.float64
). Iftensor
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 asdtype
- 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])>
code/api/pennylane.math.cast
Download Python script
Download Notebook
View on GitHub