tensorcircuit.utils#
Helper functions
- tensorcircuit.utils.append(f: Callable[[...], Any], *op: Callable[[...], Any]) Any [源代码]#
Functional programming paradigm to build function pipeline
- Example
>>> f = tc.utils.append(lambda x: x**2, lambda x: x+1, tc.backend.mean) >>> f(tc.backend.ones(2)) (2+0j)
- 参数
f (Callable[..., Any]) -- The function which are attached with other functions
op (Callable[..., Any]) -- Function to be attached
- 返回
The final results after function pipeline
- 返回类型
Any
- tensorcircuit.utils.arg_alias(f: Callable[[...], Any], alias_dict: Dict[str, Union[str, Sequence[str]]], fix_doc: bool = True) Callable[[...], Any] [源代码]#
function argument alias decorator with new docstring
- 参数
f (Callable[..., Any]) -- _description_
alias_dict (Dict[str, Union[str, Sequence[str]]]) -- _description_
fix_doc (bool) -- whether to add doc for these new alias arguments, defaults True
- 返回
the decorated function
- 返回类型
Callable[..., Any]
- tensorcircuit.utils.benchmark(f: Any, *args: Any, tries: int = 5, verbose: bool = True) Tuple[Any, float, float] [源代码]#
benchmark jittable function with staging time and running time
- 参数
f (Any) -- _description_
tries (int, optional) -- _description_, defaults to 5
verbose (bool, optional) -- _description_, defaults to True
- 返回
_description_
- 返回类型
Tuple[Any, float, float]
- tensorcircuit.utils.is_m1mac() bool [源代码]#
check whether the running platform is MAC with M1 chip
- 返回
True for MAC M1 platform
- 返回类型
bool
- tensorcircuit.utils.return_partial(f: Callable[[...], Any], return_argnums: Union[int, Sequence[int]] = 0) Callable[[...], Any] [源代码]#
Return a callable function for output ith parts of the original output along the first axis. Original output supports List and Tensor.
- Example
>>> from tensorcircuit.utils import return_partial >>> testin = np.array([[1,2],[3,4],[5,6],[7,8]]) >>> # Method 1: >>> return_partial(lambda x: x, [1, 3])(testin) (array([3, 4]), array([7, 8])) >>> # Method 2: >>> from functools import partial >>> @partial(return_partial, return_argnums=(0,2)) ... def f(inp): ... return inp ... >>> f(testin) (array([1, 2]), array([5, 6]))
- 参数
f (Callable[..., Any]) -- The function to be applied this method
return_partial (Union[int, Sequence[int]]) -- The ith parts of original output along the first axis (axis=0 or dim=0)
- 返回
The modified callable function
- 返回类型
Callable[..., Any]