qml.math.ones_like¶
-
ones_like
(tensor, dtype=None)[source]¶ Returns a tensor of all ones with the same shape and dtype as the input tensor.
- Parameters
tensor (tensor_like) – input tensor
dtype (str, np.dtype, None) – The desired output datatype of the array. If not provided, the dtype of
tensor
is used. This argument can be any supported NumPy dtype representation, including 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
an all-ones tensor with the same shape and size as
tensor
- Return type
tensor_like
Example
>>> x = torch.tensor([1., 2.]) >>> ones_like(x) tensor([1., 1.]) >>> y = tf.Variable([[0], [5]]) >>> ones_like(y, dtype=np.complex128) <tf.Tensor: shape=(2, 1), dtype=complex128, numpy= array([[1.+0.j], [1.+0.j]])>
code/api/pennylane.math.ones_like
Download Python script
Download Notebook
View on GitHub