tensorcircuit.utils¶

Helper functions

tensorcircuit.utils.append(f: Callable[[...], Any], *op: Callable[[...], Any]) Any[source]¶

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)
Parameters:
  • f (Callable[..., Any]) – The function which are attached with other functions

  • op (Callable[..., Any]) – Function to be attached

Returns:

The final results after function pipeline

Return type:

Any

tensorcircuit.utils.arg_alias(f: Callable[[...], Any], alias_dict: Dict[str, str | Sequence[str]], fix_doc: bool = True) Callable[[...], Any][source]¶

function argument alias decorator with new docstring

Parameters:
  • 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

Returns:

the decorated function

Return type:

Callable[…, Any]

tensorcircuit.utils.benchmark(f: Any, *args: Any, tries: int = 5, verbose: bool = True) Tuple[Any, float, float][source]¶

benchmark jittable function with staging time and running time

Parameters:
  • f (Any) – _description_

  • tries (int, optional) – _description_, defaults to 5

  • verbose (bool, optional) – _description_, defaults to True

Returns:

_description_

Return type:

Tuple[Any, float, float]

tensorcircuit.utils.gpu_memory_share(flag: bool = True) None[source]¶

Set the GPU memory growth mode

Parameters:

flag (bool) – whether to set the GPU memory growth mode, defaults to True

Returns:

None

tensorcircuit.utils.is_m1mac() bool[source]¶

check whether the running platform is MAC with M1 chip

Returns:

True for MAC M1 platform

Return type:

bool

tensorcircuit.utils.is_number(x: Any) bool[source]¶
tensorcircuit.utils.is_sequence(x: Any) bool[source]¶
tensorcircuit.utils.return_partial(f: Callable[[...], Any], return_argnums: int | Sequence[int] = 0) Callable[[...], Any][source]¶

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]))
Parameters:
  • f (Callable[..., Any]) – The function to be applied this method

  • return_argnums (Union[int, Sequence[int]]) – The ith parts of original output along the first axis (axis=0 or dim=0)

Returns:

The modified callable function

Return type:

Callable[…, Any]