tensorcircuit.interfaces.tensortrans¶

general function for interfaces transformation

tensorcircuit.interfaces.tensortrans.args_to_tensor(f: Callable[[...], Any], argnums: int | Sequence[int] = 0, tensor_as_matrix: bool = False, gate_to_tensor: bool = False, gate_as_matrix: bool = True, qop_to_tensor: bool = False, qop_as_matrix: bool = True, cast_dtype: bool = True) Callable[[...], Any][source]¶

Function decorator that automatically convert inputs to tensors on current backend

Example:

tc.set_backend("jax")

@partial(
tc.interfaces.args_to_tensor,
argnums=[0, 1, 2],
gate_to_tensor=True,
qop_to_tensor=True,
)
def f(a, b, c, d):
    return a, b, c, d

f(
[tc.Gate(np.ones([2, 2])), tc.Gate(np.ones([2, 2, 2, 2]))],
tc.QuOperator.from_tensor(np.ones([2, 2, 2, 2, 2, 2])),
np.ones([2, 2, 2, 2]),
tf.zeros([1, 2]),
)

# ([DeviceArray([[1.+0.j, 1.+0.j],
#        [1.+0.j, 1.+0.j]], dtype=complex64),
# DeviceArray([[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]], dtype=complex64)],
# DeviceArray([[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
#             1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
#             1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
#             1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
#             1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
#             1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
#             1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
#             1.+0.j],
#             [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
#             1.+0.j]], dtype=complex64),
# DeviceArray([[[[1.+0.j, 1.+0.j],
#                 [1.+0.j, 1.+0.j]],

#             [[1.+0.j, 1.+0.j],
#                 [1.+0.j, 1.+0.j]]],


#             [[[1.+0.j, 1.+0.j],
#                 [1.+0.j, 1.+0.j]],

#             [[1.+0.j, 1.+0.j],
#                 [1.+0.j, 1.+0.j]]]], dtype=complex64),
# <tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[0., 0.]], dtype=float32)>)
Parameters:
  • f (Callable[..., Any]) – the wrapped function whose arguments in argnums position are expected to be tensor format

  • argnums (Union[int, Sequence[int]], optional) – position of args under the auto conversion, defaults to 0

  • tensor_as_matrix (bool, optional) – try reshape all input tensor as matrix with shape rank 2, defaults to False

  • gate_to_tensor (bool, optional) – convert Gate to tensor, defaults to False

  • gate_as_matrix (bool, optional) – reshape tensor from Gate input as matrix, defaults to True

  • qop_to_tensor (bool, optional) – convert QuOperator to tensor, defaults to False

  • qop_as_matrix (bool, optional) – reshape tensor from QuOperator input as matrix, defaults to True

  • cast_dtype (bool, optional) – whether cast to backend dtype, defaults to True

Returns:

The wrapped function

Return type:

Callable[…, Any]

tensorcircuit.interfaces.tensortrans.gate_to_matrix(t: Gate, is_reshapem: bool = True) Any[source]¶
tensorcircuit.interfaces.tensortrans.general_args_to_backend(args: Any, dtype: Any | None = None, target_backend: Any | None = None, enable_dlpack: bool = True) Any[source]¶
tensorcircuit.interfaces.tensortrans.general_args_to_numpy(args: Any) Any[source]¶

Given a pytree, get the corresponding numpy array pytree

Parameters:

args (Any) – pytree

Returns:

the same format pytree with all tensor replaced by numpy array

Return type:

Any

tensorcircuit.interfaces.tensortrans.numpy_args_to_backend(args: Any, dtype: Any | None = None, target_backend: Any | None = None) Any[source]¶

Given a pytree of numpy arrays, get the corresponding tensor pytree

Parameters:
  • args (Any) – pytree of numpy arrays

  • dtype (Any, optional) – str of str of the same pytree shape as args, defaults to None

  • target_backend (Any, optional) – str or backend object, defaults to None, indicating the current default backend

Returns:

the same format pytree with all numpy array replaced by the tensors in the target backend

Return type:

Any

tensorcircuit.interfaces.tensortrans.numpy_to_tensor(t: Any, backend: Any) Any[source]¶
tensorcircuit.interfaces.tensortrans.qop_to_matrix(t: QuOperator, is_reshapem: bool = True) Any[source]¶
tensorcircuit.interfaces.tensortrans.tensor_to_backend_jittable(t: Any) Any[source]¶
tensorcircuit.interfaces.tensortrans.tensor_to_dlpack(t: Any) Any[source]¶
tensorcircuit.interfaces.tensortrans.tensor_to_dtype(t: Any) str[source]¶
tensorcircuit.interfaces.tensortrans.tensor_to_numpy(t: Any) Any[source]¶
tensorcircuit.interfaces.tensortrans.which_backend(a: Any, return_backend: bool = True) Any[source]¶

Given a tensor a, return the corresponding backend

Parameters:
  • a (Tensor) – the tensor

  • return_backend (bool, optional) – if true, return backend object, if false, return backend str, defaults to True

Returns:

the backend object or backend str

Return type:

Any