pyganak — Python bindings for Ganak
pyganak provides Python bindings for Ganak,
a high-performance exact model counter for CNF formulas. It exposes two
classes:
Counter— unweighted (and projected) model counting. Returns an arbitrary-precision Pythonint.WeightedCounter— weighted model counting with per-literal float weights. Uses MPFR internally; returns a Pythonfloat.
Arjun preprocessing is applied automatically before each count, just as the command-line tool does.
Installation
pip install pyganak
Pre-built wheels are available for Linux (x86-64, ARM64) and macOS (Apple Silicon, Intel).
Quick start
Unweighted counting
from pyganak import Counter
c = Counter()
c.add_clause([1, 2]) # x1 OR x2
c.add_clause([-1, 2]) # NOT x1 OR x2
print(c.count()) # → 2 (exact Python int)
Weighted counting
from pyganak import WeightedCounter
c = WeightedCounter()
c.add_clause([1, 2]) # x1 OR x2
# Set weights for both polarities of each variable.
c.set_lit_weight( 1, 0.3) # P(x1 = True) = 0.3
c.set_lit_weight(-1, 0.7) # P(x1 = False) = 0.7
c.set_lit_weight( 2, 0.4) # P(x2 = True) = 0.4
c.set_lit_weight(-2, 0.6) # P(x2 = False) = 0.6
# Models: (T,T)=0.12 (T,F)=0.18 (F,T)=0.28 → total=0.58
print(c.count()) # → 0.58 (Python float)
Counter API
Counter(verbose=0, seed=0)
| Parameter | Type | Default | Description |
|---|---|---|---|
verbose |
int |
0 |
Verbosity level (0 = silent) |
seed |
int |
0 |
Random seed for the solver |
add_clause(clause)
Add a single clause — iterable of nonzero signed integers (1-indexed variables; positive = positive literal, negative = negated literal).
c.add_clause([1, -2, 3]) # x1 OR NOT x2 OR x3
add_clauses(clauses)
Add multiple clauses at once.
c.add_clauses([[1, 2], [-1, 3]])
set_sampling_set(vars)
Set the projection set (independent support). Only the given variables are counted; all others are existentially quantified out. If never called, all variables are included.
c.add_clause([1, 2, 3])
c.set_sampling_set([1, 2])
print(c.count()) # → 4
count() → int
Run Arjun preprocessing + Ganak and return the exact model count as a Python
int. May only be called once per instance.
new_vars(n) / nof_vars() → int / nof_clauses() → int
Declare extra variables, or query the current variable / clause count.
WeightedCounter API
WeightedCounter(verbose=0, seed=0, prec=128)
| Parameter | Type | Default | Description |
|---|---|---|---|
verbose |
int |
0 |
Verbosity level |
seed |
int |
0 |
Random seed |
prec |
int |
128 |
MPFR precision in bits (≥ 2) |
The internal arithmetic is performed with MPFR at prec bits of precision
(default 128 ≈ 38 significant decimal digits). The final result is returned
as a Python float (double).
Floating-point caveat: Because floating-point arithmetic is not associative, the result is a high-precision approximation rather than an exact value — the order in which partial sums are accumulated can affect the last few bits. For exact rational weighted counting, use the command-line tool with
--mode 1(GMPmpqrationals).
set_lit_weight(lit, weight)
Set the weight of a literal.
lit— nonzero signed integer (1-indexed; positive = positive literal).weight— Pythonfloat(double). Stored internally as an MPFR value at the precision given to the constructor.
Always set weights for both a literal and its negation to get
well-defined weighted counting. Literals whose weights are not set default
to weight 1.0.
c.set_lit_weight( 1, 0.3) # weight of x1 = 0.3
c.set_lit_weight(-1, 0.7) # weight of ¬x1 = 0.7
add_clause(clause) / add_clauses(clauses) / set_sampling_set(vars) / new_vars(n) / nof_vars() / nof_clauses()
Identical to the Counter versions.
count() → float
Run Arjun preprocessing + Ganak weighted model counting. Returns the
weighted count as a Python float. The internal computation uses MPFR at
the configured precision. May only be called once per instance.
Examples
Plain model counting
from pyganak import Counter
# (x1 XOR x2): 2 models
c = Counter()
c.add_clause([1, 2])
c.add_clause([-1, -2])
print(c.count()) # 2
Projected model counting
from pyganak import Counter
# (x1 OR x2 OR x3) projected onto {x1, x2}: 4 models
c = Counter()
c.add_clause([1, 2, 3])
c.set_sampling_set([1, 2])
print(c.count()) # 4
Large count (arbitrary precision)
from pyganak import Counter
# 30 unconstrained variables → 2^30 models
c = Counter()
c.new_vars(30)
print(c.count()) # 1073741824
Weighted counting
from pyganak import WeightedCounter
# (x1 OR x2) with independent Bernoulli weights
c = WeightedCounter()
c.add_clause([1, 2])
c.set_lit_weight( 1, 0.3); c.set_lit_weight(-1, 0.7)
c.set_lit_weight( 2, 0.4); c.set_lit_weight(-2, 0.6)
print(c.count()) # 0.58
Weighted counting with higher MPFR precision
from pyganak import WeightedCounter
# Use 256-bit MPFR precision for the internal computation.
c = WeightedCounter(prec=256)
c.add_clause([1, 2, 3])
c.set_lit_weight( 1, 0.1); c.set_lit_weight(-1, 0.9)
c.set_lit_weight( 2, 0.2); c.set_lit_weight(-2, 0.8)
c.set_lit_weight( 3, 0.5); c.set_lit_weight(-3, 0.5)
print(c.count())
Building from source (venv)
If you have already built Ganak with CMake (see the top-level README.md
for instructions), the extension is in build/lib/pyganak*.so. Test it
without a full pip install using a venv:
python3 -m venv venv
venv/bin/pip install pytest
PYTHONPATH=build/lib venv/bin/pytest python/tests/ -v
To rebuild the extension after editing python/src/pyganak.cpp:
# If the build directory already exists:
cmake -DBUILD_PYTHON_EXTENSION=ON build # or cmake .. from inside build/
cmake --build build --target pyganak -j$(nproc)
# Then re-run tests as above.
To do a full pip install (builds everything from scratch):
pip install .
Requires: GMP ≥ 5, MPFR ≥ 3, FLINT ≥ 2. On Ubuntu/Debian:
sudo apt install libgmp-dev libmpfr-dev libflint-dev
On macOS:
brew install gmp mpfr flint
License
MIT — see LICENSE.txt in the repository root.
Metadata
Release files for pyganak 2.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 90.0 MB
Release files / pyganak-2.8.0-cp313-cp313-manylinux_2_34_x86_64.whl
| Download URL | pyganak-2.8.0-cp313-cp313-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 6.6 MB |
| Tags | CPython 3.13 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
b7652212c151f7278b52229680d12f4d76e791022e75a232628f342b294995c4
|
|
BLAKE2b-256 checksum How to use checksums |
44285458ada847d21f92c27240cb1ec04403ece4e2708d3b1f0bf94a7a89a283
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp313-cp313-manylinux_2_34_aarch64.whl
| Download URL | pyganak-2.8.0-cp313-cp313-manylinux_2_34_aarch64.whl |
|---|---|
| Size | 6.0 MB |
| Tags | CPython 3.13 Linux glibc 2.34+ ARM64 |
|
SHA-256 checksum How to use checksums |
81a7723a1e83b6fcf5f49e83274da5b100d6a57c1ed8c183eaa3d637e7f27c47
|
|
BLAKE2b-256 checksum How to use checksums |
c14471cc4d803c58fc834b0fdcab5beed22e515e08b7910e2fd36da560982983
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | pyganak-2.8.0-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 5.4 MB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
796b89fb4396ef50fb9d7c3cf6963f05cb5f302f9e7f3f402a669b6d863ce3bc
|
|
BLAKE2b-256 checksum How to use checksums |
f6a1f6c91707de75c94629785596e14c5d3bc307d8a307459ea75287e5b5e823
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp312-cp312-manylinux_2_34_x86_64.whl
| Download URL | pyganak-2.8.0-cp312-cp312-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 6.6 MB |
| Tags | CPython 3.12 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
83178cbca5a17f2a1c6f73ff2331413a9585205844b89bef78583c30067d9cc0
|
|
BLAKE2b-256 checksum How to use checksums |
f33b74f076c4fc1cea9750be1ccd3b6a0694e3d6dd482db1470564af55f39db3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp312-cp312-manylinux_2_34_aarch64.whl
| Download URL | pyganak-2.8.0-cp312-cp312-manylinux_2_34_aarch64.whl |
|---|---|
| Size | 6.0 MB |
| Tags | CPython 3.12 Linux glibc 2.34+ ARM64 |
|
SHA-256 checksum How to use checksums |
4737a9f61d993a34478e9cb5b43f77e480e164e610a0254ad3551860c7bc16a0
|
|
BLAKE2b-256 checksum How to use checksums |
cd04ec854193f0e3394b0dc0947509a363a3122a1d6e79bcf82a7bc98af7026d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | pyganak-2.8.0-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 5.4 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
c4c81a957131d7f8ad708b1b679dcd9063582c0808de98b2acc0f310e86e372d
|
|
BLAKE2b-256 checksum How to use checksums |
a7f40c9bc258909e6732160e8ba1e6befcd134c5fd927d48d06005070457f3c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp311-cp311-manylinux_2_34_x86_64.whl
| Download URL | pyganak-2.8.0-cp311-cp311-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 6.6 MB |
| Tags | CPython 3.11 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
684ceba30c3e73bc986ce6a66f3083b81920bece4e6a5a29ceacdd594cdbbedf
|
|
BLAKE2b-256 checksum How to use checksums |
b41220140b6429afef23af0b889607dd648cb71611d84edadc80cc2084735096
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp311-cp311-manylinux_2_34_aarch64.whl
| Download URL | pyganak-2.8.0-cp311-cp311-manylinux_2_34_aarch64.whl |
|---|---|
| Size | 6.0 MB |
| Tags | CPython 3.11 Linux glibc 2.34+ ARM64 |
|
SHA-256 checksum How to use checksums |
edf9a79d553c5a2afc9de114cfc36f76475e2a7d95fd30c146c6066c4a3010f5
|
|
BLAKE2b-256 checksum How to use checksums |
943d216a312be56391162813bd87b202b401b968239b9179d72ee3abe6dc82f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | pyganak-2.8.0-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 5.4 MB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
96990ea4efa2d6a152cfb5123799e04201771ee5e7f9e3b5dae69b02a3b61519
|
|
BLAKE2b-256 checksum How to use checksums |
621d37c85715b5c1af1679dec23662fd88ce4fd1f2815684b2d3c028726cf411
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp310-cp310-manylinux_2_34_x86_64.whl
| Download URL | pyganak-2.8.0-cp310-cp310-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 6.6 MB |
| Tags | CPython 3.10 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
4b5a27c357f4b628f9c2957b0a92cd922148a88ec335b0e99765e8bcd412d3a6
|
|
BLAKE2b-256 checksum How to use checksums |
46e92c6148e0fae681db705502fa18fb96c3d7ab8b887b8f4a62c8811cca2c4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp310-cp310-manylinux_2_34_aarch64.whl
| Download URL | pyganak-2.8.0-cp310-cp310-manylinux_2_34_aarch64.whl |
|---|---|
| Size | 6.0 MB |
| Tags | CPython 3.10 Linux glibc 2.34+ ARM64 |
|
SHA-256 checksum How to use checksums |
e7106ede7881f9b226cabdc295f081bf1243537634eff7d1f39b11abc72dd2b4
|
|
BLAKE2b-256 checksum How to use checksums |
dcd47153c3c123a7650094d7432df87c81a58c517c82d0f3c21d3c449490be01
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | pyganak-2.8.0-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 5.4 MB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
75a13c9299a095fb5231472f01a6b09ded2cfbe08ffd7ba32186d6949cff77c4
|
|
BLAKE2b-256 checksum How to use checksums |
07906ee42b3b62748d254adf55881b394c874a023a5fda2c314926825d94689e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp39-cp39-manylinux_2_34_x86_64.whl
| Download URL | pyganak-2.8.0-cp39-cp39-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 6.6 MB |
| Tags | CPython 3.9 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
00e229f23d0b21fe725534848b670507e95c2e56519f542f14296164d11dc225
|
|
BLAKE2b-256 checksum How to use checksums |
37924c627340ca7ae06f3d015931b4557b05a96c2015511d8b01c91b9dfe873e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp39-cp39-manylinux_2_34_aarch64.whl
| Download URL | pyganak-2.8.0-cp39-cp39-manylinux_2_34_aarch64.whl |
|---|---|
| Size | 6.0 MB |
| Tags | CPython 3.9 Linux glibc 2.34+ ARM64 |
|
SHA-256 checksum How to use checksums |
5202891b3733ac29cd3fac271e85924af95e34f96d878d93e87f4358749a4da2
|
|
BLAKE2b-256 checksum How to use checksums |
2ef9011e2be04470865d494440be834508620c1618b29d570739bb6fca9c96be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logRelease files / pyganak-2.8.0-cp39-cp39-macosx_11_0_arm64.whl
| Download URL | pyganak-2.8.0-cp39-cp39-macosx_11_0_arm64.whl |
|---|---|
| Size | 5.4 MB |
| Tags | CPython 3.9 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
409f0f4b92732f1cc69740d88e612802139aa96c8fc13de9fdd71a4cb6810d2f
|
|
BLAKE2b-256 checksum How to use checksums |
11aad257e4f6ad72d60507d79532aa6c609c12d147b9a90710d5278997fd50b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency logSecurity for pyganak 2.8.0
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 15 files in this release have been uploaded with provenance metadata.
Attestation for pyganak-2.8.0-cp313-cp313-manylinux_2_34_x86_64.whl
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 Oct 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.