A boolean gate circuit description package for saving, loading, and evaluating logic circuits.
Project description
circuit-static-description
A boolean gate circuit description package with compact binary files and a readable legacy text format.
Installation
Install from PyPI:
pip install circuit-static-description
Python package usage
This project provides a Python package named circuit_static_description. The package supports saving circuits, loading circuits, and evaluating outputs.
Importing
from circuit_static_description import Circuit
Circuit file formats
Circuit files can be saved in two formats:
- Binary format, the default for
Circuit.save(...). It stores the parsed expression tree using a compact opcode and varuint encoding, so loading avoids reparsing text expressions. - Text format, kept for compatibility and readability. Old text files continue to load normally.
Circuit.load(...) automatically detects binary files by their file header. If the header is not present, it reads the file as UTF-8 text.
The text description contains the number of inputs, the number of outputs, optional intermediate variables, and the expression for each output.
- Text comments start with
#. The parser ignores everything from#to the end of the line, so comments may appear on their own line or after a definition. - Input references use
I0,I1,I2, etc. - Intermediate variables use
V0,V1,V2, etc. OnlyVfollowed by an integer is accepted as a variable name. - Variable definitions use
V<number> = expressionand may reference inputs and earlier or later variables. - Output lines use fixed names
OUT0,OUT1, etc. - Output definitions may reference inputs, variables, and supported logic operators.
- Boolean literals
TrueandFalseare accepted anywhere an expression is accepted. Literal parsing is case-insensitive. - Supported logic operators:
AND,OR,NOT,XOR,NAND,NOR. - Circular variable dependencies and undefined variable references are rejected when the circuit is loaded or parsed.
Example:
# This is a readable text circuit file.
INPUTS 3
OUTPUTS 2
V0 = AND(I0, I1)
V1 = XOR(V0, I2)
OUT0 = V0
OUT1 = NOR(False, V1) # trailing comments are allowed
Saving a circuit
from circuit_static_description import Circuit
circuit = Circuit(
input_count=3,
output_count=2,
variables=[
("V0", "AND(I0, I1)"),
("V1", "XOR(V0, I2)"),
],
outputs=[
"V0",
"NOR(I2, V1)",
],
)
circuit.save("example.circuit") # binary by default
circuit.save("example-text.circuit", mode="text")
circuit.save("example-binary.circuit", mode="binary")
save(...) simplifies the circuit before writing by default. Pass simplify=False to preserve the original expression text:
circuit.save("example-text.circuit", mode="text", simplify=False)
You can also simplify manually. simplify() returns a new Circuit and leaves the original object unchanged.
simplified = circuit.simplify()
Simplification folds constant-only logic, applies boolean identities such as AND(True, X) = X and XOR(True, X) = NOT(X), and extracts repeated gate expressions into new intermediate variables. Existing variable names keep their original V<number> names.
Loading a circuit
from circuit_static_description import Circuit
circuit = Circuit.load("example.circuit")
Evaluating a circuit
result = circuit.evaluate([1, 0, 1])
print(result)
# Example output: [0, 0]
By default, evaluate(...) returns all declared outputs in OUT0, OUT1, ... order. You can also request specific outputs or intermediate variables with targets:
values = circuit.evaluate([1, 0, 1], targets=["V0", "OUT1"])
print(values)
# Example output: [0, 0]
targets accepts either one name such as "V0" or a list of names such as ["OUT0", "V1"]. Target names must be OUT<number> or V<number>, and results are returned in the same order as requested.
Notes
evaluateaccepts a list of input values in input order.- The output is returned as a list of
0or1values. - Intermediate variables can be read with
evaluate(inputs, targets="V0")or mixed with outputs usingtargets=["OUT0", "V1"]. - Custom variable names are not supported. Intermediate variables must be named
V0,V1,V2, etc. - Old files without variable definitions still load normally.
Benchmarking sequential evaluation
The repository includes a local benchmark script in tests/benchmark.py for measuring sequential Circuit.evaluate(...) performance on random circuits.
The benchmark script uses tqdm to show progress while running evaluation loops.
Run the benchmark from the project root with the Poetry environment active:
python tests/benchmark.py
For a shorter run, use the quick mode:
python tests/benchmark.py --quick
This script is for local testing only and is not included in the published PyPI package.
Benchmark details
tests/benchmark.pygenerates a random circuit with input size, output size, and depth.- It performs a small calibration run first to choose a comfortable workload that should finish in under 5 minutes.
- It reports the sequential evaluation time for the generated circuit.
Example output
Circuit benchmark
Input count: 16, output count: 128, depth: 6
Rounds: 200
Sequential time: 2.1234s
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file circuit_static_description-0.1.8.tar.gz.
File metadata
- Download URL: circuit_static_description-0.1.8.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.11.15 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06dc12bd813b6c62b29c380f0b682995d80c8251301bd4c6ea5713bfe0f1a161
|
|
| MD5 |
91cf6494bcfdaaac8faed3d3338ea690
|
|
| BLAKE2b-256 |
587fc9429127d5704c3a7960ba00559b430a16fef032bbd98358b14ae4a017db
|
File details
Details for the file circuit_static_description-0.1.8-py3-none-any.whl.
File metadata
- Download URL: circuit_static_description-0.1.8-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.11.15 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea740ee034e6c4a556953d6fe369debc8c8e4e833a6bc343df7cabf33f7d61f9
|
|
| MD5 |
d67237c8b7eefec608c948d10117a2d8
|
|
| BLAKE2b-256 |
2962ffa7efe85ff35638e387a3227e86db256f9a0f9e7cc4657676301fa952cb
|