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: MutableDenseMatrix,
dim: int,
) int[source]#

Count the number of dim-dimensional subsystems which constitute the (composite) system represented by matrix.

count_dims(
matrix: MutableDenseMatrix,
systems: list[int],
) int[source]#

Compute the dimensionality of the (composite) system represented by matrix.

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']
to_density(
vector: MutableDenseMatrix,
) MutableDenseMatrix[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: MutableDenseMatrix,
) MutableDenseMatrix[source]#

Transpose vector into its column form.

stringify(
matrix: MutableDenseMatrix,
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: MutableDenseMatrix | ndarray | Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol | str,
symbols: dict[MatrixSymbol | MatrixElement | Symbol | str, dict[str, Any]] | list[MatrixSymbol | MatrixElement | Symbol] | None = None,
) MutableDenseMatrix | ndarray | Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol[source]#

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

symbolize_tuples(
conditions: list[tuple[Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol | str, Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol | str]],
symbols_list: list[MatrixSymbol | MatrixElement | Symbol],
) list[tuple[Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol, Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol]][source]#

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

recursively_simplify(
expression: MutableDenseMatrix | ndarray | Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol,
conditions: list[tuple[Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol, Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol]] | None = None,
limit: int | None = None,
comprehensive: bool | None = None,
) MutableDenseMatrix | ndarray | Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol[source]#

Simplify expression recursively using the substitutions given in conditions. 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.

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

Extract the SymPy matrix from the operator object.

extract_conditions(
*states,
) list[tuple[Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol, Number | generic | Basic | MatrixSymbol | MatrixElement | Symbol]][source]#

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

extract_symbols(
*states,
) list[MatrixSymbol | MatrixElement | Symbol][source]#

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

apply_function(
matrix: MutableDenseMatrix,
function: Callable,
arguments: list[Any] | None = None,
) MutableDenseMatrix[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 * Dagger(matrix) = Dagger(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. 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.

assemble_composition(
*pairs: tuple[MutableDenseMatrix, list[int]],
) MutableDenseMatrix[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.