Skip to content

sci_solvers.energy_calculation_base

Base class for sci_solver implementations.

Defines the qiskit-addon-sqd compatible sci_solver_callable interface that all solvers in this package implement. Each concrete subclass wraps a different diagonalizer (RIKEN's sbd binary, qiskit-addon-dice-solver, PySCF's selected_ci) and exposes a uniform per-batch dispatch interface.

The single contract is::

sci_solver_callable(ci_strs_batches, hcore, eri, norb, nelec, **kwargs)
    -> List[SCIResult]

where ci_strs_batches is a list of (strs_a, strs_b) int64-array tuples — one batch per element — and the returned SCIResult.energy values are ELECTRONIC (no nuclear repulsion). Adding e_nuc is the caller's responsibility, matching qiskit-addon-sqd's convention.

Subclasses MAY accept additional optional keyword arguments beyond the protocol minimum (e.g. max_cycle, symmetrize_spin, spin_sq); those are the per-solver knobs documented on each concrete solver class.

EnergyCalculatorBase [source]

EnergyCalculatorBase(*, symmetrize_spin: bool, hamiltonian: HamiltonianData | str | PathLike[str] | None = None, verbose: bool = False, nroots: int = 1, **kwargs: Any)

Bases: ABC

Abstract base class for sci_solver_callable wrappers.

Concrete subclasses implement :meth:sci_solver_callable to diagonalize a Hamiltonian on the subspace spanned by each input batch's (strs_a, strs_b) outer product.

Args: hamiltonian: Optional HamiltonianData instance. If provided in this constructor you may not override it in sci_solver_callable to avoid ambiguity. you can provide either: - a HamiltonianData instance directly, or - a path to an FCIDUMP file (as a string or Path) from which the HamiltonianData instance will be created If None (the default), then no Hamiltonian is stored/"bound" to this class instance. symmetrize_spin: verbose: Print solver-specific progress / debug output. nroots: Number of lowest roots (states) to solve for per batch. Stored as self.nroots for the subclasses to use and implement it. **kwargs: Forwarded up the super().__init__.

compute_energy

compute_energy(ci_strs_batches: List[Tuple[ndarray, ndarray]] | None = None, bitstring_matrix_batches: List[ndarray] | None = None, sort_ci_strs: bool = True, **solver_call_kwargs: Any) -> List[SCIResult]

Compute energies for each batch of CI strs.

Note - to get from bitstrings into ci_strs you can use:

from qiskit_addon_sqd.fermion import bitstring_matrix_to_ci_strs
ci_strs = bitstring_matrix_to_ci_strs(bitstrings_to_use, open_shell=False) #or open_shell==True, depending on your case

Parameters:

Name Type Description Default
ci_strs_batches List[Tuple[ndarray, ndarray]] | None

List of (strs_a, strs_b) int64-array tuples. Provide exactly one of this argument or bitstring_matrix_batches.

None
bitstring_matrix_batches List[ndarray] | None

List of boolean NumPy matrices, one per batch. Provide exactly one of this argument or ci_strs_batches.

None
sort_ci_strs bool

Sort and deduplicate each batch's alpha/beta CI strings with np.unique before solving. Defaults to True so solvers that require strictly ascending strings (the general-algebra family) work out of the box; the bitstring path is already sorted, so this is a no-op there. Pair-mode (mode="gdb") solvers are exempt: their (strs_a, strs_b) are parallel (alpha_i, beta_i) lists, so sorting each one independently would break the pairing (they dedup/sort the pair list themselves). Pass False to forward the strings exactly as given.

True
**solver_call_kwargs Any

Additional kwargs forwarded to sci_solver_callable.

{}

sci_solver_callable_eigen_states abstractmethod

sci_solver_callable_eigen_states(ci_strs_batches: 'List[Tuple[np.ndarray, np.ndarray]] | List[np.ndarray]', hcore: ndarray, eri: ndarray, norb: int, nelec: 'Tuple[int, int] | int', **kwargs: Any) -> List[EigenStates]

Diagonalize the Hamiltonian on each batch's CI-string subspace.

This is the multi-root primitive every concrete solver implements. It returns the lowest eigenpairs of each batch (one or more roots). :meth:sci_solver_callable wraps this and hands qiskit-addon-sqd the ground state per batch, so single-root callers never touch this method directly.

Concrete implementations should call super().sci_solver_callable_eigen_states(...) first to run the bound-Hamiltonian ambiguity guard (below).

Note - to get from bitstrings into ci_strs you can use:

from qiskit_addon_sqd.fermion import bitstring_matrix_to_ci_strs
ci_strs = bitstring_matrix_to_ci_strs(bitstrings_to_use, open_shell=False) #or open_shell==True, depending on your case

Two reduction paths, dispatched on nelec's type (mirrors GeneralAlgebraSolverBase/TorchSCISolver, the two families that implement both):

  • spatial — nelec an (n_a, n_b) tuple: ci_strs_batches is a list of (strs_a, strs_b) tuples, each defining a subspace via the outer product of unique alpha and beta CI strings.
  • spinless (flat spin-orbital, no alpha/beta split — GHF / SOC / generalized-spinor Hamiltonians) — nelec a scalar total particle number N: ci_strs_batches is a list of flat strs arrays, one N-particle spin-orbital determinant bitmask list per subspace. Not every solver implements this path — a solver that doesn't should raise NotImplementedError promptly (see e.g. PySCFSolver, DICESolver, SBDSolver, DavidsonSolver, TruncatedDavidsonSolver) rather than let a bare tuple-unpack or index crash with an unrelated-looking TypeError/ValueError deep inside the implementation.

Parameters:

Name Type Description Default
ci_strs_batches 'List[Tuple[np.ndarray, np.ndarray]] | List[np.ndarray]'

List of (strs_a, strs_b) tuples (spatial), or a list of flat strs int64 arrays (spinless — see above). Each spatial tuple defines a subspace via the outer product of unique alpha and beta CI strings (int64 arrays where bit i of each integer indicates occupancy of orbital i).

required
hcore ndarray

One-electron integrals, shape (norb, norb).

required
eri ndarray

Two-electron repulsion integrals. Either full 4-D (norb,)*4 or a packed form (4-fold / 8-fold) — what's accepted depends on the concrete solver.

required
norb int

Number of spatial orbitals (spin-orbitals nmo on the spinless path).

required
nelec 'Tuple[int, int] | int'

(n_alpha, n_beta) (spatial), or a scalar total N (spinless — see above; not every solver implements this).

required
**kwargs Any

Solver-specific optional arguments. See each concrete solver for the accepted set.

{}

Returns:

Name Type Description
One List[EigenStates]

class:~sci_solvers.eigen_states.EigenStates per input batch,

List[EigenStates]

in input order. Each holds one :class:SCIResult per solved root,

List[EigenStates]

ascending in energy (states[0] is the ground state). Single-root

List[EigenStates]

solvers return an EigenStates of length one. energy fields

List[EigenStates]

are electronic only; callers add e_nuc themselves to obtain

List[EigenStates]

chemical totals.

Raises:

Type Description
NotImplementedError

nelec is a scalar (the spinless path) and this solver does not implement it.

sci_solver_callable

sci_solver_callable(ci_strs_batches: 'List[Tuple[np.ndarray, np.ndarray]] | List[np.ndarray]', hcore: ndarray, eri: ndarray, norb: int, nelec: 'Tuple[int, int] | int', **kwargs: Any) -> List[SCIResult]

Diagonalize each batch's CI-string subspace, returning ground states.

The qiskit-addon-sqd entry point: matches the sci_solver signature expected by qiskit_addon_sqd.fermion.diagonalize_fermionic_hamiltonian. Thin wrapper over :meth:sci_solver_callable_eigen_states that keeps each batch's ground state (root 0) and discards the excited states, since the SQD recovery loop consumes only the ground state. To access excited states, call :meth:sci_solver_callable_eigen_states directly.

Note - to get from bitstrings into ci_strs you can use:

from qiskit_addon_sqd.fermion import bitstring_matrix_to_ci_strs
ci_strs = bitstring_matrix_to_ci_strs(bitstrings_to_use, open_shell=False) #or open_shell==True, depending on your case

Parameters:

Name Type Description Default
ci_strs_batches 'List[Tuple[np.ndarray, np.ndarray]] | List[np.ndarray]'

List of (strs_a, strs_b) tuples (spatial), or a list of flat strs arrays (spinless — a scalar nelec; see :meth:sci_solver_callable_eigen_states). Each spatial tuple defines a subspace via the outer product of unique alpha and beta CI strings (int64 arrays where bit i of each integer indicates occupancy of orbital i).

required
hcore ndarray

One-electron integrals, shape (norb, norb).

required
eri ndarray

Two-electron repulsion integrals. Either full 4-D (norb,)*4 or a packed form (4-fold / 8-fold) — what's accepted depends on the concrete solver.

required
norb int

Number of spatial orbitals.

required
nelec 'Tuple[int, int] | int'

(n_alpha, n_beta) (spatial), or a scalar total N (spinless — not every solver implements this; see :meth:sci_solver_callable_eigen_states).

required
**kwargs Any

Solver-specific optional arguments. See each concrete solver for the accepted set.

{}

Returns:

Name Type Description
One List[SCIResult]

class:SCIResult (the ground state) per input batch, in input

List[SCIResult]

order. energy fields are electronic only; callers add e_nuc

List[SCIResult]

themselves to obtain chemical totals.

project_init_vector

project_init_vector(prev_state: Any, ci_strs: 'Tuple[np.ndarray, np.ndarray] | np.ndarray', mode: str = 'tpb', *, min_norm: float = 1e-08, device: Any = 'cpu') -> 'np.ndarray | None'

Project a previous solve's state onto a new basis, as an init_vectors entry.

Warm starting an SQD iteration from the previous one means mapping the previous ground state onto this iteration's strings, which drop some and add others. This builds the entry for one batch: amplitudes copied on the determinants both bases share, zero on the new ones, normalized. It is laid out exactly as init_vectors expects in each mode, so a caller never handles orderings or sign conventions. That holds because every mode's init_vectors contract uses the convention that mode's own sci_state.amplitudes come back in (TPB spin-block, GDB interleaved-Wick), so amplitudes are copied, never re-signed.

Parameters:

Name Type Description Default
prev_state Any

the previous result's sci_state, from the same solver mode: a SCIState (TPB, (M_a, M_b) amplitudes), a PairSCIState (GDB, 1-D amplitudes over parallel ci_strs_a/ci_strs_b) or a SpinlessSCIState (1-D amplitudes over ci_strs).

required
ci_strs 'Tuple[np.ndarray, np.ndarray] | np.ndarray'

the new batch's strings, as passed to the solver: (strs_a, strs_b) in TPB and GDB, a flat strs array on the spinless path. Any order; duplicates take the first occurrence, as the solver does.

required
mode str

"tpb" or "gdb" (the solver's mode). Ignored for a spinless state, which is recognized by its ci_strs field. Taken explicitly rather than inferred from the amplitudes' shape, which is ambiguous for a single-determinant subspace.

'tpb'
min_norm float

overlap below which the bases share nothing worth starting from.

1e-08
device Any

torch device for the key lookup, a sort over every determinant. CPU takes ~3.4 s per 10⁷ GDB pairs; pass the solve device (e.g. "cuda") at 10⁸. The result is always a CPU numpy array.

'cpu'

Returns:

Type Description
'np.ndarray | None'

The 1-D entry, or None (cold start) when the overlap norm is below

'np.ndarray | None'

min_norm, e.g. for disjoint bases.

Raises:

Type Description
ValueError

mode is not "tpb"/"gdb", or ci_strs does not have the shape that mode takes.