Skip to main content

TorchPfaffian: PyTorch-Based Pfaffian Computation

Star on GitHub GitHub forks Python 3.6 downloads PyPI version License

Tests Workflow Dist Workflow Doc Workflow Coverage Badge Workflow codecov

Description

TorchPfaffian is a Python package for efficiently computing the Pfaffian of skew-symmetric matrices using PyTorch. Designed as a PyTorch-based alternative to pfapack, it enables GPU acceleration and supports automatic differentiation, making it particularly useful in physics, quantum computing, and machine learning applications.

Features:

  • Efficient Pfaffian computation for skew-symmetric matrices
  • GPU acceleration via PyTorch
  • Support for automatic differentiation
  • Seamless integration with PyTorch tensors

Installation

With python and pip installed, run the following commands to install TorchPfaffian:

pip install torchpfaffian

For development, this project uses uv. Clone the repository and set up the environment with:

uv sync --dev --extra cpu

Use the cu128 or cu130 extra instead of cpu to install a CUDA-enabled build of PyTorch. See .github/CONTRIBUTING.md for the full contribution workflow.

Native acceleration (optional)

A Rust-accelerated signed-Pfaffian strategy (RustPfaffianParlettReid) is available when the package is built with its native extension. Building from source requires a Rust toolchain (https://rustup.rs); the project builds with maturin:

uv run maturin develop --release -m rust/Cargo.toml

Use --release for an optimized build: maturin develop compiles in debug mode by default, which makes the Rust kernel much slower. Installing a prebuilt wheel (or maturin build) is already optimized, so this only matters for local development builds.

If the native extension is not present, the package still works using the pure-Python strategies.

Usage

import torch

from torch_pfaffian import 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|, using the faster det-based path

pfaffian() selects a strategy from the input: sign=True (the default) returns the signed Pfaffian, using the native RustPfaffianParlettReid when the extension is built and falling back to the pure-Python PfaffianParlettReid otherwise; sign=False returns the magnitude using a determinant-based strategy (PfaffianFDBPf when gradients are needed, otherwise PfaffianDet). For explicit strategy selection, use get_pfaffian_function(name).

Important Links

Found a bug or have a feature request?

License

Apache License 2.0

Acknowledgements

Citation

Repository:

@misc{torchpfaffian_Gince2025,
  title={Torch Pfaffian},
  author={Jérémie Gince},
  year={2025},
  publisher={Université de Sherbrooke},
  url={https://github.com/MatchCake/TorchPfaffian},
}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchpfaffian-0.0.5.tar.gz (31.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

torchpfaffian-0.0.5-cp310-abi3-win_amd64.whl (275.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

torchpfaffian-0.0.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (392.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

torchpfaffian-0.0.5-cp310-abi3-macosx_11_0_arm64.whl (383.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file torchpfaffian-0.0.5.tar.gz.

File metadata

  • Download URL: torchpfaffian-0.0.5.tar.gz
  • Upload date:
  • Size: 31.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torchpfaffian-0.0.5.tar.gz
Algorithm Hash digest
SHA256 7fb8b259da91bf6368159c4803ee66422c717ad08a647dcbea0cb899723915b2
MD5 91e19be32fc316b41f179c1f6cf1935b
BLAKE2b-256 98b152b741b6dc99b681fa05e29786c25f8f59d5da3b4bba5d64cf40c9e70fd3

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchpfaffian-0.0.5.tar.gz:

Publisher: build_dist.yml on MatchCake/TorchPfaffian

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchpfaffian-0.0.5-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for torchpfaffian-0.0.5-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0d1a24309eb72b997caf9a50b0b9d8bb139f05070c9154605af418407f400f34
MD5 ee3605f985c4c13bea3954a50ec050eb
BLAKE2b-256 035ffcd64680da45fff3fe0733f602e443c4548342f4357b0150e638c15148cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchpfaffian-0.0.5-cp310-abi3-win_amd64.whl:

Publisher: build_dist.yml on MatchCake/TorchPfaffian

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchpfaffian-0.0.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchpfaffian-0.0.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 66973fd6e0aab694040ea4613957ceb570369183d264ea08dc8427096ed1a54b
MD5 385ef73387e825698830c88b3739863a
BLAKE2b-256 0efaa76b9cab3944fcc9544f276658929c474129c5ec96dd9ca0603bd6aa7673

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchpfaffian-0.0.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_dist.yml on MatchCake/TorchPfaffian

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchpfaffian-0.0.5-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchpfaffian-0.0.5-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3939ad259db3f727d6b84f4b00d24eded0052e650637ad9c02f9bf0dac7d1ec3
MD5 0e5438a7ebfbb984d19cd8bd362dfcda
BLAKE2b-256 fce979895965d21f796a52193343738d43ff69720ad4f25498e5d83bf38eeb29

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchpfaffian-0.0.5-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: build_dist.yml on MatchCake/TorchPfaffian

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.0.5 This release

4 files

0.0.4

4 files

0.0.3

2 files

0.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page