Skip to content

About

Python package for Pfaffian computing using PyTorch

Topics

Resources

Code of conduct

Contributing

Stars

4 stars

Watchers

0 watching

Forks

Repository files navigation

TorchPfaffian: PyTorch-Based Pfaffian Computation

Star on GitHub GitHub forks Python downloads PyPI version License DOI

Tests Workflow Dist Workflow Doc Workflow Coverage Badge Workflow codecov

Description

TorchPfaffian computes the Pfaffian of batches of skew-symmetric matrices in PyTorch, with exact gradients on CPU and GPU, in float32, float64, complex64 and complex128 (float16, bfloat16 and complex32 inputs are computed in float32 or complex64).

Statement of need

Pfaffians appear wherever fermions are paired: the overlaps and amplitudes of fermionic Gaussian states, the simulation of matchgate (free-fermion) quantum circuits, BCS and Hartree–Fock–Bogoliubov wave functions, the partition functions of planar Ising models and perfect-matching counts. In these settings the Pfaffian is usually one step of a larger differentiable model that runs on batches of matrices, often on a GPU, and is optimized by gradient descent.

The reference implementation, pfapack, works on single NumPy arrays, without automatic differentiation or GPU support. The determinant torch.linalg.det gives only Pf(A)^2 = det(A) and loses the sign, which carries the physics (interference between amplitudes). torchpfaff brings a differentiable Parlett–Reid Pfaffian to PyTorch, with a Triton kernel on CUDA, for real matrices. TorchPfaffian fills the remaining gap: a signed, batched and differentiable Pfaffian that is a drop-in PyTorch operation, for real and complex matrices, with exact gradients at singular matrices. It is the Pfaffian backend of the MatchCake matchgate simulator.

Features

  • One entry point, automatic routing. pfaffian() picks the fastest correct strategy for the input (size, dtype, device, block structure, whether gradients and the sign are needed) from a registry of strategies: an exact unrolled kernel for small matrices, Parlett–Reid tridiagonalization in Rust (CPU) and in PyTorch (any device), determinant-based magnitudes, and a block-determinant path for matrices of the form [[0, B], [-B^T, 0]].
  • Exact gradients, including at singular matrices. The backward of Pf(A) is Pf(A) A^{-T} / 2 when A is invertible. At singular A, where the inverse does not exist but the gradient does, it is computed exactly from the Pfaffian adjugate. Complex inputs follow PyTorch's Wirtinger convention.
  • Overflow-safe. Results that overflow the dtype in the linear domain are recomputed in the log domain; slog_pfaffian() returns (phase, log|Pf|) directly.
  • Round-off zeros. mask_roundoff_gradient=True gives a zero gradient to Pfaffians that vanish up to round-off, such as parity-forbidden amplitudes.
  • Batched and device-agnostic. Any leading batch shape, on CPU or CUDA. On CPU, a native Rust kernel computes the Pfaffian and the inverse of its backward. On CUDA, small inputs are computed by that kernel on the host (the device would be bound by kernel launches), and larger real ones by a single Triton kernel per batch. On CPU it is faster than pfapack's C implementation on batches of small matrices, and on batches of large ones with several threads; on a single matrix, pfapack is faster. See the performance page.
  • Low-rank updates. LowRankPfaffian gives Pfaffian ratios and updated inverses under low-rank updates and row replacements, PfaffianUpdater chains them (with delayed updates), and PfaffianChain grows a matrix block by block for qubit-by-qubit sampling. On CPU without gradients they run in Rust kernels.

Benchmarks

benchmarks/library_comparison.py times TorchPfaffian, pfapack, torchpfaff and lrux side by side, on CPU and on GPU when available, with the time and the peak memory of each call, and benchmarks/plot_results.py plots them (see benchmarks/README.md). Comparing each library at its best method and device with the fastest library on each input (full Pfaffians with and without gradients, float64 and complex128, m from 8 to 256, and low-rank ratios, n from 32 to 512, batches of 1 to 256; one CPU thread and an RTX 3070):

fastest on, CPU time vs fastest, CPU fastest on, CUDA time vs fastest, CUDA
TorchPfaffian 49% 1.3x 42% 1.5x
lrux 37% 1.9x 55% 1.5x
pfapack (C) 17% 9.2x
torchpfaff 0% 30x 25% 1.9x

The time is the geometric mean, over the inputs a library computes, of its time over the fastest one's. A method whose first call takes over 10 s is timed from that call, compilation included, which raises the CUDA means of torchpfaff (its Triton kernel) and lrux on a few inputs. Single matrices favor pfapack on CPU and TorchPfaffian on CUDA; lrux (through fermix's kernels) is the fastest on large float64 batches on CUDA. Times depend on the machine: run the script on yours.

Requirements

  • Python 3.10 to 3.14 and PyTorch 2.1 or newer.
  • Prebuilt wheels with the Rust kernel are published for Linux x86_64, Linux aarch64, Windows x86_64 and macOS arm64. On other platforms pip builds the package from source, which needs a Rust toolchain (https://rustup.rs).

Installation

pip install torchpfaffian

On Linux, pip installs the CUDA build of PyTorch by default. For a CPU-only environment, install PyTorch first from its CPU index:

pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install torchpfaffian

For development, this project uses uv. From a clone of the repository:

uv sync --dev --extra cpu
uv run maturin develop --release -m rust/Cargo.toml

The cpu, cu128 and cu130 extras select the PyTorch build; they only take effect with uv from a clone. --release matters: maturin develop builds the Rust kernel in debug mode by default, which makes it much slower. See CONTRIBUTING.md for the full development workflow.

Usage

import torch

from torch_pfaffian import pfaffian, slog_pfaffian

# Any skew-symmetric matrix of shape (..., 2n, 2n).
matrix = torch.tensor([[0.0, -3.0], [3.0, 0.0]])

pf = pfaffian(matrix)                 # signed Pfaffian (default)
magnitude = pfaffian(matrix, sign=False)  # |pf|, through the faster determinant-based path

pfaffian() chooses the strategy from the input; the full routing table is in its documentation. To force a strategy, use get_pfaffian_function(name), for example get_pfaffian_function("PfaffianParlettReid")(matrix).

Gradients flow through every path:

matrix = torch.randn(32, 10, 10, dtype=torch.float64)
matrix = (matrix - matrix.transpose(-1, -2)).requires_grad_()

pfaffian(matrix).sum().backward()     # matrix.grad = Pf(A) A^{-T} / 2 per batch element
phase, log_abs = slog_pfaffian(matrix.detach())  # log domain, never overflows

Pfaffian ratios under low-rank updates

Monte Carlo moves and neural quantum states often only need Pf(A') / Pf(A) for a matrix A' that differs from A by a few rows and columns. Given A^{-1}, LowRankPfaffian computes that ratio in O(n^2 k) (O(n) for one replaced row) instead of the O(n^3) of a new Pfaffian, together with the updated inverse. It follows the convention of lrux: A' = A - u J u^T with u of shape (n, 2k) and J = [[0, I_k], [-I_k, 0]].

from torch_pfaffian import PfaffianUpdater, pfaffian_ratio, pfaffian_ratio_replace_and_inverse

inverse = torch.linalg.inv(matrix.detach())                 # (32, 10, 10)
update = torch.randn(32, 10, 2, dtype=torch.float64)       # rank-2 update u = (x, y)
ratio = pfaffian_ratio(inverse, update)                     # Pf(A - u J u^T) / Pf(A)
change = torch.randn(32, 10, dtype=torch.float64)
ratio, new_inverse = pfaffian_ratio_replace_and_inverse(inverse, 3, change)  # row and column 3 change

updater = PfaffianUpdater(matrix.detach(), max_delay=4)    # delayed inverse updates for long chains
accepted = torch.rand(32) < updater.ratio(update).abs() ** 2
updater.accept(update, mask=accepted)                       # only the accepted walkers move

For a matrix that grows block by block, as in qubit-by-qubit sampling of matchgate circuits, pfaffian_extension_ratio gives Pf([[A, B], [-B^T, C]]) / Pf(A) = Pf(C + B^T A^{-1} B) from A^{-1}, and PfaffianChain keeps the inverse from one qubit to the next, so that a sample of n qubits costs O(n^3) instead of O(n^4):

from torch_pfaffian import PfaffianChain

chain = PfaffianChain((shots,))                              # starts from the empty matrix
for qubit in range(n_qubits):
    modes = slice(2 * qubit, 2 * qubit + 2)
    border = covariance[..., : 2 * qubit, modes]             # (shots, 2 qubit, 2)
    corners = covariance[..., modes, modes] + outcome_blocks # (shots, 2, 2, 2): the blocks for y = 0 and 1
    probabilities = chain.ratios(border, corners).abs() / 2  # conditional probabilities, (shots, 2)
    outcome = torch.multinomial(probabilities, 1)[..., 0]
    chain.extend(border, corners[torch.arange(shots), outcome])

For long runs on small matrices, where PyTorch's per-operator overhead dominates, CompiledLowRankPfaffian (and PfaffianChain(..., compiled=True), PfaffianUpdater(..., compiled=True)) runs the same computations through torch.compile: a few seconds of compilation on first use, then about 2x faster inverse updates on a single matrix, on CPU and CUDA. The eager LowRankPfaffian stays the default.

Everything is batched, differentiable, and runs on CPU and CUDA. Gradients are exact for any skew-symmetric construction of the inputs, such as a covariance matrix computed from circuit parameters; the gradient with respect to a raw inverse leaf follows the skew-split convention of pfaffian(), while those with respect to blocks sliced from a leaf matrix (border, corner) are only meaningful in their skew-symmetric part. Functions that also return the updated inverse have their own name (..._and_inverse) rather than a flag. pfaffian_ratio, pfaffian_ratio_and_inverse, pfaffian_ratio_replace, pfaffian_ratio_replace_and_inverse, pfaffian_extension_ratio, pfaffian_extension_ratio_and_inverse and skew_identity are shortcuts to the static methods of LowRankPfaffian.

Important Links

Found a bug, have a question or a feature request?

License

Apache License 2.0

Citation

If you use TorchPfaffian in your research, please cite it:

@software{gince_torchpfaffian,
  author    = {Jérémie Gince},
  title     = {{TorchPfaffian}},
  publisher = {Zenodo},
  year      = {2026},
  doi       = {10.5281/zenodo.24085345},
  url       = {https://doi.org/10.5281/zenodo.24085345},
}

This DOI always resolves to the latest version. To cite the exact version you used, take its DOI and BibTeX entry from the Zenodo record.

Related software

TorchPfaffian is compared against pfapack, torchpfaff and lrux in benchmarks/library_comparison.py. lrux, in JAX, computes determinant and Pfaffian ratios under low-rank updates, the inner loop of fermionic Monte Carlo, and its full Pfaffians come from fermix, batched determinants and Pfaffians with JAX/Pallas kernels on GPU; TorchPfaffian's LowRankPfaffian and PfaffianUpdater implement the same Pfaffian updates in PyTorch (#33), and --ratios compares them with lrux. If you use torchpfaff, lrux or fermix, please cite them as well:

@software{adams_torchpfaff,
  author  = {Corey Adams},
  title   = {{torchpfaff}: A differentiable Pfaffian for {PyTorch}, with {GPU} support via {Triton}},
  year    = {2026},
  version = {0.2.0},
  url     = {https://github.com/Nuclear-Physics-with-Machine-Learning/PyPfaffian},
}

@misc{chen_lrux,
  author        = {Ao Chen and Christopher Roth},
  title         = {{lrux}: Fast low-rank updates of determinants and {Pfaffians} in {JAX}},
  year          = {2026},
  eprint        = {2602.05255},
  archivePrefix = {arXiv},
  primaryClass  = {cond-mat.str-el},
  doi           = {10.48550/arXiv.2602.05255},
  url           = {https://arxiv.org/abs/2602.05255},
}

@software{chen_fermix,
  author  = {Ao Chen},
  title   = {{fermix}: Fast batched real and complex determinants and {Pfaffians} on {GPU} with {JAX}/{Pallas}
             kernels},
  year    = {2026},
  version = {0.1.0},
  url     = {https://github.com/ChenAo-Phys/fermix},
}

About

Python package for Pfaffian computing using PyTorch

Topics

Resources

Code of conduct

Contributing

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages