TorchPfaffian: PyTorch-Based Pfaffian Computation
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).
TorchPfaffian fills this gap: a signed, batched and differentiable Pfaffian that is a drop-in PyTorch
operation. 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)isPf(A) A^{-T} / 2whenAis invertible. At singularA, 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=Truegives 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, with a native Rust kernel on CPU. 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.
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, 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
Important Links
- Documentation: https://matchcake.github.io/TorchPfaffian/.
- Source: https://github.com/MatchCake/TorchPfaffian/.
- Tutorials: getting started and domino tilings and dimer probabilities.
- Performance against pfapack: performance.
- Known limitations (
torch.func, thread count, half precision): limitations. - Changes between versions: CHANGELOG.md.
Found a bug, have a question or a feature request?
- Open an issue; questions are welcome there.
- Contributions are welcome: see CONTRIBUTING.md.
License
Citation
If you use TorchPfaffian in your research, please cite the repository:
@software{torchpfaffian_Gince,
title={{TorchPfaffian}: {PyTorch}-based {P}faffian computation},
author={Gince, Jérémie},
url={https://github.com/MatchCake/TorchPfaffian},
}
Metadata
Release files for torchpfaffian 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torchpfaffian-0.0.6.tar.gz | 45.2 kB | With provenance | Details for torchpfaffian-0.0.6.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torchpfaffian-0.0.6-cp310-abi3-win_amd64.whl | CPython 3.10 | abi3 | Windows x86-64 | Details for torchpfaffian-0.0.6-cp310-abi3-win_amd64.whl |
| torchpfaffian-0.0.6-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ x86-64 | Details for torchpfaffian-0.0.6-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
| torchpfaffian-0.0.6-cp310-abi3-macosx_11_0_arm64.whl | CPython 3.10 | abi3 | macOS 11.0+ ARM64 | Details for torchpfaffian-0.0.6-cp310-abi3-macosx_11_0_arm64.whl |
Total release size: 1.1 MB
Release files / torchpfaffian-0.0.6.tar.gz
| Download URL | torchpfaffian-0.0.6.tar.gz |
|---|---|
| Size | 45.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35a4b8d82fcc4c9a8f0cb793ec88bcbb4a2d3b81c020ecf30265af2a1cbad714
|
|
BLAKE2b-256 checksum How to use checksums |
ea7306d62fb785815ffb588a3c40048d826e6407f836d5bdcba3a7e0a8e66230
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 9, 2026.
Transparency logRelease files / torchpfaffian-0.0.6-cp310-abi3-win_amd64.whl
| Download URL | torchpfaffian-0.0.6-cp310-abi3-win_amd64.whl |
|---|---|
| Size | 293.6 kB |
| Tags | CPython 3.10 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
00981cb4824308423f4d02def831b187d5427032b0f12c9e686578b815d54216
|
|
BLAKE2b-256 checksum How to use checksums |
fe312438dc9df3633b2e5afdf93f7be91bac7ed2e45783d4c7f4c582c3888cc1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 9, 2026.
Transparency logRelease files / torchpfaffian-0.0.6-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | torchpfaffian-0.0.6-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 409.1 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
949e21c845a20da94f0bc76fa251776004c92e6dcd50faea3bf52ef5d361baaa
|
|
BLAKE2b-256 checksum How to use checksums |
3647ce608943f5a26df0d7c02bc2bd01224a25d192b151f8c231db4790c428fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 9, 2026.
Transparency logRelease files / torchpfaffian-0.0.6-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | torchpfaffian-0.0.6-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 401.0 kB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
6192c7a19d42a0592c983643a9bb0e714b5d9daeea7b0d4476d131434509d1f0
|
|
BLAKE2b-256 checksum How to use checksums |
f6a49e6656840e1c8338587803f1c062962d9fdbc266a292e4fbd3aa3b1c9200
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 9, 2026.
Transparency logSecurity for torchpfaffian 0.0.6
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 4 files in this release have been uploaded with provenance metadata.
Attestation for torchpfaffian-0.0.6.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 Nov 9, 2026.
Transparency logA 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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.