Skip to content

Realisation layer

realise

Numerical realisation of symbolic models as dense complex128 operators in JAX.

The package provides operator construction, basis ordering, subspace and sector projectors, exact diagonalisation and time evolution by eigendecomposition. It supports the numerical validation of the static declarations made along a pipeline by a classical simulation of the same model at small chain length. Importing the package enables the 64-bit mode of JAX via qsimod.jax_setup.

hilbert

Hilbert spaces, basis ordering and single-site operator matrices.

The following conventions are fixed in this module:

  • Basis ordering. The local basis index is the occupation number on every algebra; a boson with cutoff n_max has n_max + 1 levels.
  • Tensor-factor order. Register site 0 is the leftmost, most significant factor, index = sum_j config[j] * prod_{k>j} dim[k].
  • Pauli matrices. Z = 2n - 1 = diag(-1, +1), X = [[0,1],[1,0]], Y = [[0,i],[-i,0]]; hence n = (Z+1)/2, sigma^+ = |1><0| = (X+iY)/2 and XY = iZ. qsimod.pauli uses the same conventions.
  • Spin-1/2. S^z = Z/2; S^+- coincide with sigma^+-.
  • Bosons. The occupation cutoff is set by a RealisationRequest, with default n_max = 2.

Every operator is realised as a dense array.

RealisationRequest dataclass

The choices a numerical realisation requires in addition to the model.

Attributes:

Name Type Description
boson_cutoff int

the maximum occupation n_max kept on a bosonic site, so that a bosonic local space has n_max + 1 levels. The cutoff is ignored by algebras that are finite-dimensional.

fermion_ordering tuple[str, ...] | None

the roles whose sites carry a Jordan-Wigner string, in the order in which the string runs. Defaults to every fermionic role, ascending by site.

LocalSpace dataclass

The local Hilbert space of one register position.

Attributes:

Name Type Description
site int

the register index.

algebra Algebra

the algebra of the site.

dimension int

the number of levels kept.

jordan_wigner_position int | None

for a fermionic site, its position in the Jordan-Wigner ordering; None for every other algebra. The string of a fermionic operator runs over the sites with a strictly smaller position.

HilbertSpace dataclass

A tensor product of local spaces, with an explicit basis ordering.

Attributes:

Name Type Description
spaces tuple[LocalSpace, ...]

the local spaces, ascending by register site. Site 0 is the leftmost tensor factor.

request RealisationRequest

the realisation request from which the space was constructed, retained for reports.

sites property
sites: tuple[int, ...]

The register sites, ascending.

dimensions property
dimensions: tuple[int, ...]

The local dimension of each site, in site order.

dimension property
dimension: int

The total Hilbert-space dimension.

strides property
strides: tuple[int, ...]

The mixed-radix stride of each tensor factor; site 0 is the most significant.

fermionic_sites property
fermionic_sites: tuple[int, ...]

The sites carrying a Jordan-Wigner position, in string order.

of classmethod
of(
    structure: StructureType,
    request: RealisationRequest | None = None,
) -> HilbertSpace

Construct the Hilbert space of a structural type.

Parameters:

Name Type Description Default
structure StructureType

the structural type of the model.

required
request RealisationRequest | None

the cutoffs and orderings; defaults are used if omitted.

None

Returns:

Type Description
HilbertSpace

The Hilbert space.

Raises:

Type Description
ValueError

if any degree of freedom has no finite-dimensional representation.

space_at
space_at(site: int) -> LocalSpace

The local space at a register site.

Raises:

Type Description
KeyError

if the site is not part of the space.

position_of
position_of(site: int) -> int

The tensor-factor position of a register site.

configuration
configuration(index: int) -> tuple[int, ...]

The occupation configuration of a basis index, in site order.

Raises:

Type Description
IndexError

if the index is out of range.

index_of
index_of(configuration: Sequence[int]) -> int

The basis index of an occupation configuration given in site order.

Raises:

Type Description
ValueError

if the configuration has the wrong length or an out-of-range occupation.

basis_state
basis_state(configuration: Sequence[int]) -> Array

The computational basis state of an occupation configuration.

configurations
configurations() -> tuple[tuple[int, ...], ...]

Every occupation configuration, in basis order; the whole basis is materialised.

label
label(index: int) -> str

A textual label of a basis index, for instance "|1 0 1 0 1>".

identity
identity() -> Array

The identity operator on the whole space.

embed
embed(site: int, matrix: Array) -> Array

Embed a local matrix at site into the full space as a tensor factor.

kron_of
kron_of(factors: Mapping[int, Array]) -> Array

The tensor product with factors at the given sites and the identity elsewhere.

jordan_wigner_string
jordan_wigner_string(site: int) -> tuple[int, ...]

The sites whose parity operator precedes a fermionic operator at site.

The result is empty for a non-fermionic site; otherwise it lists the fermionic sites with a strictly smaller Jordan-Wigner position.

local_matrix

local_matrix(
    symbol: OpSymbol, algebra: Algebra, dimension: int
) -> Array

The matrix of one primitive operator on the local space of one site.

Parameters:

Name Type Description Default
symbol OpSymbol

the primitive operator.

required
algebra Algebra

the algebra of the site.

required
dimension int

the local dimension of the site.

required

Returns:

Type Description
Array

A (dimension, dimension) complex array.

Raises:

Type Description
ValueError

if the operator is not defined on that algebra, or the algebra has no finite-dimensional representation.

build

Construction of dense JAX operators from symbolic models and Pauli sums.

Jordan-Wigner convention: a fermionic ladder operator at Jordan-Wigner position p is realised as

psi_l = (-1)**p * (prod_{q < p} P_q) * sigma^-_l, P = diag(+1, -1) = 1 - 2n,

that is, the Jordan-Wigner transformation with alternating signs, under which psi_l psi_{l+1} -> + sigma^-_{2l} sigma^-_{2l+2}. The parity string runs over the fermionic sites with a strictly smaller Jordan-Wigner position. The anticommutation relations are carried entirely by the explicit parity strings, and each term is assembled as a single tensor product.

build_operator

build_operator(
    hamiltonian: OperatorSum,
    space: HilbertSpace,
    environment: Mapping[str, float],
) -> Array

Realise a symbolic operator sum as a dense JAX array.

Parameters:

Name Type Description Default
hamiltonian OperatorSum

the symbolic sum.

required
space HilbertSpace

the Hilbert space in which the operator is constructed.

required
environment Mapping[str, float]

values for every parameter occurring in the coefficients.

required

Returns:

Type Description
Array

A (dimension, dimension) complex128 array.

Raises:

Type Description
KeyError

if a coefficient refers to an unbound parameter.

ValueError

if an operator is not defined on the algebra of its site.

build_pauli_string

build_pauli_string(
    string: PauliString, qubits: int
) -> Array

Realise one Pauli string on a register of qubits qubits.

The tensor order is that of HilbertSpace; qubit 0 is the leftmost, most significant factor.

build_pauli_sum

build_pauli_sum(pauli: PauliSum, qubits: int) -> Array

Realise a Pauli sum on a register of qubits qubits.

local_subspace_projector

local_subspace_projector(
    subspace: LocalSubspace, space: HilbertSpace
) -> Array

The projector onto a declared per-site occupation subspace, as a diagonal basis mask.

Raises:

Type Description
ValueError

if the cutoff of the space cannot represent a listed occupation.

sector_projector

sector_projector(
    constraints: Sequence[ConstraintOperator],
    space: HilbertSpace,
    environment: Mapping[str, float],
    tolerance: float = 1e-10,
) -> Array

The projector onto a declared superselection sector.

A basis mask when every constraint operator is diagonal in the occupation basis, otherwise the projector onto the kernel of sum_l (G_l - g_l)**2 from one eigh.

Parameters:

Name Type Description Default
constraints Sequence[ConstraintOperator]

the declared constraint operators, with their target eigenvalues.

required
space HilbertSpace

the Hilbert space.

required
environment Mapping[str, float]

parameter values for the coefficients of the constraint operators.

required
tolerance float

the largest admissible distance between an eigenvalue and its target.

1e-10

Returns:

Type Description
Array

The projector, as a dense array.

sandwich

sandwich(operator: Array, projector: Array) -> Array

The product P A P, the operator as it acts inside a declared subspace.

subspace_indices

subspace_indices(
    projector: Array, tolerance: float = 0.5
) -> Array

The basis indices retained by a diagonal projector, ascending.

restrict

restrict(operator: Array, indices: Array) -> Array

The block of operator on the basis states listed in indices.

sector

Numerical realisation inside a declared sector, without constructing the full space.

SectorBasis enumerates the configurations admitted by a LocalSubspace and a family of diagonal ConstraintOperators by a pruned depth-first search, and build_operator_in assembles P H P in that basis by lookup. Terms that leave the sector are dropped. The realisation is therefore exact for a model that is confined to the sector and lossy for a model whose leakage out of the sector is the observable; the latter is realised densely. Fermionic degrees of freedom are rejected: a Jordan-Wigner string is a property of the ordering and not a per-configuration amplitude.

SectorBasis dataclass

The configurations admitted by a declared subspace and a family of constraints.

Attributes:

Name Type Description
space HilbertSpace

the Hilbert space against which the configurations are indexed; it is never materialised as an array.

configurations tuple[tuple[int, ...], ...]

the admitted configurations, in enumeration order.

index Mapping[tuple[int, ...], int]

the position of each configuration in configurations.

dimension property
dimension: int

The number of configurations in the sector.

of classmethod
of(
    structure: StructureType,
    subspace: LocalSubspace | None = None,
    constraints: Sequence[ConstraintOperator] = (),
    request: RealisationRequest | None = None,
    environment: Mapping[str, float] | None = None,
) -> SectorBasis

Enumerate the sector by depth-first search with pruning.

Sites are assigned in order, and each constraint is checked as soon as the last site of its support is assigned.

Parameters:

Name Type Description Default
structure StructureType

the structural type of the model.

required
subspace LocalSubspace | None

the declared per-site occupations; every occupation is admitted if omitted.

None
constraints Sequence[ConstraintOperator]

the declared constraint operators, with their target eigenvalues.

()
request RealisationRequest | None

the cutoffs and orderings; defaults are used if omitted.

None
environment Mapping[str, float] | None

values for the coefficients of the constraints.

None

Returns:

Type Description
SectorBasis

The basis.

Raises:

Type Description
ValueError

if any degree of freedom is fermionic.

state
state(configuration: Sequence[int]) -> Array

The basis vector of one configuration, in the ordering of the sector.

Parameters:

Name Type Description Default
configuration Sequence[int]

the occupations, in site order.

required

Returns:

Type Description
Array

A unit vector of length dimension.

Raises:

Type Description
KeyError

if the configuration is not in the sector.

build_operator_in

build_operator_in(
    hamiltonian: OperatorSum,
    basis: SectorBasis,
    environment: Mapping[str, float],
) -> Array

Realise a symbolic operator sum directly in the basis of a sector.

The result equals restrict(sandwich(build_operator(...), projector), indices) and is constructed at the cost of the sector; terms that leave the sector are dropped.

Parameters:

Name Type Description Default
hamiltonian OperatorSum

the symbolic sum.

required
basis SectorBasis

the sector in which the operator is constructed.

required
environment Mapping[str, float]

values for every parameter occurring in the coefficients.

required

Returns:

Type Description
Array

A (dimension, dimension) complex128 array over the sector.

Raises:

Type Description
KeyError

if a coefficient refers to an unbound parameter.

evolve

Exact diagonalisation, time evolution and error measures.

Time evolution is computed exactly: the propagator U(t) = V diag(exp(-i E t)) V^dagger is obtained from one Hermitian eigendecomposition and carries no time-discretisation error. Two error measures are provided: spectral_norm, the operator 2-norm in which the Trotter error bounds are stated, and max_abs_deviation, the largest absolute entry, which is not a submultiplicative norm.

hermiticity_defect

hermiticity_defect(operator: Array) -> float

The defect max |H - H^dagger|, zero to machine precision for a Hermitian operator.

eigensystem

eigensystem(operator: Array) -> tuple[Array, Array]

The eigenvalues and eigenvectors of a Hermitian operator.

Parameters:

Name Type Description Default
operator Array

a Hermitian array; eigh uses only its Hermitian part.

required

Returns:

Type Description
Array

(eigenvalues, eigenvectors) with eigenvalues ascending and eigenvectors in

Array

the columns.

low_lying_spectrum

low_lying_spectrum(
    operator: Array, count: int = 8
) -> Array

The count lowest eigenvalues of a Hermitian operator, ascending.

manifold_levels

manifold_levels(
    operator: Array, manifold: Sequence[int]
) -> tuple[Array, float]

The levels continuing from a degenerate manifold, selected by overlap.

Each eigenvector is ranked by the weight it retains on the basis states of the manifold, and the len(manifold) eigenvectors of largest weight are selected.

Parameters:

Name Type Description Default
operator Array

the Hermitian operator to diagonalise.

required
manifold Sequence[int]

the basis indices of the configurations of the manifold.

required

Returns:

Type Description
Array

As many levels as the manifold has configurations, ascending, and the smallest weight

float

that any selected eigenvector retains inside the manifold.

spacing_deviation

spacing_deviation(
    left: ArrayLike, right: ArrayLike, scale: float = 1.0
) -> float

The largest level-by-level gap between two mean-centred spectra, in units of scale.

Parameters:

Name Type Description Default
left ArrayLike

one spectrum, ascending.

required
right ArrayLike

the other spectrum, of the same length.

required
scale float

the energy unit in which the gap is reported.

1.0

Returns:

Type Description
float

The gap.

propagator

propagator(operator: Array, time: float) -> Array

The propagator exp(-i H t) of a Hermitian H, from one eigendecomposition.

evolve_state

evolve_state(
    operator: Array,
    state: Array,
    times: Sequence[float] | ArrayLike,
) -> Array

Evolve state under a Hermitian H to each of times, from one eigendecomposition.

Parameters:

Name Type Description Default
operator Array

the Hermitian generator.

required
state Array

the initial state vector.

required
times Sequence[float] | ArrayLike

the times at which the state is reported, as a sequence or a 1-d array.

required

Returns:

Type Description
Array

An array of shape (len(times), dimension), one state per time.

expectation

expectation(operator: Array, states: Array) -> Array

The expectation value of operator in one state or a stack of states.

Parameters:

Name Type Description Default
operator Array

a Hermitian observable.

required
states Array

a state vector, or an array of shape (times, dimension).

required

Returns:

Type Description
Array

A scalar, or one real value per state.

gauss_violation

gauss_violation(
    constraint_operators: Sequence[Array], states: Array
) -> Array

The gauge violation sum_l <G_l**2>, the diagnostic of gauge invariance.

Parameters:

Name Type Description Default
constraint_operators Sequence[Array]

the realised constraint operators.

required
states Array

a state vector, or a stack of states.

required

Returns:

Type Description
Array

The summed expectation of the squares, per state.

spectral_norm

spectral_norm(operator: Array) -> float

The operator 2-norm, the largest singular value.

The Trotter error bounds are stated in this norm.

max_abs_deviation

max_abs_deviation(left: Array, right: Array) -> float

The largest absolute entry of left - right.

This measure is not a submultiplicative norm and not the norm in which the Trotter bounds are stated.

commutator_spectral_norm

commutator_spectral_norm(
    left: Array, right: Array
) -> float

The commutator norm ||[A, B]|| in the spectral norm, for Hermitian A and B.

i[A, B] is Hermitian, so its norm is its largest absolute eigenvalue.

jax_setup

Process-global JAX configuration.

Importing this module enables the 64-bit mode of JAX. The setting is process-global and also affects the JAX code of the importing program. Every operator is built in complex128; real spectra, times and observables use float64.

enable_x64

enable_x64() -> None

Enable the 64-bit mode of JAX; the call is idempotent and process-global.

x64_enabled

x64_enabled() -> bool

Whether JAX is currently in 64-bit mode.

require_x64

require_x64() -> None

Raise if JAX is not in 64-bit mode.

Raises:

Type Description
RuntimeError

if the 64-bit mode has been disabled after the import of the package.