qsymm.linalg module

qsymm.linalg.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)[source]

Alternative to numpy.allclose to test that two ndarrays are elementwise close. Unlike the numpy version, the relative tolerance is not elementwise, but it is relative to the largest entry in the arrays.

qsymm.linalg.commutator(A, B)[source]
qsymm.linalg.family_to_vectors(family, all_keys=None)[source]

Convert a list of Model to standard vector representation.

Parameters:
  • family (iterable of Model objects) – The Model to convert to vector representation.

  • all_keys (iterable or None) – Iterable that must contain all the keys in the family. An optional parameter that allows to fix the ordering of the returned vectors to the same as in all_keys. Otherwise, the keys are ordered arbitrarily (all_keys = None).

Returns:

  • A matrix representing the family, in which the family members form the

  • rows.

qsymm.linalg.matrix_basis(dim, traceless=False, antihermitian=False, real=False, sparse=False)[source]

“Construct a basis for the vector space of dim x dim matrices, that may in addition be traceless, and/or composed of Hermitian or skew-Hermitian basis elements.

Parameters:
  • dim (positive integer) – The dimension of the matrices (dim x dim).

  • traceless (boolean) – Whether the vector space only includes traceless matrices (True) or not (False).

  • antihermitian (boolean) – Whether to use a basis composed of Hermitian matrices (False), or skew-Hermitian matrices (True).

  • real – Whether the basis only spans real matrices. Depending on ‘antihermitian’ it spans symmetric (False) or antisymmetric (True) matrices only.

  • sparse – If True, scipy.sparse.csr_matrix is returned, otherwise np.ndarray.

Returns:

basis – A generator that returns matrices that span the vector space.

Return type:

generator

qsymm.linalg.mtm(a, B, c)[source]
qsymm.linalg.nullspace(A, atol=1e-06, return_complement=False, sparse=None, k_max=-10)[source]

Compute an approximate basis for the nullspace of matrix A.

Parameters:
  • A (ndarray or spmatrix) – A should be 2-D.

  • atol (real) – Absolute tolerance when deciding whether an eigen/singular value is zero, so the corresponding vector is included in the null space.

  • return_complement (bool) – Whether to return the basis of the orthocomplement ot the null space as well. Treated as False if sparse is True.

  • sparse (bool or None (default)) – Whether to use sparse linear algebra to find the null space. The default value is inferred from the type of the input array.

  • k_max (int (default -10)) – If k_max<0, the full null-space is found by increasing the number of eigenvectors found in each step by abs(k_max). If k_max>0, k_max is the maximum number of null space vectors requested. If the null space dimension is higher than k_max, basis of a random subspace is returned. Can be used to increase performance if the maximum null space dimensionality is known. Ignored unless sparse is True.

Returns:

  • ns (ndarray) – Basis of the null space of A as row vectors.

  • nsc (ndarray) – Basis of the complement of the null space as row vectors, the othonormal basis of the row span of A. Only returned if return_complement=True and sparse=False.

qsymm.linalg.prop_to_id(A)[source]
qsymm.linalg.rref(m, rtol=0.001, return_S=False)[source]

Bring a matrix to reduced row echelon form.

Parameters:
  • m (2D numpy.ndarray) – The matrix on which to perform row reduction.

  • rtol (float) – The tolerance relative to the largest matrix element for what is considered a zero entry.

  • return_S (bool) – Whether to return the matrix of linear operations that brings the input matrix to reduced row echelon form.

Returns:

  • red (2D numpy.ndarray) – The input matrix in reduced row echelon form.

  • S (2D numpy.ndarray) – The matrix of operations that brings the input matrix to reduced row echelon form. Only returned if return_S = True.

  • Note (If r is the reduced row echelon form of the input matrix m, then) – m = S.dot(r).

qsymm.linalg.simult_diag(mats, tol=1e-06, checks=0)[source]

Simultaneously diagonalize commuting normal square matrices. Recursive algorithm, first diagonalize first matrix, express the rest of the matrices in its eigenbasis, and repeat the procedure for the rest of the matrices in each of the degenerate eigensubspaces. Recursion depth is limited by the number of nontrivial nested eigensubspaces, which is at most the number of matrices or the size of the matrices.

Parameters:
  • mats (ndarray) – List of commuting matrices, 3 index array.

  • tol (float) – Tolerance when deciding degeneracy of eigenvalues.

  • checks (int) – Amount of checks to do. 0: no checks 1: check the final result 2: check all the intermediate results

Returns:

  • Ps (list of ndarrays) – List of rectangular matrices of common eigenvectors spanning each of the common eigensubspaces. Stacking them gives unitary matrix ‘U = np.hstack(Ps)’ such that ‘U.T.conj() @ mats[i] @ U’ is diagonal for every ‘i’.

  • Notes

  • It is recommended to order matrices such that those with eigenvalues that

  • are far spaced or exactly degenerate (such as symmetries) are first, and

  • those which may have accidental near degeneracies (such as Hamiltonians)

  • are last.

  • Tolerance is divided by the size of the block when deciding which eigenvalues

  • are degenerate. The default value seems to work well, but not extensively

  • tested, numerical instabilities are possible.

qsymm.linalg.solve_mat_eqn(HL, HR=None, hermitian=False, traceless=False, conjugate=False, sparse=None, k_max=-10)[source]

Solve for X the simultaneous matrix equatioins X HL[i] = HR[i] X for every i. It is mapped to a system of linear equations, the null space of which gives a basis for all sulutions.

Parameters:
  • HL (ndarray or list of arrays or sparse matrices) – Coefficient matrices of identical square shape, list of arrays of shape (n, n) or one array of shape (m, n, n).

  • HR (ndarray or list of ndarrays or None) – Same as HL. If HR = None, HR = HL is used.

  • hermitian (booleans) – If X has to be hermitian, use hermitian = True. If X has to be traceless, use traceless = True.

  • traceless (booleans) – If X has to be hermitian, use hermitian = True. If X has to be traceless, use traceless = True.

  • conjugate (boolean or list of booleans) – If True, solve X HL[i] = HR[i] X^* instead. If a list with the same length as HL and HR is provided, conjugation is applied to the equations with index corresponding to the True entries.

  • sparse (bool or None (default)) – Whether to use sparse linear algebra to find the null space. The default value is inferred from the type of the input array.

  • k_max (int) – If k_max<0, all solutions are found by increasing the number of soulutions sought in each step by abs(k_max). If k_max>0, k_max is the maximum number of solutions requested. If the solution space dimension is higher than k_max, basis of a random subspace is returned. Can be used to increase performance if the maximum number of solutions is known. Ignored unless sparse is True.

Returns:

List of linearly independent square matrices that span all solutions of the eaquations.

Return type:

ndarray of shape (l, n, n), or list of csr_matrix

qsymm.linalg.sparse_basis(bas, num_digits=3, reals=False)[source]

Reduce the number of nonzero entries in a (basis) matrix by rounding the matrix and bringing it to reduced row echelon form.

Parameters:
  • bas (numpy.ndarray) – The (basis) matrix to prettify and round.

  • num_digits (positive integer) – Number of significant digits to which to round.

  • reals (boolean) – If True, the real and imaginary parts of the matrix are treated separately.

Returns:

  • A rounded, sparsified matrix with the same row span as the input matrix.

  • This is an attempt at matrix sparsification, i.e. we attempt to construct

  • the sparsest matrix that has the same row rank. Note that the reduction to

  • row echelon form is numerically unstable, so this function should be used

  • with caution.

qsymm.linalg.split_list(vals, tol=1e-06)[source]