qrunch.chemistry.qrackle.matrix_modules.unrestricted_matrix_module

Unrestricted (alpha/beta) matrix wrapper and composite matrix module.

Classes

UnrestrictedMatrix

A pair of matrices representing alpha and beta spin components.

UnrestrictedMatrixModule

Matrix module operating on alpha/beta spin pairs.

UnrestrictedVector

A pair of vectors representing alpha and beta spin components.

class UnrestrictedMatrix

Bases: Generic[TMatrix]

A pair of matrices representing alpha and beta spin components.

__init__(alpha: TMatrix, beta: TMatrix) → None
Parameters:
  • alpha (TMatrix)

  • beta (TMatrix)

Return type:

None

alpha: TMatrix
beta: TMatrix
fill_from_raw_numpy_arrays(alpha_array: NDArray[float64], beta_array: NDArray[float64]) → None

Fill both spin components from raw numpy arrays.

Parameters:
  • alpha_array (NDArray[float64]) – Two-dimensional array for the alpha component.

  • beta_array (NDArray[float64]) – Two-dimensional array for the beta component.

Return type:

None

property shape: tuple[int, int]

Shape of each spin component (both must match).

to_raw_numpy_arrays() → tuple[NDArray[float64], NDArray[float64]]

Tuple with the contents of both spin components as raw numpy arrays.

Return type:

tuple[NDArray[float64], NDArray[float64]]

class UnrestrictedMatrixModule

Bases: SCFMatrixModuleProtocol[UnrestrictedMatrix[TMatrix], UnrestrictedVector[TVector], TMatrix, TVector]

Matrix module operating on alpha/beta spin pairs.

Each operation is delegated independently to both spin components via the underlying scalar MatrixModuleProtocol. This class mirrors the MatrixModuleProtocol interface with UnrestrictedMatrix and UnrestrictedVector types, so the SCF driver can treat restricted and unrestricted calculations uniformly.

__init__(matrix_module: MatrixModuleProtocol[TMatrix, TVector]) → None

Initialise the unrestricted module.

Parameters:

matrix_module (MatrixModuleProtocol[TMatrix, TVector]) – Underlying module used for each spin component.

Return type:

None

add(left: UnrestrictedMatrix[TMatrix], right: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate the spin-component-wise sum.

Parameters:
Return type:

UnrestrictedMatrix[TMatrix]

allocation_report() → str

Get a human-readable summary of all live allocations.

Return type:

str

antisymmetrize(matrix: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate the spin-component-wise antisymmetric projection \(\tfrac{1}{2}(M_{\sigma} - M_{\sigma}^{T})\).

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted square matrix to be antisymmetrized.

Return type:

UnrestrictedMatrix[TMatrix]

commutator(left: UnrestrictedMatrix[TMatrix], right: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate the spin-component-wise commutator \([L, R] = L R - R L\).

Parameters:
Return type:

UnrestrictedMatrix[TMatrix]

copy_matrix(matrix: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Independent copy of both spin components.

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted matrix to be copied.

Return type:

UnrestrictedMatrix[TMatrix]

diagonalize(matrix: UnrestrictedMatrix[TMatrix]) → SCFEigenvalueSolution[UnrestrictedMatrix[TMatrix], UnrestrictedVector[TVector]]

Solve the symmetric eigenvalue problem spin-component-wise.

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted symmetric square matrix.

Return type:

SCFEigenvalueSolution[UnrestrictedMatrix[TMatrix], UnrestrictedVector[TVector]]

free_matrix(matrix: UnrestrictedMatrix[TMatrix]) → None

Free both spin components.

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted matrix to release.

Return type:

None

free_vector(vector: UnrestrictedVector[TVector]) → None

Free both spin components of a vector.

Parameters:

vector (UnrestrictedVector[TVector]) – Unrestricted vector to release.

Return type:

None

frobenius_inner_product(left: UnrestrictedMatrix[TMatrix], right: UnrestrictedMatrix[TMatrix]) → float

Calculate the combined Frobenius inner product \(\sum_{i,j} L_{\alpha,i,j} R_{\alpha,i,j} + L_{\beta,i,j} R_{\beta,i,j}\).

Parameters:
Return type:

float

frobenius_norm(matrix: UnrestrictedMatrix[TMatrix]) → float

Calculate the combined Frobenius norm \(\sqrt{\|M_{\alpha}\|_F^2 + \|M_{\beta}\|_F^2}\).

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted matrix.

Return type:

float

from_scalar_matrix(matrix: TMatrix) → UnrestrictedMatrix[TMatrix]

Wrap a scalar matrix into an unrestricted matrix with identical alpha and beta.

The matrix is copied so that the two spin components are independent.

Parameters:

matrix (TMatrix) – Scalar matrix to promote to both spin channels.

Return type:

UnrestrictedMatrix[TMatrix]

static from_spin_channels(channels: list[TMatrix]) → UnrestrictedMatrix[TMatrix]

Take ownership of alpha and beta matrices without copying.

Parameters:

channels (list[TMatrix]) – Two equally shaped scalar matrices, alpha then beta.

Return type:

UnrestrictedMatrix[TMatrix]

generalized_eigenvalue_solver(a_matrix: UnrestrictedMatrix[TMatrix], b_matrix: UnrestrictedMatrix[TMatrix]) → SCFEigenvalueSolution[UnrestrictedMatrix[TMatrix], UnrestrictedVector[TVector]]

Solve the generalized symmetric eigenvalue problem spin-component-wise.

Parameters:
  • a_matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted symmetric square matrix \(A\).

  • b_matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted symmetric positive-definite matrix \(B\).

Return type:

SCFEigenvalueSolution[UnrestrictedMatrix[TMatrix], UnrestrictedVector[TVector]]

gram_matrix(matrices: list[UnrestrictedMatrix[TMatrix]], other_matrices: list[UnrestrictedMatrix[TMatrix]] | None = None) → NDArray[float64]

Calculate Frobenius inner products \(G_{i,j} = \langle M_i, N_j \rangle_{F}\).

If other_matrices is omitted, use matrices for both lists and return a symmetric Gram matrix. Otherwise, return a matrix of shape (len(matrices), len(other_matrices)) without imposing symmetry.

Parameters:
  • matrices (list[UnrestrictedMatrix[TMatrix]]) – Unrestricted matrices defining the rows.

  • other_matrices (list[UnrestrictedMatrix[TMatrix]] | None) – Unrestricted matrices defining the columns, or None to reuse matrices. All input matrices must have identical shapes.

Return type:

NDArray[float64]

identity(dimension: int) → UnrestrictedMatrix[TMatrix]

Unrestricted identity matrix (identity in both components).

Parameters:

dimension (int) – Number of rows and columns for each component.

Return type:

UnrestrictedMatrix[TMatrix]

init_matrix(shape: tuple[int, int], *, zeroes: bool) → UnrestrictedMatrix[TMatrix]

Allocate an unrestricted matrix (alpha and beta).

Parameters:
  • shape (tuple[int, int]) – Desired shape for each spin component.

  • zeroes (bool) – If True both components are initialized to zero.

Return type:

UnrestrictedMatrix[TMatrix]

init_vector(length: int, *, zeroes: bool) → UnrestrictedVector[TVector]

Allocate an unrestricted vector (alpha and beta).

Parameters:
  • length (int) – Desired length for each spin component.

  • zeroes (bool) – If True both components are initialized to zero.

Return type:

UnrestrictedVector[TVector]

inverse(matrix: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate spin-component-wise inverse \(M_{\alpha}^{-1}, M_{\beta}^{-1}\).

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted matrix with non-singular components.

Return type:

UnrestrictedMatrix[TMatrix]

property is_restricted: Literal[False]

Whether the module is restricted, False if unrestricted.

linear_combination(coefficients: list[float], matrices: list[UnrestrictedMatrix[TMatrix]]) → UnrestrictedMatrix[TMatrix]

Calculate the linear combination \(\sum_i c_i M_i\) spin-component-wise.

Parameters:
  • coefficients (list[float]) – Real coefficients \(c_i\).

  • matrices (list[UnrestrictedMatrix[TMatrix]]) – Unrestricted matrices \(M_i\) of identical shape.

Return type:

UnrestrictedMatrix[TMatrix]

lowdin_orthogonalizer(overlap: UnrestrictedMatrix[TMatrix], threshold: float = 1e-09) → UnrestrictedMatrix[TMatrix]

Construct the symmetric (Lowdin) orthogonalizer \(X = S^{-1/2}\) spin-component-wise.

Parameters:
  • overlap (UnrestrictedMatrix[TMatrix]) – Unrestricted symmetric positive-definite overlap matrix \(S\).

  • threshold (float) – Eigenvalue cutoff for near-linear-dependence removal.

Return type:

UnrestrictedMatrix[TMatrix]

matrix_exponential(matrix: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate \(\exp(M_{\alpha})\) and \(\exp(M_{\beta})\) independently.

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted square matrix.

Return type:

UnrestrictedMatrix[TMatrix]

matrix_multiply(left: UnrestrictedMatrix[TMatrix], right: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate the spin-component-wise matrix product.

Parameters:
Return type:

UnrestrictedMatrix[TMatrix]

static matrix_spin_channels(matrix: UnrestrictedMatrix[TMatrix]) → list[TMatrix]

Borrow the alpha and beta scalar matrices without copying.

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted spin wrapper.

Return type:

list[TMatrix]

metadata_hash(matrix: UnrestrictedMatrix[TMatrix]) → str

Calculate a stable metadata hash for an unrestricted matrix.

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted matrix whose alpha and beta contents should be hashed.

Return type:

str

property number_of_live_allocations: int

Number of matrices and vectors currently held by the underlying module.

property scalar_module: MatrixModuleProtocol[TMatrix, TVector]

Underlying scalar matrix module.

scale(matrix: UnrestrictedMatrix[TMatrix], scalar: float) → UnrestrictedMatrix[TMatrix]

Scale both spin components by a scalar.

Parameters:
  • matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted matrix to scale.

  • scalar (float) – Scaling factor.

Return type:

UnrestrictedMatrix[TMatrix]

slice_columns(matrix: UnrestrictedMatrix[TMatrix], start_column_alpha: int, end_column_alpha: int, start_column_beta: int, end_column_beta: int) → UnrestrictedMatrix[TMatrix]

Create a new unrestricted matrix containing columns start_column to end_column (exclusive).

The slicing is applied independently to both spin components.

Parameters:
  • matrix (UnrestrictedMatrix[TMatrix]) – Source unrestricted matrix.

  • start_column_alpha (int) – First alpha column index (inclusive).

  • end_column_alpha (int) – Last alpha column index (exclusive).

  • start_column_beta (int) – First beta column index (inclusive).

  • end_column_beta (int) – Last beta column index (exclusive).

Return type:

UnrestrictedMatrix[TMatrix]

solve_linear_system(lhs_matrix: UnrestrictedMatrix[TMatrix], rhs_matrix: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Solve \(A X = B\) for \(X\) spin-component-wise.

Parameters:
  • lhs_matrix (UnrestrictedMatrix[TMatrix]) – Square non-singular unrestricted matrix \(A\).

  • rhs_matrix (UnrestrictedMatrix[TMatrix]) – Right-hand-side unrestricted matrix \(B\).

Return type:

UnrestrictedMatrix[TMatrix]

subtract(left: UnrestrictedMatrix[TMatrix], right: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate the spin-component-wise difference.

Parameters:
Return type:

UnrestrictedMatrix[TMatrix]

symmetrize(matrix: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate the spin-component-wise symmetric projection \(\tfrac{1}{2}(M_{\sigma} + M_{\sigma}^{T})\).

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted square matrix to be symmetrized.

Return type:

UnrestrictedMatrix[TMatrix]

property total_allocated_bytes: int

Total memory in bytes used by live allocations in the underlying module.

trace(matrix: UnrestrictedMatrix[TMatrix]) → float

Calculate the combined trace \(\mathrm{Tr}(M_{\alpha}) + \mathrm{Tr}(M_{\beta})\).

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted matrix.

Return type:

float

trace_of_product(left: UnrestrictedMatrix[TMatrix], right: UnrestrictedMatrix[TMatrix]) → float

Calculate the combined trace \(\mathrm{Tr}(L_{\alpha} R_{\alpha}) + \mathrm{Tr}(L_{\beta} R_{\beta})\).

Parameters:
Return type:

float

transpose(matrix: UnrestrictedMatrix[TMatrix]) → UnrestrictedMatrix[TMatrix]

Calculate the spin-component-wise transpose \(M_{\alpha}^{T}, M_{\beta}^{T}\).

Parameters:

matrix (UnrestrictedMatrix[TMatrix]) – Unrestricted matrix to be transposed.

Return type:

UnrestrictedMatrix[TMatrix]

class UnrestrictedVector

Bases: Generic[TVector]

A pair of vectors representing alpha and beta spin components.

__init__(alpha: TVector, beta: TVector) → None
Parameters:
  • alpha (TVector)

  • beta (TVector)

Return type:

None

alpha: TVector
beta: TVector
property length: int

Length of each spin component (both must match).