TorchSCISolver¶
Our native PyTorch/Triton solver supports CPU, CUDA, Apple MPS and distributed solving, with TPB, GDB and spinless bases. Start with the feature examples. The wrapper API and engine API document the complete signatures and types.
Usage¶
from sci_solvers.energy_solvers.factory import create_solver
from sci_solvers.hamiltonian import HamiltonianData
ham = HamiltonianData.from_fcidump("FCIDUMP")
solver = create_solver(
"torch_sci",
symmetrize_spin=False,
hamiltonian=ham,
device="cuda",
mode="tpb",
davidson_block_size=20,
davidson_tolerance=1e-4,
)
results = solver.compute_energy(ci_strs_batches=[(strs_a, strs_b)])
total_energy = results[0].energy + ham.e_nuc
strs_a and strs_b are the input determinant strings. compute_energy returns
one ground-state result per batch; energies are electronic.
Main options¶
| Option | Meaning |
|---|---|
mode |
"tpb" (default) or "gdb". |
device |
Single-device "cpu", "cuda" or "mps". |
devices |
CUDA devices for independent batches. |
distributed |
Ranks cooperating on one subspace. |
nroots / n_roots |
Number of lowest eigenstates. |
davidson_block_size |
Davidson subspace size before restart; None sizes automatically. |
davidson_tolerance |
Residual tolerance; tol overrides it per call. |
max_davidson_iters |
Maximum Davidson restart cycles. |
dtype |
Hamiltonian arithmetic; default torch.float64. |
davidson_dtype |
Davidson storage; defaults to the arithmetic dtype. |
compute_full_rdm |
Build full RDMs; default False. |
compute_spin_squared |
Measure ground-state S². |
TPB vs GDB¶
- TPB: independent alpha/beta string sets form a Cartesian product.
Dimension:
len(strs_a) * len(strs_b). - GDB: parallel arrays contain determinant pairs.
Dimension: the number of unique
(strs_a[i], strs_b[i])pairs. - Spinless: flat determinant lists with a scalar electron count select the spin-orbital path automatically.
For GDB, preserve the original pairing. bitstring_matrix_to_ci_strs and
compute_energy(bitstring_matrix_batches=...) produce independent spin sets for
TPB. For a GDB SQD loop, use
torch_sci_solver.diagonalize_fermionic_hamiltonian_pairs.
TPB amplitudes have shape (n_alpha_strings, n_beta_strings); GDB and spinless
amplitudes are flat. Integer strings wider than 64 bits use multiple 64-bit limbs.
Backends¶
matvec_impl="auto" selects the supported backend for the device and dtype.
On NVIDIA CUDA it selects Triton for TPB, spinless and real FP32/FP64 GDB.
Complex GDB defaults to PyTorch. Automatic GDB selection falls back to PyTorch
when Triton is unavailable.
"torch" uses portable PyTorch for matrix-vector products, enumeration and
connectivity. "triton" requests supported CUDA kernels.
"batched" is identical to "torch" but deprecated.
Memory and diagnostics¶
davidson_block_size controls vector storage; chunk_size bounds batched work.
gdb_kwargs={"budget_bytes": ...} bounds enumeration scratch.
memory_budget_bytes guides workspace, automatic tiling and Davidson sizing.
These settings do not cap total process memory.
Use diagnostics={"matvec_profile": True} for phase timings. Full RDMs and spin
measurements add postprocessing work and memory.
Distributed diagonalization (one subspace across GPUs)¶
Use distributed for one large subspace and devices for independent batches.
The distributed guide shows a torchrun example,
checkpointing, memory budgets and measured capacity.
Precision and complex integrals¶
Set dtype=torch.float32 and davidson_dtype=torch.float32 for full FP32.
Set dtype=torch.float64 and davidson_dtype=torch.float32 to retain FP64
Hamiltonian arithmetic with smaller Davidson storage. Complex Hermitian integrals
require complex128 or complex64; non-Hermitian inputs are rejected.
davidson_fp32_shift="hf" shifts the operator to reduce FP32 cancellation.
davidson_fp32_energy="ritz" reports the shifted Ritz value; "rq64" evaluates an
FP64 Rayleigh quotient, requiring an extra product per root. "auto" uses Ritz
values at residual tolerances of at least 1e-3 and the Rayleigh quotient below it.
Lower precision changes attainable residuals; inspect convergence rather than
assuming FP64 tolerances remain reachable.
Apple MPS supports FP32 and complex64, with single-device solving. It does not support Triton or distributed process groups.
Excited states and spin¶
Set nroots=k and call sci_solver_callable_eigen_states to obtain all roots:
from sci_solvers.energy_solvers.factory import create_solver
from sci_solvers.hamiltonian import HamiltonianData
ham = HamiltonianData.from_fcidump("FCIDUMP")
solver = create_solver(
"torch_sci", symmetrize_spin=False, hamiltonian=ham, nroots=4, davidson_block_size=20
)
results = solver.sci_solver_callable_eigen_states(
[(strs_a, strs_b)],
ham.hcore,
ham.eri,
ham.num_orbitals,
(ham.num_elec_a, ham.num_elec_b),
)
energies = [root.energy for root in results[0].states]
The root count must fit the basis and Davidson subspace. spin_sq applies a native
spin penalty; reported energies exclude that penalty. apply_ab_projection
controls alpha–beta swap projection. Spin penalties and S² measurements are
unavailable for spinless inputs, which have no alpha/beta partition.
In TPB/GDB, full RDMs and spin diagnostics are ground-state only.
Collinear spinless bases reuse the pair engine¶
A flat basis whose determinants split at nmo // 2 into constant up/down half
occupations, under a Hamiltonian that conserves both half-spin counts (no
spin-flip one-/two-body terms), is exactly an (α, β) pair list over the two
halves. For those problems the solver auto-routes the subspace through the
GDB pair engine and converts the eigenvector back: the halves become
(α, β), the integrals are re-indexed from the [up | down] block layout onto
the pair engine's interleaved layout, and a per-determinant sign bridges the two
Slater-determinant conventions. This inherits the pair engine's deduplicated
same-spin excitation enumeration (once per unique string, not once per
determinant) and its Triton block kernels, which the flat enumerator does not
use.
The gate is conservative and silent on failure, so a genuinely spinless
(SOC / spin-flip) or non-collinear problem keeps the flat path unchanged. The
route is also skipped for compute_full_rdm and compute_spin_squared requests
and for caller init_vectors in this release; those fall back to the flat
engine. An internal per-process override (solver._spinless_collinear = "off",
used by the tests and the A/B benchmark) forces the flat path; it is not part of
the public API. The bridge lives in
torch_sci_solver.torch_impl.spinless_pair_bridge.
Warm start from a previous state (init_vectors)¶
Use project_init_vector to map a previous state's amplitudes onto the new basis,
preserving the mode's indexing and signs. It returns None when there is no overlap.
Pass one vector per batch; None starts that batch cold.
from sci_solvers.energy_calculation_base import project_init_vector
from sci_solvers.energy_solvers.factory import create_solver
solver = create_solver("torch_sci", symmetrize_spin=False, mode="gdb", device="cuda")
init_vectors = [
project_init_vector(previous_state, strings, "gdb", device="cuda")
for strings in ci_strs_batches
]
results = solver.sci_solver_callable_eigen_states(
ci_strs_batches,
hcore,
eri,
norb,
nelec,
init_vectors=init_vectors,
)
The inputs here are the previous sci_state, the new CI-string batches and the
Hamiltonian integrals. Multi-root starts can supply one vector per root.
The default missed-root check tests warm multi-root solves for lower eigenstates
outside the initial vectors' span. probe_method="warm_start" provides a separate
state-based interface for extending a basis; see the engine API for its contract.
Public interface: config, capabilities, preflight¶
solver.configreports requested and resolved settings.solver.capabilities()reports supported modes, dtypes and features.solver.preflight(ci_strings, nelec, norb=norb)validates inputs and returns the planned layout and memory estimates without running Davidson.
solver.last_call_subspace_stats() reports timings, matrix-vector counts and
warm-start status. Check each result's convergence flag before using its energy.