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,
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]
- count_systems(
- matrix: mat | arr,
- dim: int,
Count the number of
dim-dimensional subsystems that constitute the (composite) system on whichmatrixresides.
- count_dims(
- matrix: mat | arr,
- num_systems: int,
Compute the dimensionality of the (composite) system on which
matrixresides.
- check_systems_conflicts(
- *subsystems: list[int],
Check for conflicts (common element(s)) in the given (unpacked) tuple of lists. Returns
Trueif any are found, otherwiseFalse.
- adjust_targets(
- targets: list[int],
- removed: list[int],
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],
Arranges the elements of
itemsthe according to the respective locations (e.g., system indices) inpositions. The main use case would be to arrange gates in a multipartite system. The lengths of bothpositionsanditemsmust be the same, andpositionsmust not contain any missing system indices.Examples
>>> arrange([[0, 3], [1, 2]], ["a", "b"]) ['a', 'b', 'b', 'a']
- conjugate_transpose(
- matrix: mat | arr,
Compute the conjugate transpose of
matrix.
- to_density(
- vector: mat | arr,
Compute the outer product of
vectorwith itself, thereby converting any vector state into density matrix form. Leaves square matrices unaffected, and raises an error for non-square matrices.
- to_array(
- matrix: mat | arr | list[list[num | expr | str]],
- numerical: bool | None = None,
Converts
matrixto a NumPy array.
- to_matrix(
- matrix: mat | arr | list[list[num | expr | str]],
Converts
matrixto a SymPy matrix.
- cast(
- matrix: mat | arr | list[list[num | expr | str]],
- numerical: bool | None = None,
- array: bool | None = None,
Converts
matrixto either a NumPy array or SymPy matrix.
- to_numerical(
- expression: num | expr | str,
- numerical: bool | None = None,
Converts
expressionto a numerical value, if possible.
- generate_identity(
- size: int,
- numerical: bool | None = None,
- array: bool | None = None,
Construct an identity matrix as either a SymPy matrix or NumPy matrix.
- generate_zeros(
- size: int,
- numerical: bool | None = None,
- array: bool | None = None,
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,
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,
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],
Sympify the numerical, symbolic, or string expression pairs within tuples of the list
substitutionsand 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,
Simplify
expressionrecursively using the substitutions given insubstitutions. Runs untilexpressionis unchanged from the previous iteration, or until thelimitnumber of iterations is reached. IfcomprehensiveisFalse, 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]],
Make the substitutions as specified in
substitutionsto the givenmatrix.
- extract_matrix(
- operator: mat | arr | QuantumObject,
Extract the SymPy matrix from the
operatorobject.
- extract_array(
- operator: mat | arr | QuantumObject,
Extract the NumPy array from the
operatorobject.
- extract_representation(
- operator: mat | arr | QuantumObject,
Extract the matrix representation from the
operatorobject.
- extract_substitutions(
- *states,
Extract any substitution conditions accessible via the
substitutionsproperty from the objects instates.
- extract_symbols(
- *states,
Extract any SymPy symbols accessible via the
symbolsproperty from the objects instates.
- apply_function(
- matrix: mat | arr,
- function: Callable,
- arguments: list[Any] | None = None,
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]],
Change the default value of an argument in a subclass’s constructor. The argument
class_objectis the class whose__init__signature is to be targeted.
- fix_arguments(
- arguments,
- kwarguments,
- class_object,
- arg_pairs: list[tuple[str, Any]],
Fix the value of an argument in a subclass’s constructor. The argument
class_objectis the class whose__init__signature is to be targeted.