Skip to content

sci_solvers.energy_solvers.pyscf_solver

PySCF-based sci_solver implementation.

Thin wrapper around pyscf.fci.selected_ci.kernel_fixed_space, with a convergence signal surfaced on every result and multi-root support.

Single-root path (n_roots == 1)

Calls qiskit_addon_sqd.fermion.solve_sci per batch via :meth:PySCFSolver._solve_one_with_convergence, which monkey-patches SelectedCI to capture the Davidson converged flag that solve_sci would otherwise discard. Returns :class:~sci_solvers.eigen_states.SCIResultWithConvergence so callers can check result.state.

Multi-root path (n_roots > 1)

qiskit_addon_sqd cannot be used because its solve_sci is hard-wired for a single root. The module-level :func:solve_sci_batch_multiroot helper calls kernel_fixed_space directly with nroots > 1, iterates over the returned list of CI vectors to build one :class:~sci_solvers.eigen_states.SCIResultWithConvergence per root, and reads myci.converged (available because we own the myci instance) to populate state on each root.

PySCFSolver [source]

PySCFSolver(*, symmetrize_spin: bool, hamiltonian: HamiltonianData | str | PathLike[str] | None = None, verbose: bool = False, nroots: int = 1, n_roots: int = 1, spin_shift: float = 0.2, with_rdm2: bool = True, **kwargs: Any)

Bases: EnergyCalculatorBase

sci_solver wrapping PySCF's selected-CI kernel.

Suitable for small-to-medium subspaces on CPU. Multithreaded via BLAS; supports spin projection via spin_sq.

Multi-root support: pass n_roots > 1 to solve for excited states in addition to the ground state. When n_roots == 1 (default) the solver uses :meth:_solve_one_with_convergence per batch (capturing PySCF's Davidson converged flag). When n_roots > 1 it calls kernel_fixed_space directly via :func:solve_sci_batch_multiroot. All results are :class:~sci_solvers.eigen_states.SCIResultWithConvergence — read the convergence state from result.state, and excited states from states[1:] of each per-batch :class:~sci_solvers.eigen_states.EigenStates.

Initialize the PySCFSolver.

Parameters:

Name Type Description Default
symmetrize_spin bool

Passed to the base class.

required
hamiltonian HamiltonianData | str | PathLike[str] | None

Optional bound Hamiltonian (see base class).

None
verbose bool

Print solver progress.

False
nroots int

Number of lowest eigenpairs to solve for per batch. Default 1 (ground state only, backward-compatible). When > 1, each per-batch :class:~sci_solvers.eigen_states.EigenStates holds one :class:~sci_solvers.eigen_states.SCIResultWithConvergence per root, ascending in energy — read excited states from states[1:].

1
n_roots int

Backward-compatible alias for nroots. Pass one or the other, not both (a non-default value on each raises).

1
spin_shift float

Penalty strength forwarded to fix_spin_ when spin_sq is set at call time. More significant for n_roots > 1 (needs to push all wrong-spin states above all target-spin roots), but accepted on both paths.

0.2
with_rdm2 bool

Whether to retain the 2-RDM in each result. Only relevant for n_roots > 1; the single-root path (via solve_sci) always populates rdm2.

True

sci_solver_callable_eigen_states

sci_solver_callable_eigen_states(ci_strs_batches: List[Tuple[ndarray, ndarray]] | List[ndarray], hcore: ndarray, eri: ndarray, norb: int, nelec: Tuple[int, int] | int, *, spin_sq: float | None = None, max_cycle: int = 200, davidson_block_size: int | None = None, **kwargs: Any) -> List[EigenStates]

Diagonalize each batch via PySCF's selected_ci kernel.

When n_roots == 1 calls :meth:_solve_one_with_convergence per batch, which captures PySCF's Davidson converged flag. When n_roots > 1 calls kernel_fixed_space directly via :func:solve_sci_batch_multiroot.

Parameters:

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

List of (strs_a, strs_b) int64-array tuples.

required
hcore ndarray

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

required
eri ndarray

Two-electron repulsion integrals, shape (norb,)*4.

required
norb int

Number of spatial orbitals.

required
nelec Tuple[int, int] | int

(n_alpha, n_beta). A scalar nelec (the spinless flat spin-orbital path) is rejected with NotImplementedError — this solver does not implement it; see :func:~sci_solvers.energy_calculation_base.reject_spinless_nelec.

required
spin_sq float | None

Target value for total spin squared (S²).

None
max_cycle int

Flat Davidson iteration budget — total matvec cycles across all restarts (PySCF's Davidson is one flat loop; a restart is an internal flag, not an outer loop). Kept distinct from the nested solvers' max_davidson_iters, for which max_cycle ~= max_davidson_iters * davidson_block_size.

200
davidson_block_size int | None

Davidson subspace size — vectors accumulated before a restart, carrying the same meaning as on TorchSCISolver / SBDSolver. PySCF widens its own max_space by (nroots-1)*4, so that widening is subtracted here and the effective subspace equals what you ask for. None (default) selects 6*nroots + 8, i.e. PySCF's historical 12 + 2*nroots restated in this unit, leaving the value PySCF receives unchanged. Pass this instead of max_space, which is rejected.

None
**kwargs Any

Additional kwargs forwarded to the underlying PySCF call — e.g. tol, lindep, max_memory.

{}

Returns:

Name Type Description
One List[EigenStates]

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

List[EigenStates]

input order. Each holds one

List[EigenStates]

class:~sci_solvers.eigen_states.SCIResultWithConvergence per

List[EigenStates]

solved root, ascending in energy: states[0] is the ground

List[EigenStates]

state and states[1:] are the excited states (present only when

List[EigenStates]

n_roots > 1). Energies are electronic only.

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

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.