tensorcircuit.interfaces.tensortrans#
general function for interfaces transformation
- tensorcircuit.interfaces.tensortrans.args_to_tensor(f: Callable[[...], Any], argnums: Union[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] [源代码]#
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)>)
- 参数
f (Callable[..., Any]) -- the wrapped function whose arguments in
argnums
position are expected to be tensor formatargnums (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 Falsegate_as_matrix (bool, optional) -- reshape tensor from
Gate
input as matrix, defaults to Trueqop_to_tensor (bool, optional) -- convert
QuOperator
to tensor, defaults to Falseqop_as_matrix (bool, optional) -- reshape tensor from
QuOperator
input as matrix, defaults to Truecast_dtype (bool, optional) -- whether cast to backend dtype, defaults to True
- 返回
The wrapped function
- 返回类型
Callable[..., Any]
- tensorcircuit.interfaces.tensortrans.gate_to_matrix(t: tensorcircuit.gates.Gate, is_reshapem: bool = True) Any [源代码]#
- tensorcircuit.interfaces.tensortrans.general_args_to_backend(args: Any, dtype: Optional[Any] = None, target_backend: Optional[Any] = None, enable_dlpack: bool = True) Any [源代码]#
- tensorcircuit.interfaces.tensortrans.general_args_to_numpy(args: Any) Any [源代码]#
Given a pytree, get the corresponding numpy array pytree
- 参数
args (Any) -- pytree
- 返回
the same format pytree with all tensor replaced by numpy array
- 返回类型
Any
- tensorcircuit.interfaces.tensortrans.numpy_args_to_backend(args: Any, dtype: Optional[Any] = None, target_backend: Optional[Any] = None) Any [源代码]#
Given a pytree of numpy arrays, get the corresponding tensor pytree
- 参数
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
- 返回
the same format pytree with all numpy array replaced by the tensors in the target backend
- 返回类型
Any
- tensorcircuit.interfaces.tensortrans.qop_to_matrix(t: tensorcircuit.quantum.QuOperator, is_reshapem: bool = True) Any [源代码]#
- tensorcircuit.interfaces.tensortrans.which_backend(a: Any, return_backend: bool = True) Any [源代码]#
Given a tensor
a
, return the corresponding backend- 参数
a (Tensor) -- the tensor
return_backend (bool, optional) -- if true, return backend object, if false, return backend str, defaults to True
- 返回
the backend object or backend str
- 返回类型
Any