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

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.3.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

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

torchpfaffian-0.0.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (333.1 kB view details)

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

File details

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

File metadata

  • Download URL: torchpfaffian-0.0.3.tar.gz
  • Upload date:
  • Size: 18.3 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.3.tar.gz
Algorithm Hash digest
SHA256 bcc38a77c1e1b29069f96b781e988d89bbfdc6976e12938e0f875c7091a05d2b
MD5 f6aa9ad048c3d9ded7c424b51e0cc8f1
BLAKE2b-256 4cc26404cd303cb020bed14b5abb54d5132a7befe6d058f9978272d705ade857

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for torchpfaffian-0.0.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9bbd17128bfbb62560cb533c73233d804fc0f440782de3513a2ee4e930fc1171
MD5 2aa170bccbc55508ceb07bc2fd78932a
BLAKE2b-256 69c46e1aa5b9880dceae77367ca5aa795d138b70b5c8d470af0fa86134e51692

See more details on using hashes here.

Provenance

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

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