qrunch.chemistry.qrackle.matrix_modules.restricted_matrix_module

Restricted (closed-shell) matrix wrapper and composite matrix module.

Classes

RestrictedMatrix

A single matrix representing a closed-shell (restricted) quantity.

RestrictedMatrixModule

Matrix module operating on restricted (closed-shell) matrices.

RestrictedVector

A single vector representing a closed-shell (restricted) quantity.

class RestrictedMatrix

Bases: Generic[TMatrix]

A single matrix representing a closed-shell (restricted) quantity.

__init__(data: TMatrix) → None
Parameters:

data (TMatrix)

Return type:

None

data: TMatrix
fill_from_raw_numpy_array(array: NDArray[float64]) → None

Fill the matrix from a raw numpy array.

Parameters:

array (NDArray[float64]) – Two-dimensional array matching the matrix shape.

Return type:

None

property shape: tuple[int, int]

Shape of the matrix.

to_raw_numpy_array() → NDArray[float64]

Contents as a raw numpy array.

Return type:

NDArray[float64]

class RestrictedMatrixModule

Bases: SCFMatrixModuleProtocol[RestrictedMatrix[TMatrix], RestrictedVector[TVector], TMatrix, TVector]

Matrix module operating on restricted (closed-shell) matrices.

Each operation is delegated to the underlying MatrixModuleProtocol. This class mirrors the interface of UnrestrictedMatrixModule so the SCF driver can treat restricted and unrestricted calculations uniformly.

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

Initialise the restricted module.

Parameters:

matrix_module (MatrixModuleProtocol[TMatrix, TVector]) – Underlying module used for matrix operations.

Return type:

None

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

Calculate the element-wise sum \(L + R\).

Parameters:
Return type:

RestrictedMatrix[TMatrix]

allocation_report() → str

Get a human-readable summary of all live allocations.

Return type:

str

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

Calculate the antisymmetric projection \(\tfrac{1}{2}(M - M^{T})\).

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted square matrix to be antisymmetrized.

Return type:

RestrictedMatrix[TMatrix]

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

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

Parameters:
Return type:

RestrictedMatrix[TMatrix]

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

Create an independent copy.

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted matrix to be copied.

Return type:

RestrictedMatrix[TMatrix]

diagonalize(matrix: RestrictedMatrix[TMatrix]) → SCFEigenvalueSolution[RestrictedMatrix[TMatrix], RestrictedVector[TVector]]

Solve the symmetric eigenvalue problem \(A v = \lambda v\).

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted symmetric square matrix.

Return type:

SCFEigenvalueSolution[RestrictedMatrix[TMatrix], RestrictedVector[TVector]]

free_matrix(matrix: RestrictedMatrix[TMatrix]) → None

Free the matrix.

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted matrix to release.

Return type:

None

free_vector(vector: RestrictedVector[TVector]) → None

Free the vector.

Parameters:

vector (RestrictedVector[TVector]) – Restricted vector to release.

Return type:

None

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

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

Parameters:
Return type:

float

frobenius_norm(matrix: RestrictedMatrix[TMatrix]) → float

Calculate the Frobenius norm \(\| M \|_{F} = \sqrt{\sum_{i,j} M_{i,j}^{2}}\).

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted matrix.

Return type:

float

from_scalar_matrix(matrix: TMatrix) → RestrictedMatrix[TMatrix]

Wrap a scalar matrix into a restricted matrix.

Parameters:

matrix (TMatrix) – Scalar matrix to wrap.

Return type:

RestrictedMatrix[TMatrix]

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

Take ownership of one scalar matrix without copying.

Parameters:

channels (list[TMatrix]) – List containing the restricted scalar matrix.

Return type:

RestrictedMatrix[TMatrix]

generalized_eigenvalue_solver(a_matrix: RestrictedMatrix[TMatrix], b_matrix: RestrictedMatrix[TMatrix]) → SCFEigenvalueSolution[RestrictedMatrix[TMatrix], RestrictedVector[TVector]]

Solve the generalized symmetric eigenvalue problem \(A v = \lambda B v\).

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

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

Return type:

SCFEigenvalueSolution[RestrictedMatrix[TMatrix], RestrictedVector[TVector]]

gram_matrix(matrices: list[RestrictedMatrix[TMatrix]], other_matrices: list[RestrictedMatrix[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[RestrictedMatrix[TMatrix]]) – Restricted matrices defining the rows.

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

Return type:

NDArray[float64]

identity(dimension: int) → RestrictedMatrix[TMatrix]

Get the restricted identity matrix of the given dimension.

Parameters:

dimension (int) – Number of rows and columns.

Return type:

RestrictedMatrix[TMatrix]

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

Allocate a restricted matrix.

Parameters:
  • shape (tuple[int, int]) – Desired shape.

  • zeroes (bool) – If True the matrix is initialized to zero.

Return type:

RestrictedMatrix[TMatrix]

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

Allocate a restricted vector.

Parameters:
  • length (int) – Desired length.

  • zeroes (bool) – If True the vector is initialized to zero.

Return type:

RestrictedVector[TVector]

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

Calculate the inverse \(M^{-1}\).

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted non-singular matrix.

Return type:

RestrictedMatrix[TMatrix]

property is_restricted: Literal[True]

Whether the module is restricted, False if unrestricted.

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

Calculate the linear combination \(\sum_i c_i M_i\).

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

  • matrices (list[RestrictedMatrix[TMatrix]]) – Restricted matrices \(M_i\) of identical shape.

Return type:

RestrictedMatrix[TMatrix]

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

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

Parameters:
  • overlap (RestrictedMatrix[TMatrix]) – Symmetric positive-definite overlap matrix \(S\).

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

Return type:

RestrictedMatrix[TMatrix]

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

Calculate the matrix exponential \(\exp(M)\).

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted square matrix.

Return type:

RestrictedMatrix[TMatrix]

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

Calculate the matrix product \(L R\).

Parameters:
Return type:

RestrictedMatrix[TMatrix]

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

Borrow the restricted scalar matrix without copying.

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted spin wrapper.

Return type:

list[TMatrix]

metadata_hash(matrix: RestrictedMatrix[TMatrix]) → str

Calculate a stable metadata hash for a restricted matrix.

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted matrix whose 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: RestrictedMatrix[TMatrix], scalar: float) → RestrictedMatrix[TMatrix]

Scale the matrix by a scalar \(cM\).

Parameters:
  • matrix (RestrictedMatrix[TMatrix]) – Restricted matrix to scale.

  • scalar (float) – Scaling factor.

Return type:

RestrictedMatrix[TMatrix]

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

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

Parameters:
  • matrix (RestrictedMatrix[TMatrix]) – Source restricted 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:

RestrictedMatrix[TMatrix]

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

Solve \(A X = B\) for \(X\).

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

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

Return type:

RestrictedMatrix[TMatrix]

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

Calculate the element-wise difference \(L - R\).

Parameters:
Return type:

RestrictedMatrix[TMatrix]

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

Calculate the symmetric projection \(\tfrac{1}{2}(M + M^{T})\).

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted square matrix to be symmetrized.

Return type:

RestrictedMatrix[TMatrix]

property total_allocated_bytes: int

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

trace(matrix: RestrictedMatrix[TMatrix]) → float

Calculate the trace \(\mathrm{Tr}(M) = \sum_{i} M_{i,i}\).

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted matrix.

Return type:

float

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

Calculate the trace \(\mathrm{Tr}(L R) = \sum_{i,k} L_{i,k} R_{k,i}\).

Parameters:
Return type:

float

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

Calculate the transpose \(M^{T}\).

Parameters:

matrix (RestrictedMatrix[TMatrix]) – Restricted matrix to be transposed.

Return type:

RestrictedMatrix[TMatrix]

class RestrictedVector

Bases: Generic[TVector]

A single vector representing a closed-shell (restricted) quantity.

__init__(data: TVector) → None
Parameters:

data (TVector)

Return type:

None

data: TVector
fill_from_raw_numpy_array(array: NDArray[float64]) → None

Fill the vector from a raw numpy array.

Parameters:

array (NDArray[float64]) – One-dimensional array matching the vector length.

Return type:

None

property length: int

Length of the vector.

to_raw_numpy_array() → NDArray[float64]

Contents as a raw numpy array.

Return type:

NDArray[float64]