torch-sci-solver¶
A native PyTorch/Triton eigensolver for selected configuration interaction (SCI) and sample-based quantum diagonalization (SQD). It supports CPU, CUDA and Apple MPS, with TPB, GDB and spinless bases, real or complex integrals, and multiple roots. The Hamiltonian-vector product builds on RIKEN's SBD implementation.
Main features¶
- Distributed solving: one subspace across GPUs
- Independent batches on multiple GPUs
- TPB and GDB bases
- Complex and spinless Hamiltonians
- Spin penalties and measurements
- Excited states, warm starts and RDMs
See the engine API
for signatures, types and option defaults, and the
solver guide for the sci-solvers integration.
Install¶
From the sci-solvers repository:
uv sync
For a standalone installation with SQD integration:
uv pip install 'torch-sci-solver[sqd]'
Usage¶
make_torch_sci_solver returns a callable engine. The examples below assume
Hamiltonian integrals hcore, eri, orbital count norb, electron counts
nelec=(n_alpha, n_beta) and CI-string batches are loaded.
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda")
results = solver([(strs_a, strs_b)], hcore, eri, norb, nelec)
electronic_energy = results[0].states[0].energy
Each batch returns an EigenStates container with roots in ascending energy.
Energies are electronic; add the nuclear repulsion for total energies.
| Setting | GPU work |
|---|---|
device="cuda" |
One GPU per solve. |
distributed={...} |
Multiple GPUs cooperate on one subspace. |
devices=["cuda:0", "cuda:1"] |
GPUs diagonalize independent batches. |
Distributed GPU diagonalization (one subspace across GPUs)¶
Davidson vectors and matrix-vector work are partitioned across ranks. Integrals and some lookup data remain replicated. TPB, GDB and spinless bases support this path with real or complex integrals and single or multiple roots.
Include the following in solve.py, alongside your input loading, and launch with
torchrun --standalone --nproc-per-node=8 solve.py:
import torch
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(
mode="tpb",
dtype=torch.float32,
davidson_dtype=torch.float32,
davidson_block_size=4,
distributed={"world_size": 8, "backend": "nccl"},
)
results = solver([(strs_a, strs_b)], hcore, eri, norb, nelec)
The solver uses the launcher's ranks; rank 0 receives the results. For GDB, set
mode="gdb" and preserve the alpha/beta pairing of each determinant.
See the capacity and timing summary
and distributed guide for memory settings
and checkpointing. distributed and devices are mutually exclusive.
Multi-GPU independent batches¶
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(devices=["cuda:0", "cuda:1"])
results = solver(ci_strings, hcore, eri, norb, nelec)
solver.close()
Each subspace must fit on one GPU. The worker pool is reused across calls and
released by close(). This setting does not increase the capacity of a single solve.
Precision¶
dtype controls Hamiltonian arithmetic and defaults to torch.float64.
davidson_dtype controls Davidson storage and defaults to the arithmetic dtype.
Set both to torch.float32 for full FP32; use complex128 or complex64 for
complex Hermitian integrals. Lower precision reduces vector storage but changes
numerical accuracy and attainable residuals.
To keep FP64 arithmetic with FP32 Davidson storage:
import torch
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda", dtype=torch.float64, davidson_dtype=torch.float32)
Tuning and memory¶
davidson_block_sizelimits the Davidson subspace before restarting;Nonesizes it automatically. Smaller values reduce vector storage.chunk_sizebounds batched matrix-vector work.gdb_kwargs={"budget_bytes": ...}bounds GDB enumeration workspace.memory_budget_bytesguides automatic workspace and Davidson sizing.
These budgets do not cap total GPU or host memory. See the distributed guide for memory guidance and benchmark sizes.
matvec_impl="auto" selects the supported backend for the device and dtype.
"triton" requests CUDA kernels; "torch" selects portable PyTorch throughout,
including enumeration and connectivity.
Short feature examples¶
GPU acceleration¶
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda")
Apple MPS¶
MPS requires FP32 or complex64 and supports single-device solving.
import torch
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="mps", dtype=torch.float32)
TPB — tensor product basis¶
Independent alpha and beta string sets define a Cartesian product of dimension
len(strs_a) * len(strs_b).
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda", mode="tpb")
results = solver([(strs_a, strs_b)], hcore, eri, norb, nelec)
GDB — general determinant basis¶
Parallel arrays define one determinant (strs_a[i], strs_b[i]) per row. Preserve
this pairing; TPB's bitstring_matrix_to_ci_strs conversion is unsuitable for GDB.
GDB amplitudes are flat, with one entry per unique pair.
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda", mode="gdb")
results = solver([(strs_a, strs_b)], hcore, eri, norb, nelec)
Complex integrals¶
Complex integrals require a complex solver dtype. Non-Hermitian Hamiltonians are rejected.
import torch
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda", dtype=torch.complex128)
Spinless Hamiltonian¶
Pass flat determinant arrays, spin-orbital integrals and a scalar particle count. Spin penalties and S² measurements require an alpha/beta partition and are unavailable on this path.
import torch
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda", dtype=torch.complex128)
results = solver([strs], hcore, eri, nmo, n_total)
⟨S²⟩ penalty¶
spin_sq targets a spin sector through a penalty. The returned energy excludes
the penalty contribution.
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda")
results = solver(ci_strings, hcore, eri, norb, nelec, spin_sq=0.0)
Measure ⟨S²⟩¶
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda", compute_spin_squared=True)
results = solver(ci_strings, hcore, eri, norb, nelec)
spin_squared = solver.stats["spin_squared"]
Excited states¶
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda", n_roots=4, davidson_block_size=20)
results = solver(ci_strings, hcore, eri, norb, nelec)
energies = [root.energy for root in results[0].states]
Every root carries amplitudes and occupancies. In TPB/GDB, full RDMs, S² diagnostics and warm-start snapshots are ground-state only. The default missed-root check checks warm multi-root solves for lower eigenstates that the initial vectors missed.
Warm start¶
Pass one initial vector per batch, indexed by the determinants in the new basis.
None selects a cold start. See the
warm-start guide
for projecting a previous result onto a changed basis.
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda")
results = solver(ci_strings, hcore, eri, norb, nelec, init_vectors=[previous_amplitudes])
1-RDM and 2-RDM¶
Full RDMs require additional work and memory and are disabled by default.
from torch_sci_solver import make_torch_sci_solver
solver = make_torch_sci_solver(device="cuda", compute_full_rdm=True)
results = solver(ci_strings, hcore, eri, norb, nelec)
rdm1, rdm2 = results[0].states[0].rdm1, results[0].states[0].rdm2
Tests¶
uv run pytest packages/torch-sci-solver/tests
GPU tests skip when the device is unavailable. torch-sci-solver-smoke checks
CPU/CUDA agreement on the bundled molecular data.