Skip to main content

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

Installation and Usage

This library is available as a package on PyPI:

python -m pip install circuit

The library can be imported in the usual way:

import circuit
from circuit import *

Examples

This library makes 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 gate list associated with a circuit can be converted into a concise human-readable format, enabling manual inspection of the circuit:

>>> c.gates.to_legible()
(('id',), ('id',), ('and', 0, 1), ('id', 2))

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 organization of input gates and output gates into distinct bit vectors of specific lengths) 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.not_, [g4])
>>> g6 = 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 which an output gate cannot be reached (such as g5 in the example above). Doing so does not change the order of the input gates or the order of the output gates:

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

Other methods make it possible to discard a gate, to replace a collection of gates, and to convert a circuit into a boolean function. Descriptions and examples of these and other methods can be found in the documentation for the main library module.

Development

All installation and development dependencies are fully specified in pyproject.toml. The project.optional-dependencies object is used to specify optional requirements for various development tasks. This makes it possible to specify additional options (such as docs, lint, and so on) when performing installation using pip:

python -m pip install .[docs,lint]

Documentation

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

python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make html

Testing and Conventions

All unit tests are executed and their coverage is measured when using pytest (see the pyproject.toml file for configuration details):

python -m pip install .[test]
python -m pytest

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

python src/circuit/circuit.py -v

Style conventions are enforced using Pylint:

python -m pip install .[lint]
python -m pylint src/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.

Publishing

This library can be published as a package on PyPI by a package maintainer. First, install the dependencies required for packaging and publishing:

python -m pip install .[publish]

Ensure that the correct version number appears in pyproject.toml, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has an automation rule that activates and sets as the default all tagged versions. Create and push a tag for this version (replacing ?.?.? with the version number):

git tag ?.?.?
git push origin ?.?.?

Remove any old build/distribution files. Then, package the source into a distribution archive using the wheel package:

rm -rf build dist src/*.egg-info
python -m build --sdist --wheel .

Finally, upload the package distribution archive to PyPI using the twine package:

python -m twine upload dist/*

Release files for circuit 2.0.1

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

Source distribution (sdist)

Source distribution for circuit 2.0.1
File Size Uploaded
circuit-2.0.1.tar.gz 19.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for circuit 2.0.1
File Interpreter ABI Platform
circuit-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.6 kB

Release files / circuit-2.0.1.tar.gz

Download URL circuit-2.0.1.tar.gz
Size 19.1 kB
Tags Source
SHA-256 checksum
How to use checksums
322d13757a1054f1fd8d4d4788b5daed1b61323c8c42e9e96d71694daf6bb65f
BLAKE2b-256 checksum
How to use checksums
1a6228ba1c1f36bbacbd7f4524e08d34e07879b9d034bfda2734649b2b2ba9d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.2

Release files / circuit-2.0.1-py3-none-any.whl

Download URL circuit-2.0.1-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c59496f816b3cbd58e8c6a9d6c488cef3ce7a24dfbb32b44c40da6c3082bb61c
BLAKE2b-256 checksum
How to use checksums
13020a91a4d01af8340cd5bbaf4a1889e51d4b11303e9fd8edbc8cc9c8d98101
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.2

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

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