Python toolkit to support fundamental energy limits and reversible computing research
Project description
Landauer
Python toolkit to support fundamental energy limits and reversible computing research
Install
Installing this Python package:
python3 -m pip install landauer
Modules
Parse
Parses a hardware description file (written using a Verilog subset) and returns an and-inverter graph (as an instance of NetworkX DiGraph):
import landauer.parse as parse
half_adder = '''
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
'''
aig = parse.parse(half_adder)
print(aig)
# DiGraph with 8 nodes and 10 edges
You can enable majority-gates support using the switch
--majority_support
. In this case the output is an AIG superset where a node with three inputs (e.g. a, b, and c) is equivalent to(a & b) | (a & c) | (b & c)
.
Verilog subset supported: Single module description. Restricted to
input
,output
, andwire
declarations (registers nor arrays are supported). Usage of identifiers before their proper declaration is currently not supported.
You can also use this module via the command line. The output is a JSON-serialized NetworkX DiGraph in an adjacency format:
cat << EOF | python -m landauer.parse --stdin
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
EOF
# {"directed": true, "multigraph": false, "graph": [], "nodes": [{"id": "a"}, {"id": 1}, {"id": "b"}, {"id": 2}, {"id": 3}, {"id": "sum"}, {"id": 4}, {"id": "cout"}], "adjacency": [[{"inverter": false, "id": 1}, {"inverter": true, "id": 2}, {"inverter": false, "id": 4}], [{"inverter": true, "id": 3}], [{"inverter": true, "id": 1}, {"inverter": false, "id": 2}, {"inverter": false, "id": 4}], [{"inverter": true, "id": 3}], [{"inverter": true, "id": "sum"}], [], [{"inverter": false, "id": "cout"}], []]}
Simulate
Simulates the design for all possible inputs and calculates the entropy for some specific signal sets. We can list the following sets for each gate: inputs, output, and every output-input combination.
import landauer.parse as parse
import landauer.simulate as simulate
half_adder = '''
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
'''
simulation = simulate.simulate(parse.parse(half_adder))
print(simulation)
# {frozenset({'b', 'a'}): 2.0, frozenset({1}): 0.8112781244591328, frozenset({1, 'b'}): 1.5, frozenset({1, 'a'}): 1.5, frozenset({1, 'b', 'a'}): 2.0, frozenset({2}): 0.8112781244591328, frozenset({2, 'b'}): 1.5, frozenset({2, 'a'}): 1.5, frozenset({2, 'b', 'a'}): 2.0, frozenset({4}): 0.8112781244591328, frozenset({'b', 4}): 1.5, frozenset({4, 'a'}): 1.5, frozenset({'b', 4, 'a'}): 2.0, frozenset({1, 2}): 1.5, frozenset({3}): 1.0, frozenset({1, 3}): 1.5, frozenset({2, 3}): 1.5, frozenset({1, 2, 3}): 1.5}
The output is a Python dictionary where the key is the signal set (as a Python
frozenset
), and the value is the entropy (in bits).
You can also use this module via the command line:
cat << EOF | python -m landauer.parse --stdin | python -m landauer.simulate --stdin
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
EOF
# [{"variables": ["a", "b"], "entropy": 2.0}, {"variables": [1], "entropy": 0.8112781244591328}, {"variables": [1, "a"], "entropy": 1.5}, {"variables": [1, "b"], "entropy": 1.5}, {"variables": [1, "a", "b"], "entropy": 2.0}, {"variables": [2], "entropy": 0.8112781244591328}, {"variables": [2, "a"], "entropy": 1.5}, {"variables": [2, "b"], "entropy": 1.5}, {"variables": [2, "a", "b"], "entropy": 2.0}, {"variables": [4], "entropy": 0.8112781244591328}, {"variables": ["a", 4], "entropy": 1.5}, {"variables": [4, "b"], "entropy": 1.5}, {"variables": ["a", 4, "b"], "entropy": 2.0}, {"variables": [1, 2], "entropy": 1.5}, {"variables": [3], "entropy": 1.0}, {"variables": [1, 3], "entropy": 1.5}, {"variables": [2, 3], "entropy": 1.5}, {"variables": [1, 2, 3], "entropy": 1.5}]
Evaluate
Calculates the total entropy loss and losses for each gate given circuit and its simulation data.
import landauer.parse as parse
import landauer.simulate as simulate
import landauer.evaluate as evaluate
half_adder = '''
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
'''
aig = parse.parse(half_adder)
simulation = simulate.simulate(aig)
entropy = evaluate.evaluate(aig, simulation)
print(entropy)
# {'gates': {1: 1.188721875540867, 2: 1.188721875540867, 3: 0.5, 4: 1.188721875540867}, 'total': 4.066165626622601}
You may provide an optimized circuit instead (check
Naive
module below). However, you should provide the simulation result from the original circuit.
You can also use this module via the command line:
cat << EOF > half_adder.v
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
EOF
python -m landauer.parse --file half_adder.v > half_adder.json
python -m landauer.simulate --file half_adder.v > simulation.json
python -m landauer.evaluate simulation.json --file half_adder.json
# {"total": 4.066165626622601, "gates": [{"gate": 1, "loss": 1.188721875540867}, {"gate": 2, "loss": 1.188721875540867}, {"gate": 3, "loss": 0.5}, {"gate": 4, "loss": 1.188721875540867}]}
Naive
Optimizes the circuit using the heuristics from "CHAVES, J. et all. Designing Partially Reversible Field-Coupled Nanocomputing Circuits. IEEE Transactions on Nanotechnology, Volume 18, 2019." There are two strategies: The first one is more conservative and doesn't change the circuit depth (delay-oriented). The second one is more aggressive and may change the circuit depth. However, this last one (energy-oriented) leverages better energy optimization.
import landauer.parse as parse
import landauer.simulate as simulate
import landauer.evaluate as evaluate
import landauer.naive as naive
half_adder = '''
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
'''
aig = parse.parse(half_adder)
simulation = simulate.simulate(aig)
print(evaluate.evaluate(aig, simulation))
# 4.066165626622601
delay_oriented = naive.naive(aig, naive.Strategy.DELAY_ORIENTED)
print(evaluate.evaluate(delay_oriented, simulation))
# 4.066165626622601
energy_oriented = naive.naive(aig, naive.Strategy.ENERGY_ORIENTED)
print(evaluate.evaluate(energy_oriented, simulation))
# 1.688721875540867
Please notice that the delay-oriented strategy couldn't find any opportunity to propagate inputs (to avoid information/entropy losses). On the other hand, the energy-oriented heuristic reduced the entropy losses from 4.07 to 1.69.
You can also use this module via command line. The output is a JSON-serialized NetworkX MultiDiGraph in an adjacency format.
cat << EOF > half_adder.v
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
EOF
python -m landauer.parse --file half_adder.v > half_adder.json
python -m landauer.simulate --file half_adder.v > simulation.json
python -m landauer.evaluate simulation.json --file half_adder.json
python -m landauer.naive energy_oriented --file half_adder.json
# {"directed": true, "multigraph": true, "graph": [], "nodes": [{"level": 0, "id": "a"}, {"level": 1, "id": 1}, {"level": 0, "id": "b"}, {"level": 2, "id": 2}, {"level": 3, "id": 3}, {"level": 4, "id": "sum"}, {"level": 3, "id": 4}, {"level": 4, "id": "cout"}], "adjacency": [[{"inverter": false, "id": 1, "attributes": {"color": "#0173b2"}, "key": 0}], [{"inverter": true, "id": 3, "key": 0}, {"forward": true, "inverter": true, "attributes": {"color": "#0173b2"}, "id": 2, "key": "a"}, {"forward": true, "inverter": true, "attributes": {"color": "#56b4e9"}, "id": 2, "key": "b"}], [{"inverter": true, "id": 1, "attributes": {"color": "#56b4e9"}, "key": 0}], [{"inverter": true, "id": 3, "key": 0}, {"forward": true, "inverter": true, "attributes": {"color": "#0173b2"}, "id": 4, "key": "a"}, {"forward": true, "inverter": false, "attributes": {"color": "#56b4e9"}, "id": 4, "key": "b"}], [{"inverter": true, "id": "sum", "key": 0}], [], [{"inverter": false, "id": "cout", "key": 0}], []]}
Graph
Generates a DOT file given an and-inverter graph. There are two visualization modes: default
and paper
. Input propagations are represented by colored edges. Inverters are represented by dashed edges.
import landauer.parse as parse
import landauer.naive as naive
import landauer.graph as graph
half_adder = '''
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
'''
aig = parse.parse(half_adder)
energy_oriented = naive.naive(aig, naive.Strategy.ENERGY_ORIENTED)
print(graph.default(energy_oriented))
# digraph {
# a
# 1
# b
# 2
# 3
# sum
# 4
# cout
# a -> 1 [color="#0173b2" style=solid]
# 1 -> 3 [style=dashed]
# 1 -> 2 [color="#0173b2" style=dashed]
# 1 -> 2 [color="#56b4e9" style=dashed]
# b -> 1 [color="#56b4e9" style=dashed]
# 2 -> 3 [style=dashed]
# 2 -> 4 [color="#0173b2" style=dashed]
# 2 -> 4 [color="#56b4e9" style=solid]
# 3 -> sum [style=dashed]
# 4 -> cout [style=solid]
#}
You can call the method show
to display the graph using matplotlib
:
graph.show(graph.default(energy_oriented))
You can also use this module via command line:
cat << EOF | python -m landauer.parse --stdin | python -m landauer.naive energy_oriented --stdin | python -m landauer.graph --type paper --stdin --show
module half_adder (a, b, sum, cout);
input a, b;
output sum, cout;
assign sum = a ^ b;
assign cout = a & b;
endmodule
EOF
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
File details
Details for the file landauer-0.0.1.tar.gz
.
File metadata
- Download URL: landauer-0.0.1.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8103b6e69dd091b4c18ead16d2eb71202cf3be115ab3ff85f3e2f1dcd51e5304 |
|
MD5 | e552a980426030429dc009e59f80da25 |
|
BLAKE2b-256 | d64ecd82a99bbb2e01b7df6ae506783e1cf1728ad00dfdf43ed08dd0f70efbbf |
File details
Details for the file landauer-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: landauer-0.0.1-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd10788c5039f222cd64fd2693b89e15b8bd66c097d0669d45ead3f9f8a4ce5f |
|
MD5 | 06bb0b0878df7224d4f01be8187872de |
|
BLAKE2b-256 | 1f85893372d9938ad646904af36777d01b55e374c85c2c9c3d5b76ac9cf23982 |