qrunch.chemistry.qrackle.matrix_modules.scf_matrix_module_protocol

Protocol for SCF matrix modules.

Defines a common interface that both RestrictedMatrixModule and UnrestrictedMatrixModule satisfy, so the SCF driver can be written generically over the spin treatment.

The protocol is parameterized by two type variables:

  • TSCFMatrix — the “spin matrix” type (RestrictedMatrix[TMatrix] or UnrestrictedMatrix[TMatrix]).

  • TSCFVector — the “spin vector” type (RestrictedVector[TVector] or UnrestrictedVector[TVector]).

Classes

SCFEigenvalueSolution

Solution of a symmetric eigenvalue problem at the SCF (spin-wrapped) level.

SCFMatrixModuleProtocol

Protocol for matrix modules used by the SCF driver.

SCFMatrixProtocol

Structural interface satisfied by both spin-matrix wrapper types.

class SCFEigenvalueSolution

Bases: Generic[TSCFMatrix, TSCFVector]

Solution of a symmetric eigenvalue problem at the SCF (spin-wrapped) level.

Unlike EigenvalueSolution, the type variables here are unbounded so that both restricted and unrestricted wrapper types can be used.

__init__(eigenvalues: TSCFVector, eigenvectors: TSCFMatrix) → None
Parameters:
  • eigenvalues (TSCFVector)

  • eigenvectors (TSCFMatrix)

Return type:

None

eigenvalues: TSCFVector

Spin vector of eigenvalues, sorted in ascending order.

eigenvectors: TSCFMatrix

Spin matrix whose columns are the corresponding eigenvectors.

class SCFMatrixModuleProtocol

Bases: Protocol[TSCFMatrix, TSCFVector, TMatrix, TVector]

Protocol for matrix modules used by the SCF driver.

Both RestrictedMatrixModule and UnrestrictedMatrixModule satisfy this protocol, allowing the SCF loop to be written generically.

__init__(*args, **kwargs)
add(left: TSCFMatrix, right: TSCFMatrix) → TSCFMatrix

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

Parameters:
  • left (TSCFMatrix) – First summand.

  • right (TSCFMatrix) – Second summand.

Return type:

TSCFMatrix

allocation_report() → str

Get a human-readable summary of all live allocations.

Return type:

str

antisymmetrize(matrix: TSCFMatrix) → TSCFMatrix

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

Parameters:

matrix (TSCFMatrix) – Square spin matrix to be antisymmetrized.

Return type:

TSCFMatrix

commutator(left: TSCFMatrix, right: TSCFMatrix) → TSCFMatrix

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

Parameters:
  • left (TSCFMatrix) – Left operand.

  • right (TSCFMatrix) – Right operand.

Return type:

TSCFMatrix

copy_matrix(matrix: TSCFMatrix) → TSCFMatrix

Independent copy.

Parameters:

matrix (TSCFMatrix) – Spin matrix to be copied.

Return type:

TSCFMatrix

diagonalize(matrix: TSCFMatrix) → SCFEigenvalueSolution[TSCFMatrix, TSCFVector]

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

Parameters:

matrix (TSCFMatrix) – Symmetric square spin matrix.

Return type:

SCFEigenvalueSolution[TSCFMatrix, TSCFVector]

free_matrix(matrix: TSCFMatrix) → None

Free the matrix.

Parameters:

matrix (TSCFMatrix) – Spin matrix to release.

Return type:

None

free_vector(vector: TSCFVector) → None

Free the vector.

Parameters:

vector (TSCFVector) – Spin vector to release.

Return type:

None

frobenius_inner_product(left: TSCFMatrix, right: TSCFMatrix) → float

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

Parameters:
  • left (TSCFMatrix) – First matrix.

  • right (TSCFMatrix) – Second matrix of identical shape.

Return type:

float

frobenius_norm(matrix: TSCFMatrix) → float

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

Parameters:

matrix (TSCFMatrix) – Spin matrix.

Return type:

float

from_scalar_matrix(matrix: TMatrix) → TSCFMatrix

Wrap a spin-channel-independent scalar matrix into a spin matrix.

For restricted modules this wraps the matrix directly. For unrestricted modules the matrix is copied into both alpha and beta components.

Parameters:

matrix (TMatrix) – Scalar matrix to promote to the spin-matrix type.

Return type:

TSCFMatrix

from_spin_channels(channels: list[TMatrix]) → TSCFMatrix

Wrap scalar matrices without copying, in restricted or alpha/beta order.

The returned wrapper takes ownership of the supplied matrices. Restricted modules require one channel; unrestricted modules require two equally shaped channels.

Parameters:

channels (list[TMatrix]) – Scalar matrices to transfer to the spin wrapper.

Return type:

TSCFMatrix

generalized_eigenvalue_solver(a_matrix: TSCFMatrix, b_matrix: TSCFMatrix) → SCFEigenvalueSolution[TSCFMatrix, TSCFVector]

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

Parameters:
  • a_matrix (TSCFMatrix) – Symmetric square spin matrix \(A\).

  • b_matrix (TSCFMatrix) – Symmetric positive-definite spin matrix \(B\).

Return type:

SCFEigenvalueSolution[TSCFMatrix, TSCFVector]

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

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

Return type:

NDArray[float64]

identity(dimension: int) → TSCFMatrix

Get the spin identity matrix of the given dimension.

Parameters:

dimension (int) – Number of rows and columns.

Return type:

TSCFMatrix

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

Allocate a spin matrix.

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

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

Return type:

TSCFMatrix

init_vector(length: int, *, zeroes: bool) → TSCFVector

Allocate a vector.

Parameters:
  • length (int) – Desired length.

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

Return type:

TSCFVector

property is_restricted: bool

Whether the module is restricted, False if unrestricted.

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

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

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

  • matrices (list[TSCFMatrix]) – Spin matrices \(M_i\) of identical shape.

Return type:

TSCFMatrix

lowdin_orthogonalizer(overlap: TSCFMatrix, threshold: float = 1e-09) → TSCFMatrix

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

Parameters:
  • overlap (TSCFMatrix) – Symmetric positive-definite overlap matrix \(S\).

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

Return type:

TSCFMatrix

matrix_exponential(matrix: TSCFMatrix) → TSCFMatrix

Calculate \(\exp(M)\) independently for each spin channel.

The input is unchanged and the result owns independent storage.

Parameters:

matrix (TSCFMatrix) – Square spin matrix.

Return type:

TSCFMatrix

matrix_multiply(left: TSCFMatrix, right: TSCFMatrix) → TSCFMatrix

Calculate the matrix product \(L R\).

Parameters:
  • left (TSCFMatrix) – Left factor.

  • right (TSCFMatrix) – Right factor.

Return type:

TSCFMatrix

matrix_spin_channels(matrix: TSCFMatrix) → list[TMatrix]

Borrow the scalar matrices without copying, in restricted or alpha/beta order.

The matrices remain owned by the spin wrapper and must not be freed separately.

Parameters:

matrix (TSCFMatrix) – Spin matrix whose channels are requested.

Return type:

list[TMatrix]

metadata_hash(matrix: TSCFMatrix) → str

Calculate a stable metadata hash for a spin matrix.

Parameters:

matrix (TSCFMatrix) – Spin 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: TSCFMatrix, scalar: float) → TSCFMatrix

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

Parameters:
  • matrix (TSCFMatrix) – Spin matrix to scale.

  • scalar (float) – Scaling factor.

Return type:

TSCFMatrix

slice_columns(matrix: TSCFMatrix, start_column_alpha: int, end_column_alpha: int, start_column_beta: int, end_column_beta: int) → TSCFMatrix

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

The slicing is applied independently to each spin component.

Parameters:
  • matrix (TSCFMatrix) – Source spin 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:

TSCFMatrix

subtract(left: TSCFMatrix, right: TSCFMatrix) → TSCFMatrix

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

Parameters:
  • left (TSCFMatrix) – Minuend.

  • right (TSCFMatrix) – Subtrahend.

Return type:

TSCFMatrix

symmetrize(matrix: TSCFMatrix) → TSCFMatrix

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

Parameters:

matrix (TSCFMatrix) – Square spin matrix to be symmetrized.

Return type:

TSCFMatrix

property total_allocated_bytes: int

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

trace(matrix: TSCFMatrix) → float

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

Parameters:

matrix (TSCFMatrix) – Spin matrix.

Return type:

float

trace_of_product(left: TSCFMatrix, right: TSCFMatrix) → float

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

Parameters:
  • left (TSCFMatrix) – Left factor.

  • right (TSCFMatrix) – Right factor.

Return type:

float

transpose(matrix: TSCFMatrix) → TSCFMatrix

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

Parameters:

matrix (TSCFMatrix) – Spin matrix to be transposed.

Return type:

TSCFMatrix

class SCFMatrixProtocol

Bases: Protocol

Structural interface satisfied by both spin-matrix wrapper types.

Both RestrictedMatrix and UnrestrictedMatrix expose a shape; this protocol lets generic SCF code query it without depending on the concrete wrapper type.

__init__(*args, **kwargs)
property shape: tuple[int, int]

Shape of the matrix as (number_of_rows, number_of_columns).