Skip to main content

PyTorch-based Pfaffian computation for skew-symmetric matrices.

Project description

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},
}

Project details


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.4.tar.gz (21.5 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.4-cp310-abi3-win_amd64.whl (238.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

torchpfaffian-0.0.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (360.4 kB view details)

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

torchpfaffian-0.0.4-cp310-abi3-macosx_11_0_arm64.whl (350.8 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: torchpfaffian-0.0.4.tar.gz
  • Upload date:
  • Size: 21.5 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.4.tar.gz
Algorithm Hash digest
SHA256 7b2bb907e841215226c97300f6bb53ba087221a0a054984d8c91fc90b6c0aef3
MD5 e61660d4677986ebd9a5b60a27f6cf5c
BLAKE2b-256 08765951ce47d2dfd230edaf2b056911359f55734402daa8d360c780a034c70e

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchpfaffian-0.0.4.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.4-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for torchpfaffian-0.0.4-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 28a87f600f6f845b39394ccdb6b87b9e037bff3b3aa56db0b9618cdcb0f30371
MD5 415b7df38764e7f2fc77b35b11ffe474
BLAKE2b-256 80239b1e0d5ecc04b11da4e515c49acf4dffaa83b394db2e1b6569da93af1f2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchpfaffian-0.0.4-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.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchpfaffian-0.0.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 70efc27ddc46a8e7ac979688c77195dc4ec5b251caed9848e5beea5fce69eae2
MD5 bac9f0a9ed0981b614bc23928f008133
BLAKE2b-256 0ba2a6acbae1742431ba0967032d6d17c6bdaebb14fa0e64c24565417c7d5fec

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchpfaffian-0.0.4-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.4-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchpfaffian-0.0.4-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b0623135c40fe6587e935c2d5f6399022995aa43564c995bdf7a4ddccba3dd3
MD5 63792bed3f903ab596daef54b515a850
BLAKE2b-256 29d4855e880263d0194c0ef2e9578a776b683703d4ab476e9c0c420ee699c48b

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchpfaffian-0.0.4-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.

Supported by

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