Skip to main content

py-aiger-coins

warning 3.0.0 and greater are a major rewrite of this code base. I am trying to port most of the useful features.

Build Status codecov Updates PyPI version License: MIT

Library for creating circuits that encode discrete distributions and Markov Decision Processes. The name comes from the random bit model of drawing from discrete distributions using coin flips.

Table of Contents

Install

To install this library run:

$ pip install py-aiger-coins

Note that to actually compute probabilities, one needs to install with the bdd option.

$ pip install py-aiger-coins[bdd]

For developers, note that this project uses the poetry python package/dependency management tool. Please familarize yourself with it and then run:

$ poetry install

Usage

py-aiger-coins extends the standard py-aiger-bv and py-aiger-discrete abstractions to allow for certain bits to be set via biased coins.

The library centers around the PCirc object. The easiest way to use py-aiger-coins is to throught the pcirc function.

import aiger_bv as BV
import aiger_coins as C


expr1 = BV.uatom(2, 'x') + BV.uatom(2, 'y')

# Create distribution over bits.
my_pcirc = C.pcirc(expr1) \
            .randomize({'x': {0: 1/6, 1: 2/6, 2: 3/6}})

assert my_pcirc.outputs == {expr1.output}

# This probablistic circuit uses 2 biased coins to represent this input.
assert my_pcirc.num_coins == 2
assert my_pcirc.coin_biases == (1/3, 1/2)

# 'x' input is replaced with coinflips.
assert my_pcirc.inputs == {'y'}

# Underlying circuit now has a single input representing coin inputs.
underlying_circ = my_pcirc.circ

assert underlying_circ.inputs == {'y', my_pcirc.coins_id}

Note that aiger_coins.PCirc implements the same API as aiger_bv and aiger_coins.

Sequential Circuit API

For example, sequential and parallel composition allow combining probablistic circuits.

incr = (BV.uatom(2, 'z') + 1)
adder = (BV.uatom(2, 'x') + BV.uatom(2, 'y')).with_output('z')

# Create distribution over bits.
pcirc = C.pcirc(adder)                                         \
         .randomize({'x': {0: 1/6, 1: 2/6, 2: 3/6}})
pcirc >>= incr

or

inc_x = C.pcirc(BV.uatom(2, 'x') + 1)          \
         .randomize({'x': {0: 1/3, 2: 2/3}})                      # Pr(x=2) = 2/3

inc_y = C.pcirc(BV.uatom(3, 'y') + 1)          \
         .randomize({'y': {0: 1/3, 5: 1/3, 3: 1/3}})

inc_xy = inc_x | inc_y  #

Similarly, unroll, loopback are also implemented.

note unroll combines all coin flips into a single input in temporal order.

Finite Functions

py-aiger-coins also works well with the py-aiger-discrete API for working with arbitrary functions over finite sets. For example:

from bidict import bidict  # `pip install bidict`

# Create encoder/decoder for dice.
lookup = bidict({0: '⚀', 1: '⚁', 2: '⚂', 3: '⚃'})     # invertable dictionary.
encoder = aiger_discrete.Encoding(decode=lookup.get, encode=lookup.inv.get)

# Represent dice with a 2 bit vector.
expr1 = BV.uatom(2, '🎲')

# Add encoded dice to x. Because why not.
expr2 = BV.uatom(2, 'x') + expr1
func = aiger_discrete.from_aigbv(
    expr2.aigbv, input_encodings={'🎲': encoder}
)

# Create distribution over bits.
circ = C.pcirc(func) \
        .randomize({'🎲': {'⚀': 1/6, '⚁': 2/6, '⚂': 3/6}})

assert circ.inputs == {'x'}
assert circ.outputs == {expr2.output}
assert circ.coin_biases == (Fraction(1, 3), Fraction(1, 2))

Metadata

Release files for py-aiger-coins 3.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-aiger-coins 3.3.7
File Size Uploaded Provenance Actions
py-aiger-coins-3.3.7.tar.gz 9.1 kB Without provenance Details for py-aiger-coins-3.3.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-aiger-coins 3.3.7
File Interpreter ABI Platform Actions
py_aiger_coins-3.3.7-py3-none-any.whl Python 3 none any Details for py_aiger_coins-3.3.7-py3-none-any.whl

Total release size: 17.8 kB

Release files / py-aiger-coins-3.3.7.tar.gz

Download URL py-aiger-coins-3.3.7.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
49e9130d8126efb572f2113dbdcc36fe0f6db0f98368e21990c50c473121043c
BLAKE2b-256 checksum
How to use checksums
22b521ef8c98a626db6a4ba81f00fd97632a5b3816fa72c459c4d5e3329f82ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.9.13 Linux/6.0.0

Release files / py_aiger_coins-3.3.7-py3-none-any.whl

Download URL py_aiger_coins-3.3.7-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f6a3ae482bb73b6798bf1fd5c891dff59c0deda9d0e3b43cc3b223e2cd5051d8
BLAKE2b-256 checksum
How to use checksums
e6fda10c940dfeeaa6431f14d0c387868988675bae4d786e36c0b313879a970d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.9.13 Linux/6.0.0

Security for py-aiger-coins 3.3.7

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release

3.3.7 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page