code: def cast(tensor, dtype):
      """Casts the given tensor to a new type.
  
      Args:
          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:
          tensor_like: a tensor with the same shape and values as ``tensor`` and the
          same dtype as ``dtype``
  
      **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])>
      """
      if isinstance(tensor, (list, tuple, int, float, complex)):
          tensor = np.asarray(tensor)
  
      if not isinstance(dtype, str):
          try:
              dtype = np.dtype(dtype).name
          except (AttributeError, TypeError, ImportError):
              dtype = getattr(dtype, "name", dtype)
  
      return ar.astype(
          tensor,
          ar.to_backend_dtype(dtype, like=ar.infer_backend(tensor)),
      )
  
file_path: pennylane/pennylane/math/utils.py
imports: import warnings
  import autoray
  import numpy
  from autograd.numpy.numpy_boxes import ArrayBox
  from autoray import numpy
  from .None import single_dispatch
label_desc: Use image node_id 3 for calling a global function with example usage: cast(tensor, dtype) and returns: ar