Skip to main content

Minimal native Python library for building and working with logical circuits.

Project description

Minimal native Python library for building and working with logical circuits.

PyPI version and link. Read the Docs documentation status. GitHub Actions status. Coveralls test coverage summary.

Package Installation and Usage

The package is available on PyPI:

python -m pip install circuit

The library can be imported in the usual way:

import circuit
from circuit import *

Examples

This library make it possible to programmatically construct logical circuits consisting of interconnected logic gates. The functions corresponding to individual logic gates are represented using the logical library. In the example below, a simple conjunction circuit is constructed, and its input and output gates (corresponding to the logical unary identity function) are created and designated as such:

>>> from circuit import circuit, op
>>> c = circuit()
>>> g0 = c.gate(op.id_, is_input=True)
>>> g1 = c.gate(op.id_, is_input=True)
>>> g2 = c.gate(op.and_, [g0, g1])
>>> g3 = c.gate(op.id_, [g2], is_output=True)
>>> c.count() # Number of gates in the circuit.
4

The circuit accepts two input bits (represented as integers) and can be evaluated on any list of two bits using the evaluate method. The result is a bit vector that includes one bit for each output gate.

>>> c.evaluate([0, 1])
[0]
>>> [list(c.evaluate(bs)) for bs in [[0, 0], [0, 1], [1, 0], [1, 1]]]
[[0], [0], [0], [1]]

Note that the order of the output bits corresponds to the order in which the output gates were originally introduced using the gate method. It is possible to specify the signature of a circuit (i.e., the number of input gates and the number of output gates) at the time the circuit object is created:

>>> from circuit import signature
>>> c = circuit(signature([2], [1]))
>>> g0 = c.gate(op.id_, is_input=True)
>>> g1 = c.gate(op.id_, is_input=True)
>>> g2 = c.gate(op.not_, [g0])
>>> g3 = c.gate(op.not_, [g1])
>>> g4 = c.gate(op.xor_, [g2, g3])
>>> g5 = c.gate(op.id_, [g4], is_output=True)
>>> [list(c.evaluate([bs])) for bs in [[0, 0], [0, 1], [1, 0], [1, 1]]]
[[[0]], [[1]], [[1]], [[0]]]

It is also possible to remove all internal gates from a circuit from which an output gate cannot be reached. Doing so does not change the order of the input gates or the order of the output gates:

>>> c.count()
6
>>> c.prune_and_topological_sort_stable()
>>> c.count()
5

Documentation

The documentation can be generated automatically from the source files using Sphinx:

cd docs
python -m pip install -r requirements.txt
sphinx-apidoc -f -E --templatedir=_templates -o _source .. ../setup.py && make html

Testing and Conventions

All unit tests are executed and their coverage is measured when using nose (see setup.cfg for configution details):

python -m pip install nose coverage
nosetests --cover-erase

Alternatively, all unit tests are included in the module itself and can be executed using doctest:

python circuit/circuit.py -v

Style conventions are enforced using Pylint:

python -m pip install pylint
pylint circuit

Contributions

In order to contribute to the source code, open an issue or submit a pull request on the GitHub page page for this library.

Versioning

Beginning with version 0.2.0, the version number format for this library and the changes to the library associated with version number increments conform with Semantic Versioning 2.0.0.

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

circuit-0.4.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

circuit-0.4.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file circuit-0.4.0.tar.gz.

File metadata

  • Download URL: circuit-0.4.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.26.0 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.0

File hashes

Hashes for circuit-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e5d778b019195af7b6d53472d10b6695812db2106aec4895ada08719f075583b
MD5 293476aa4f08b9a97b5f456459066481
BLAKE2b-256 0b95b8194660048ce20d53b8d8072c978880f1a2dd1e344d5cab53c7124be11a

See more details on using hashes here.

File details

Details for the file circuit-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: circuit-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.26.0 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.0

File hashes

Hashes for circuit-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2e0533d02f82f90334c9343b13fed92206a22b0c47ce36bdf7dfecc53202491
MD5 564d1629a316bba6c80e26648192cd6d
BLAKE2b-256 b0c71d14c66b15cb39f03435bf9016209590d255397137fa761430246115f963

See more details on using hashes here.

Supported by

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