Skip to content

torch_sci_solver.torch_impl.pairs

Pair-basis (no-product-space) primitives for the selected-CI engine.

Product mode — the historical default — stores the wavefunction over the Cartesian product {(α_i, β_j) : i ∈ [0, M_α), j ∈ [0, M_β)} linearly indexed as k = i * M_β + j (dim = M_α * M_β). Pair mode instead stores the wavefunction over an explicit list of N (α, β) pairs (dim = N).

This module defines the data structures needed by the pair-mode diagonal and matvec kernels, plus the length-N initial-vector helper. The kernels themselves live next to their product-mode counterparts (mult.py, rdm.py, spin_sq.py) and consume a PairBasis where they would otherwise use (adets_t, bdets_t) and the Cartesian-product index math.

The pair basis exposes three views over the same N pairs:

  • alpha_of_pair[k], beta_of_pair[k] — the α- and β-strings (as int64 bitmasks) of pair k. Parallel arrays of length N.
  • unique_alphas, unique_betas — the sorted, deduplicated sets of α- and β-strings referenced by any pair. The existing per-spin excitation-table builder (sbd_ref.helpers.generate_excitation) is fed these directly.
  • alpha_uid_of_pair[k], beta_uid_of_pair[k] — the position of pair k's α- and β-strings in the unique arrays. This is the bridge that lets a per-spin excitation table (indexed by uid-in-unique-alphas) be re-projected onto pair space.

Two additional lookup structures are precomputed for the kernels:

  • partners_of_alpha (CSR) — for each unique α-string, the list of pair indices sharing that α. Used by α-only excitation kernels.
  • partners_of_beta (CSR) — the mirror for β. Used by β-only kernels.

Both are needed at kernel time to answer "given the ket α (or β) of an excitation, which pair indices in this basis are the matching bra?"

PairSCIState [source] dataclass

PairSCIState(amplitudes: 'np.ndarray', ci_strs_a: 'np.ndarray', ci_strs_b: 'np.ndarray', norb: int, nelec: 'tuple[int, int]')

Pair-mode analog of qiskit_addon_sqd.fermion.SCIState.

SCIState requires amplitudes.shape == (len(ci_strs_a), len(ci_strs_b)) — the Cartesian product layout. Pair mode's basis is a length-N list of (α, β) pairs; the eigenvector is a length-N 1D vector and the α- and β-string arrays are parallel length-N arrays. Storing that in an SCIState fails the 2D shape check, so pair mode returns this lookalike dataclass instead. Same public fields (amplitudes, ci_strs_a, ci_strs_b, norb, nelec); consumers reading them by name see equivalent data. Consumers that reshape into 2D would already have been wrong on a pair basis — they'll now fail at the type check rather than silently mis-index.

save

save(filename)

Save the PairSCIState object to an .npz file.

PairBasis [source] dataclass

PairBasis(alpha_of_pair: Tensor, beta_of_pair: Tensor, unique_alphas: Tensor, unique_betas: Tensor, alpha_uid_of_pair: Tensor, beta_uid_of_pair: Tensor, partners_of_alpha_offsets: Tensor, partners_of_alpha_pair_idx: Tensor, partners_of_alpha_beta_uid: Tensor, partners_of_beta_offsets: Tensor, partners_of_beta_pair_idx: Tensor, partners_of_beta_alpha_uid: Tensor)

An explicit list of (α, β) determinant pairs.

All tensors live on the same device and are int64. Pair indices run over [0, N) where N = alpha_of_pair.shape[0].

For bases with n_orb <= 64 the string fields are 1-D (N,) tensors. For wider bases (n_orb > 64) they are (N, n_limbs) limb matrices (limb k holds bits [64k, 64k+64)). The same applies to the unique string arrays: unique_alphas / unique_betas are either 1-D (M,) or 2-D (M, n_limbs).

Fields: alpha_of_pair, beta_of_pair: length-N int64 tensors of α- and β-string bitmasks (1-D or (N, n_limbs)). (alpha_of_pair[k], beta_of_pair[k]) is pair k. unique_alphas, unique_betas: sorted unique α- and β-string bitmasks (1-D (M,) or 2-D (M, n_limbs)). alpha_uid_of_pair, beta_uid_of_pair: length-N int64. Position of pair k's α- and β-strings in unique_alphas / unique_betas. partners_of_alpha_offsets: length M_α_unique + 1 int64 CSR offsets. partners_of_alpha_pair_idx: length-N int64. For unique-α index a, pair_idx[offsets[a] : offsets[a+1]] is the list of pair indices whose α equals unique_alphas[a]. Sorted by pair index. partners_of_alpha_beta_uid: length-N int64. Same slicing — the β-uid of each partner pair. Kept alongside so β-lookups on the ket side (intersection with ket-α's partners) can be done in uid space. partners_of_beta_offsets, partners_of_beta_pair_idx, partners_of_beta_alpha_uid: mirror for β.

Convention: In product mode the analogous data is implicit: the pair set is the full Cartesian product, alpha_of_pair[k] = adets[k // M_β], beta_of_pair[k] = bdets[k % M_β], and partners_of_alpha[a] is simply [a * M_β + j for j in range(M_β)]. Pair mode makes the set explicit and drops the implicit M_α × M_β grid.

n_limbs property

n_limbs: int

Number of 64-bit limbs per string (1 for n_orb <= 64).

n_pairs property

n_pairs: int

N — the flat basis dimension in pair mode.

n_unique_alphas property

n_unique_alphas: int

Number of distinct α-strings appearing in the pair set.

n_unique_betas property

n_unique_betas: int

Number of distinct β-strings appearing in the pair set.

device property

device: device

Torch device shared by all fields.

StructuralPairBlock [source] dataclass

StructuralPairBlock(alpha_start: int, alpha_stop: int, beta_start: int, beta_stop: int, pair_base: int)

One Cartesian block of a :class:StructuralPairBasis.

The block is unique_alphas[alpha_start:alpha_stop] × unique_betas[beta_start:beta_stop]. Its pairs occupy the contiguous global pair-index range [pair_base, pair_base + n_pairs) in row-major (α-major) order, so pair (i, j) of the grid has index pair_base + (i - alpha_start) * n_beta + (j - beta_start).

Attributes:

Name Type Description
alpha_start int

First α uid of the block (inclusive).

alpha_stop int

One past the last α uid of the block.

beta_start int

First β uid of the block (inclusive).

beta_stop int

One past the last β uid of the block.

pair_base int

Global index of the block's first pair.

n_alpha property

n_alpha: int

Number of α strings in the block.

n_beta property

n_beta: int

Number of β strings in the block.

n_pairs property

n_pairs: int

Number of pairs in the block.

StructuralPairBasis [source] dataclass

StructuralPairBasis(blocks: 'tuple[StructuralPairBlock, ...]', unique_alphas: Tensor, unique_betas: Tensor, n_pairs: int, pair_bases: Tensor, alpha_block_ids: Tensor, beta_block_ids: Tensor)

A pair basis described by Cartesian blocks instead of a pair list.

The basis is an ordered, pair-disjoint union of :class:StructuralPairBlock products. Only the unique α/β bitmask arrays (O(M_unique)) and a small per-string block-membership table are stored — no length-N array and no partners CSR. It is therefore the representation to use when the flat pair list would not fit. The block matvec, the serial RDM builders and the tiled distributed post-processing all walk its blocks; only the streamed table fallback still needs explicit per-pair arrays and rejects it.

Blocks may share strings (a string can appear in several blocks), but the block ranges are contiguous over the unique arrays and the pair sets are disjoint. A pair index is therefore resolved by locating its block and doing the grid arithmetic above.

Attributes:

Name Type Description
blocks 'tuple[StructuralPairBlock, ...]'

The blocks, in global pair order.

unique_alphas, unique_betas

int64 bitmask arrays, either 1-D (M_unique,) single words or (M_unique, n_limbs) limb matrices when a block string needs more than 64 bits (limb k holds bits [64k, 64k + 64)). Each block's slice is sorted by bitmask, but the arrays as a whole are block-major, so they need not be globally sorted.

n_pairs int

Total number of pairs (the flat dimension).

pair_bases Tensor

(n_blocks,) first global pair index of each block, strictly increasing.

alpha_block_ids, beta_block_ids

(M_unique, K) block ids of each string, padded with -1. K is the largest number of blocks containing one string.

n_unique_alphas property

n_unique_alphas: int

Number of unique α bitmasks stored.

n_unique_betas property

n_unique_betas: int

Number of unique β bitmasks stored.

n_blocks property

n_blocks: int

Number of Cartesian blocks.

n_limbs property

n_limbs: int

Number of 64-bit limbs per unique string (1 for single-word bases).

device property

device: device

Torch device shared by all fields.

block_of_pair

block_of_pair(pair_index: Tensor) -> Tensor

Block id of each global pair index.

pair_uids

pair_uids(pair_index: Tensor) -> tuple[Tensor, Tensor]

(α uid, β uid) of each global pair index.

pair_strings

pair_strings(pair_index: Tensor) -> tuple[Tensor, Tensor]

(α bitmask, β bitmask) of each global pair index.

pair_index_of_uids

pair_index_of_uids(alpha_uid: Tensor, beta_uid: Tensor) -> tuple[Tensor, Tensor]

Locate pairs from their (α uid, β uid) coordinates.

Parameters:

Name Type Description Default
alpha_uid, beta_uid

parallel (n,) uid tensors.

required

Returns:

Type Description
Tensor

(found, pair_index). found[i] is False when the uid pair is

Tensor

not in the basis; pair_index is then meaningless (clamped to a

tuple[Tensor, Tensor]

valid block) and must be ignored.

pair_basis_from_arrays

pair_basis_from_arrays(strings_a, strings_b, *, n_orb: int | None = None, device: device | str = 'cpu') -> PairBasis

Build a :class:PairBasis from two length-N arrays of α- and β-strings.

Semantics: each index i defines one pair (strings_a[i], strings_b[i]). Duplicate pairs (same α AND same β) are collapsed and the pair set is sorted lexicographically by (α, β). This mirrors product mode's per- spin sort — the basis order is a deterministic function of the input, not of insertion order.

Strings wider than 64 bits are carried as (M, n_limbs) limb matrices (limb k holds bits [64k, 64k+64)). The function infers the limb count from the input data (any string >= 2^64) or from n_orb if given. A wide base returns 2-D string fields; a one-word base keeps the legacy 1-D fields unless the caller explicitly passes a 2-D (M, 1) matrix.

Parameters:

Name Type Description Default
strings_a, strings_b

parallel iterables of α- and β-string int bitmasks. May be Python ints of any width, 1-D arrays, or (M, n_limbs) limb matrices. Must have equal length after conversion to arrays.

required
n_orb int | None

number of spatial orbitals. If None, inferred from the maximum bit position present in the input strings.

None
device device | str

torch device for the returned tensors.

'cpu'

Returns:

Name Type Description
A PairBasis

class:PairBasis on device.

Raises:

Type Description
ValueError

if strings_a and strings_b have different lengths, if the pair set is empty after dedup, if the two spins mix a 1-D bitmask array with a 2-D limb matrix, or if a limb matrix has the wrong number of limbs for n_orb.

structural_pair_basis_from_blocks

structural_pair_basis_from_blocks(blocks: 'Sequence[tuple[Iterable[int], Iterable[int]]]', *, n_orb: int | None = None, device: device | str = 'cpu') -> StructuralPairBasis

Build a :class:StructuralPairBasis from Cartesian block string sets.

Each block is given as two iterables of α- and β-string bitmasks; the block is their full Cartesian product. Duplicate strings inside one block are collapsed. Blocks must be pair-disjoint, which is checked with the sufficient condition that two blocks either have disjoint α string sets or disjoint β string sets.

The unique strings are renumbered block-major so every block covers a contiguous uid range: new strings of a block are appended in ascending order, and a block whose strings are already present must already form a contiguous range (appending new strings is allowed only when that range ends at the current end of the axis). A block layout that cannot be ordered this way is rejected rather than silently materialised.

Strings may be Python ints of any width, 1-D integer arrays, or (M, n_limbs) limb matrices. A block whose strings need more than 64 bits (or that is passed already split) makes the basis store its unique arrays as (M_unique, n_limbs) limb matrices, limb k holding bits [64k, 64k + 64); every other axis is zero-padded to the same width. A basis whose strings all fit in one word keeps the legacy 1-D arrays unless n_orb asks for more limbs.

Parameters:

Name Type Description Default
blocks 'Sequence[tuple[Iterable[int], Iterable[int]]]'

One (alpha_strings, beta_strings) pair per block, in global pair order.

required
n_orb int | None

optional number of spatial orbitals. When given, the basis is widened to ceil(n_orb / 64) limbs even if every block string fits in one word, matching the explicit pair basis' rule. This matters above 64 orbitals with a selected space that happens to occupy low orbitals only.

None
device device | str

Torch device for the returned tensors.

'cpu'

Returns:

Type Description
StructuralPairBasis

The structural basis, with the pair index of block b's grid

StructuralPairBasis

position (i, j) equal to

StructuralPairBasis

pair_base + (i - alpha_start) * n_beta + (j - beta_start).

Raises:

Type Description
ValueError

no blocks, an empty block, a pair-overlapping block pair, or a block layout that has no contiguous-range renumbering.

TypeError

a block string array is not integer-typed.