Skip to main content

fockit - Quantum many-body operators and solvers

Copyright (C) 2026 H. L. Nourse

What is fockit?

Build many-body models, write your own operator algebras, and test algorithms on them. Quick and simple.

Comes with fermions, bosons and Pauli operators, and you can add your own. Simple exact solvers check your ideas on small systems.

Particles must have abelian statistics.

Where to start?

The example below builds a model and solves it; the sections after it add your own algebra and open systems. More examples are in examples/ and in the tests:

  • tests/algebra/ for the operator algebra.
  • tests/hilbert/ for Hilbert spaces and matrices; test_symmetries.py there for symmetry sectors (momentum, reflection, spin flip, spin inversion, particle-hole, point groups).
  • tests/solve/ for the solvers.
  • examples/lindblad.py for open systems, with weak and strong symmetries.

Example

An electron hopping between two sites. Each site has a phonon and a spin.

from fockit.algebra import b, bdag, c, cdag, nc, sigma_x, sigma_z
from fockit.hilbert import (
    BosonSpace, FermionSpace, PauliSpace, build_basis, to_matrix,
)
from fockit.solve import groundstate

# The operator. Modes are labelled by int, float, str, bool, None, an Enum,
# a frozen dataclass, or a tuple of these, e.g. cdag(0, "up")
H = -(cdag(0) * c(1) + cdag(1) * c(0)) + 0.2 * sigma_z(0) * sigma_z(1)
for i in (0, 1):
    H += bdag(i) * b(i) + 0.5 * nc(i) * (b(i) + bdag(i)) + 0.3 * nc(i) * sigma_x(i)

# A symmetry reduced basis H acts on
basis = build_basis(
    {FermionSpace(): [0, 1], BosonSpace(n_max=6): [0, 1], PauliSpace(): [0, 1]},
    diagonal_symmetries={nc(0) + nc(1): 1},               # one electron
    permutation_symmetries=[({0: 1, 1: 0}, 1)],           # even under swapping the sites
    onsite_symmetries=[([sigma_x(0), sigma_x(1)], 1)],    # even under flipping both spins
)

# The matrix representation of H in the above basis
H_mat = to_matrix(H, basis)

# The ground state
energy, state = groundstate(H_mat)

Your own algebra

Give the rules for swapping two neighbouring operators, which order is normal, and the adjoint. For example Majorana fermions:

from dataclasses import dataclass

from fockit.algebra import Monomial, Op, OpSum, SwapResult, mode_sort_key, normal_order


@dataclass(frozen=True)
class MajoranaRules:
    """γ_i γ_j + γ_j γ_i = 2 δ_ij."""

    def swap(self, left: Op, right: Op) -> SwapResult:
        if left == right:
            return SwapResult(terms=((1, ()),))  # γ γ = 1
        return SwapResult(terms=((-1, (right, left)),))  # anticommute

    def is_normal_ordered_pair(self, left: Op, right: Op) -> bool:
        return mode_sort_key(left.mode) < mode_sort_key(right.mode)

    def dagger(self, op: Op) -> Op:
        return op


def gamma(mode: int) -> OpSum:
    return OpSum({Monomial((Op(mode, "γ", MajoranaRules()),)): 1})


normal_order(gamma(1) * gamma(0) * gamma(1))  # -γ(0)
normal_order((gamma(0) + gamma(1)) ** 2)  # 2

examples/majorana.py solves a Majorana model with it, and examples/anyons.py also gives anyons their own Hilbert space.

Open systems

Lindblad master equations work the same way. left(A) multiplies the density matrix rho from the left (rho -> A rho) and right(B) from the right (rho -> rho B), so the generator is an ordinary operator expression.

To get a matrix, rho is vectorized: written as a vector of its elements rho_mn, in Liouville space. doubled adds a copy of every mode to hold the column index n, so each basis state is a pair (m, n) and stands for the matrix |m><n|. The generator, a superoperator, then becomes a sparse matrix acting on that vector. Symmetries and your own algebras work as before.

import numpy as np

from fockit.algebra import c, cdag, left, liouvillian, nc, right
from fockit.hilbert import FermionSpace, build_basis, doubled, to_matrix, trace_vector
from fockit.solve import slowest_modes, steady_state

sites = range(6)
H = -sum(cdag((i + 1) % 6) * c(i) + cdag(i) * c((i + 1) % 6) for i in sites)
N = sum(nc(i) for i in sites)

# particles leak out of every site and are pumped back in
jumps = [np.sqrt(0.6) * c(i) for i in sites] + [np.sqrt(0.3) * cdag(i) for i in sites]
generator = liouvillian(H, jumps)

# a basis for the density matrix rho, with symmetries as before
basis = build_basis(
    doubled({FermionSpace(): sites}),
    diagonal_symmetries={left(N) - right(N): 0},
    permutation_symmetries=[({i: (i + 1) % 6 for i in sites}, 1)],
)
L = to_matrix(generator, basis)

# the identity matrix as a vector: trace @ rho is Tr(rho)
trace = trace_vector(basis)
# the steady state, normalized to Tr(rho) = 1
rho = steady_state(L, trace)
# Tr(N rho), which is 2
filling = trace @ (to_matrix(left(N), basis) @ rho)
# the 3 slowest-decaying modes; their decay rates are -rates.real
rates, _ = slowest_modes(L, k=3)

The jumps here change N, but on both sides of rho at once, so only left(N) - right(N) is conserved (a weak symmetry). Jumps that commute with N, like dephasing, also conserve left(N) and right(N) separately (a strong symmetry). expm_multiply_evolve(1j * L, rho, times) evolves rho in time, and density_matrix turns the vector back into a matrix.

Dependencies

Installation

Clone source

git clone https://github.com/thenoursehorse/fockit
cd fockit

Install with uv

uv sync

Add --extra sympy for symbolic coefficients.

Tests

uv sync --extra sympy
uv run pytest

Development

After installing as for the tests, set up the pre-commit hooks:

uv run pre-commit install

AI use

This code was written with AI coding assistants (Claude Code and Codex). All of it is reviewed and tested.

Metadata

Release files for fockit 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fockit 0.5.1
File Size Uploaded Provenance
fockit-0.5.1.tar.gz 65.1 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fockit 0.5.1
File Interpreter ABI Platform
fockit-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 111.8 kB

Release files / fockit-0.5.1.tar.gz

Download URL fockit-0.5.1.tar.gz
Size 65.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6d0accca0f880f4993a2952fcdca122fd143604d10f31cfc80515754c3510d96
BLAKE2b-256 checksum
How to use checksums
1ec9a765f72d8f9f42a641632d8694cd213ff900d3937fde55917fe87579bd36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Release files / fockit-0.5.1-py3-none-any.whl

Download URL fockit-0.5.1-py3-none-any.whl
Size 46.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
37fab9d386eff1f357c46fdd09036e8bac6972c0d3b195995790fe9bb9657860
BLAKE2b-256 checksum
How to use checksums
0df4f0275ea906485dadb5b84980f2b6cbb379a4a0b8dbd5e2fa265bc4d5ce7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Security for fockit 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fockit-0.5.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page