qrunch.chemistry.qrackle.matrix_modules.numpy_matrix_module

Numpy implementation of the MatrixModuleProtocol.

Classes

NumpyMatrix

Numpy implementation of MatrixProtocol.

NumpyMatrixModule

Dense numpy implementation of MatrixModuleProtocol.

NumpyVector

Numpy implementation of VectorProtocol.

class NumpyMatrix

Bases: MatrixProtocol

Numpy implementation of MatrixProtocol.

__init__(data: NDArray[float64]) → None
Parameters:

data (NDArray[float64])

Return type:

None

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

Fill the matrix with data from a raw numpy array.

Parameters:

array (NDArray[float64]) – Two-dimensional numpy array with the correct shape.

Return type:

None

property shape: tuple[int, int]

Shape of the matrix.

to_raw_numpy_array() → NDArray[float64]

Contents of the matrix as a dense raw numpy array.

Return type:

NDArray[float64]

class NumpyMatrixModule

Bases: MatrixModuleProtocol[NumpyMatrix, NumpyVector]

Dense numpy implementation of MatrixModuleProtocol.

The module also keeps track of every matrix and vector it allocates and can report the currently live objects together with the allocation call-site. Memory tracking is automatic; allocation-stack capture can be disabled to avoid the per-allocation traceback cost in tight loops.

__init__(*, track_allocation_stack: bool = True, stack_capture_depth: int = 12) → None

Initialise the module and its allocation tracker.

Parameters:
  • track_allocation_stack (bool) – When True (default) capture a Python call-stack snapshot at every allocation. Set to False to only count allocations and bytes.

  • stack_capture_depth (int) – Maximum number of stack frames captured per allocation when track_allocation_stack is enabled.

Return type:

None

add(left: NumpyMatrix, right: NumpyMatrix) → NumpyMatrix

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

Parameters:
Return type:

NumpyMatrix

allocation_report() → str

Get a human-readable, multi-line summary of all live allocations.

The summary includes per-object shape, byte count and (when enabled) the call stack captured at the allocation site.

Return type:

str

antisymmetrize(matrix: NumpyMatrix) → NumpyMatrix

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

Parameters:

matrix (NumpyMatrix) – Square matrix to be antisymmetrized.

Return type:

NumpyMatrix

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

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

Parameters:
Return type:

NumpyMatrix

copy_matrix(matrix: NumpyMatrix) → NumpyMatrix

Create an independent copy of matrix.

Parameters:

matrix (NumpyMatrix) – Matrix to copy.

Return type:

NumpyMatrix

diagonalize(matrix: NumpyMatrix) → EigenvalueSolution[NumpyMatrix, NumpyVector]

Solve \(A v = \\lambda v\) for a symmetric matrix.

Parameters:

matrix (NumpyMatrix) – Symmetric square matrix.

Return type:

EigenvalueSolution[NumpyMatrix, NumpyVector]

free_matrix(matrix: NumpyMatrix) → None

Release the bookkeeping entry for matrix.

For the numpy backend the actual memory is owned by the underlying numpy.ndarray and is reclaimed by the Python garbage collector once no Python reference remains. This method removes the module-level reference held for allocation tracking.

Parameters:

matrix (NumpyMatrix) – Matrix previously returned by this module.

Return type:

None

free_vector(vector: NumpyVector) → None

Release the bookkeeping entry for vector.

See free_matrix() for semantics.

Parameters:

vector (NumpyVector) – Vector previously returned by this module.

Return type:

None

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

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

Parameters:
Return type:

float

frobenius_norm(matrix: NumpyMatrix) → float

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

Parameters:

matrix (NumpyMatrix) – Matrix whose Frobenius norm is requested.

Return type:

float

generalized_eigenvalue_solver(a_matrix: NumpyMatrix, b_matrix: NumpyMatrix) → EigenvalueSolution[NumpyMatrix, NumpyVector]

Solve \(A v = \\lambda B v\) for symmetric \(A\) and SPD \(B\).

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

  • b_matrix (NumpyMatrix) – Symmetric positive-definite square matrix \(B\).

Return type:

EigenvalueSolution[NumpyMatrix, NumpyVector]

identity(dimension: int) → NumpyMatrix

Get the square identity matrix of the given dimension.

Parameters:

dimension (int) – Number of rows and columns.

Return type:

NumpyMatrix

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

Allocate a new dense numpy matrix.

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

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

Return type:

NumpyMatrix

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

Allocate a new dense numpy vector.

Parameters:
  • length (int) – Desired number of elements.

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

Return type:

NumpyVector

inverse(matrix: NumpyMatrix) → NumpyMatrix

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

Parameters:

matrix (NumpyMatrix) – Square non-singular matrix.

Return type:

NumpyMatrix

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

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

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

  • matrices (list[NumpyMatrix]) – Matrices of identical shape; must be non-empty and of the same length as coefficients.

Return type:

NumpyMatrix

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

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

Eigenvectors whose overlap eigenvalue is below threshold are dropped, projecting out near-linear-dependent directions.

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

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

Return type:

NumpyMatrix

matrix_exponential(matrix: NumpyMatrix) → NumpyMatrix

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

Parameters:

matrix (NumpyMatrix) – Square matrix.

Return type:

NumpyMatrix

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

Calculate the matrix product \(L R\).

Parameters:
Return type:

NumpyMatrix

metadata_hash(matrix: NumpyMatrix) → str

Calculate a stable metadata hash for a matrix.

Parameters:

matrix (NumpyMatrix) – Matrix whose contents should be hashed.

Return type:

str

new_matrix(array: NDArray[float64]) → NumpyMatrix

Allocate an independently owned matrix containing a copy of the input array.

Parameters:

array (NDArray[float64]) – Two-dimensional numpy array whose shape and contents are copied.

Return type:

NumpyMatrix

property number_of_live_allocations: int

Number of currently tracked (not yet freed) objects.

resized_copy(matrix: NumpyMatrix, *, shape: tuple[int, int]) → NumpyMatrix

Create a new matrix of the given shape holding the overlapping top-left block of matrix.

Parameters:
  • matrix (NumpyMatrix) – Source matrix.

  • shape (tuple[int, int]) – Shape of the result as (rows, columns).

Return type:

NumpyMatrix

scale(matrix: NumpyMatrix, scalar: float) → NumpyMatrix

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

Parameters:
  • matrix (NumpyMatrix) – Matrix to scale.

  • scalar (float) – Real scaling factor.

Return type:

NumpyMatrix

shifted_floored_divide(numerator: NumpyMatrix, denominator: NumpyMatrix, *, shift: float, denominator_floor: float) → NumpyMatrix

Divide by shifted, sign-preserving floored denominators without modifying the inputs.

Parameters:
  • numerator (NumpyMatrix) – Numerator matrix.

  • denominator (NumpyMatrix) – Denominator matrix of identical shape.

  • shift (float) – Finite scalar subtracted from every denominator.

  • denominator_floor (float) – Finite positive minimum denominator magnitude.

Return type:

NumpyMatrix

slice_columns(matrix: NumpyMatrix, start_column: int, end_column: int) → NumpyMatrix

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

Parameters:
  • matrix (NumpyMatrix) – Source matrix.

  • start_column (int) – First column index (inclusive).

  • end_column (int) – Last column index (exclusive).

Return type:

NumpyMatrix

solve_linear_system(lhs_matrix: NumpyMatrix, rhs_matrix: NumpyMatrix) → NumpyMatrix

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

Parameters:
  • lhs_matrix (NumpyMatrix) – Square non-singular matrix \(A\).

  • rhs_matrix (NumpyMatrix) – Right-hand-side matrix \(B\).

Return type:

NumpyMatrix

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

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

Parameters:
Return type:

NumpyMatrix

symmetrize(matrix: NumpyMatrix) → NumpyMatrix

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

Parameters:

matrix (NumpyMatrix) – Square matrix to be symmetrized.

Return type:

NumpyMatrix

property total_allocated_bytes: int

Total number of bytes used by currently tracked objects.

trace(matrix: NumpyMatrix) → float

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

Parameters:

matrix (NumpyMatrix) – Square matrix.

Return type:

float

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

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

Parameters:
Return type:

float

transpose(matrix: NumpyMatrix) → NumpyMatrix

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

Parameters:

matrix (NumpyMatrix) – Matrix to transpose.

Return type:

NumpyMatrix

vector_outer_difference(left: NumpyVector, right: NumpyVector) → NumpyMatrix

Create the matrix with elements \(L_i - R_j\).

Parameters:
Return type:

NumpyMatrix

class NumpyVector

Bases: VectorProtocol

Numpy implementation of VectorProtocol.

__init__(data: NDArray[float64]) → None
Parameters:

data (NDArray[float64])

Return type:

None

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

Fill the vector with data from a raw numpy array.

Parameters:

array (NDArray[float64]) – One-dimensional numpy array with length elements.

Return type:

None

property length: int

Number of elements of the vector.

to_raw_numpy_array() → NDArray[float64]

Contents of the vector as a raw numpy array.

Return type:

NDArray[float64]