Helpers#

This module contains general helper functions.

It is not intended to be used directly by the user.

from qhronology.utilities import helpers

General helper functions. Not intended to be used directly by the user.

flatten_list(
nested_list: list,
) list[source]#

Flatten a list of any nesting depth and structure, e.g.:

Examples

>>> flatten_list([1, [2, [3, [4]]]])
[1, 2, 3, 4]
>>> flatten_list([[1], [2], [3], [4]])
[1, 2, 3, 4]
list_depth(
nested_list: list,
) int[source]#

Compute the depth of a (nested) list.

count_systems(
matrix: mat | arr,
dim: int,
) int[source]#

Count the number of dim-dimensional subsystems that constitute the (composite) system on which matrix resides.

count_dims(
matrix: mat | arr,
num_systems: int,
) int[source]#

Compute the dimensionality of the (composite) system on which matrix resides.

check_systems_conflicts(
*subsystems: list[int],
) bool[source]#

Check for conflicts (common element(s)) in the given (unpacked) tuple of lists. Returns True if any are found, otherwise False.

adjust_targets(
targets: list[int],
removed: list[int],
) list[int][source]#

Adjust the specified system indices (targets) according to those which have been removed (removed) from the total set.

arrange(
positions: list[list[int]],
items: list[Any],
) list[Any][source]#

Arranges the elements of items the according to the respective locations (e.g., system indices) in positions. The main use case would be to arrange gates in a multipartite system. The lengths of both positions and items must be the same, and positions must not contain any missing system indices.

Examples

>>> arrange([[0, 3], [1, 2]], ["a", "b"])
['a', 'b', 'b', 'a']
conjugate_transpose(
matrix: mat | arr,
) mat | arr[source]#

Compute the conjugate transpose of matrix.

dtype(
matrix: mat | arr,
) type[source]#

Determine the numerical data type (dtype) of matrix.

to_density(
vector: mat | arr,
) mat | arr[source]#

Compute the outer product of vector with itself, thereby converting any vector state into density matrix form. Leaves square matrices unaffected, and raises an error for non-square matrices.

to_column(
vector: mat | arr,
) mat | arr[source]#

Transpose vector into its column form.

to_array(
matrix: mat | arr | list[list[num | expr | str]],
numerical: bool | None = None,
) arr[source]#

Converts matrix to a NumPy array.

to_matrix(
matrix: mat | arr | list[list[num | expr | str]],
) mat[source]#

Converts matrix to a SymPy matrix.

cast(
matrix: mat | arr | list[list[num | expr | str]],
numerical: bool | None = None,
array: bool | None = None,
) mat | arr[source]#

Converts matrix to either a NumPy array or SymPy matrix.

to_numerical(
expression: num | expr | str,
numerical: bool | None = None,
) num | expr | str[source]#

Converts expression to a numerical value, if possible.

generate_identity(
size: int,
numerical: bool | None = None,
array: bool | None = None,
) mat | arr[source]#

Construct an identity matrix as either a SymPy matrix or NumPy matrix.

generate_zeros(
size: int,
numerical: bool | None = None,
array: bool | None = None,
) mat | arr[source]#

Construct a matrix or zeros as either a SymPy matrix or NumPy matrix.

stringify(
matrix: mat | arr,
dim: int,
delimiter: str | None = None,
product: bool | None = None,
) str[source]#

Render the mathematical expression (as a string) of the given matrix.

symbolize_expression(
expression: mat | arr | num | expr | str,
symbols: dict[sym | str, dict[str, Any]] | list[sym] | None = None,
) mat | arr | num | expr[source]#

Sympify a numerical, symbolic, or string expression, and replace the symbols with given counterparts.

symbolize_substitutions(
substitutions: list[tuple[num | expr | str, num | expr | str]],
symbols_list: list[sym],
) list[tuple[num | expr, num | expr]][source]#

Sympify the numerical, symbolic, or string expression pairs within tuples of the list substitutions and replace the symbols with given counterparts.

recursively_simplify(
expression: mat | arr | num | expr,
substitutions: list[tuple[num | expr, num | expr]] | None = None,
limit: int | None = None,
comprehensive: bool | None = None,
) mat | arr | num | expr[source]#

Simplify expression recursively using the substitutions given in substitutions. Runs until expression is unchanged from the previous iteration, or until the limit number of iterations is reached. If comprehensive is False, the algorithm uses a relatively efficient subset of simplifying operations, otherwise it uses a larger, more powerful (but slower) set.

apply_substitutions(
matrix: mat | arr,
substitutions: list[tuple[num | expr | str, num | expr | str]],
) mat | arr[source]#

Make the substitutions as specified in substitutions to the given matrix.

extract_matrix(
operator: mat | arr | QuantumObject,
) mat[source]#

Extract the SymPy matrix from the operator object.

extract_array(
operator: mat | arr | QuantumObject,
) arr[source]#

Extract the NumPy array from the operator object.

extract_representation(
operator: mat | arr | QuantumObject,
) mat | arr[source]#

Extract the matrix representation from the operator object.

extract_substitutions(
*states,
) list[tuple[num | expr, num | expr]][source]#

Extract any substitution conditions accessible via the substitutions property from the objects in states.

extract_symbols(
*states,
) list[TypeAliasForwardRef('sym')][source]#

Extract any SymPy symbols accessible via the symbols property from the objects in states.

apply_function(
matrix: mat | arr,
function: Callable,
arguments: list[Any] | None = None,
) mat | arr[source]#

Applies a function to a matrix. This is accomplished using eigendecomposition, in which the specified matrix is assumed to be normal (i.e., matrix * conjugate_transpose(matrix) = conjugate_transpose(matrix) * matrix, which holds true for density operators).

default_arguments(
arguments,
kwarguments,
class_object,
arg_pairs: list[tuple[str, Any]],
)[source]#

Change the default value of an argument in a subclass’s constructor. The argument class_object is the class whose __init__ signature is to be targeted.

fix_arguments(
arguments,
kwarguments,
class_object,
arg_pairs: list[tuple[str, Any]],
)[source]#

Fix the value of an argument in a subclass’s constructor. The argument class_object is the class whose __init__ signature is to be targeted.

tensor_product(
*matrices: mat | arr,
) mat | arr[source]#

Compute the tensor (or Kronecker) product of the items in matrices.

assemble_composition(
*pairs: tuple[mat | arr, list[int]],
) mat | arr[source]#

Assemble a composite state from constituent subsystems described by the items in pairs. For each pair: - The first element is the subsystem’s state matrix. - The second element is the list of indices of its systems.