Skip to content

Commit

Permalink
Example logic netlist
Browse files Browse the repository at this point in the history
  • Loading branch information
kb1dds committed Jul 26, 2024
1 parent f0b19f7 commit 1cfe60a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pysheaf/logic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
import numpy as np
import networkx as nx
import pysheaf as ps
import matplotlib.pyplot as plt

netlist=json.load(open('logic_netlist.json'))

shf = ps.Sheaf()
shf.fromNetlist(netlist)
shf.mNumpyNormType = 2

pos=nx.layout.spring_layout(shf)
nx.draw_networkx_labels(shf,pos)
nx.draw_networkx_edges(shf,pos)
plt.show()

for a in [0,1]:
for b in [0,1]:
for c in [0,1]:
q = min((1-a*b)+c,1)

shf.GetCell('A').SetDataAssignment(ps.Assignment('cell',np.array((a,))))
shf.GetCell('B').SetDataAssignment(ps.Assignment('cell',np.array((b,))))
shf.GetCell('C').SetDataAssignment(ps.Assignment('cell',np.array((c,))))

shf.FuseAssignment()

qp = shf.GetCell('Q').mDataAssignment

print('A={}, B={}, C={}, Q={} =?= {} '.format(a,b,c,q,qp))

67 changes: 67 additions & 0 deletions pysheaf/logic_netlist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{ "AND2" : { "data_dimension" : 2,
"bounds" : "[(0,1)]*2",
"ports" : { "IN1" : "lambda x: x[0]",
"IN2" : "lambda x: x[1]",
"OUT" : "lambda x: x[0]*x[1]" }
},
"OR2" : { "data_dimension" : 2,
"bounds" : "[(0,1)]*2",
"ports" : { "IN1" : "lambda x: x[0]",
"IN2" : "lambda x: x[1]",
"OUT" : "lambda x: min(x[0]+x[1],1)" }
},
"NOT" : { "data_dimension" : 1,
"bounds" : "[(0,1)]",
"ports" : { "IN" : "lambda x: x[0]",
"OUT" : "lambda x: 1-x[0]" }
},
"A": { "data_dimension" : 1,
"bounds" : "[(0,1)]",
"optimize" : 0,
"connections" : [
{ "part" : "AND2",
"port" : "IN1"}
]
},
"B": { "data_dimension" : 1,
"bounds" : "[(0,1)]",
"optimize" : 0,
"connections" : [
{ "part" : "AND2",
"port" : "IN2"}
]
},
"C" : { "data_dimension" : 1,
"bounds" : "[(0,1)]",
"optimize" : 0,
"connections" : [
{ "part" : "OR2",
"port" : "IN1"}
]
},
"E" : { "data_dimension" : 1,
"bounds" : "[(0,1)]",
"connections" : [
{ "part" : "AND2",
"port" : "OUT"},
{ "part" : "NOT",
"port" : "IN"}
]
},
"F" : { "data_dimension" : 1,
"bounds" : "[(0,1)]",
"connections" : [
{ "part" : "NOT",
"port" : "OUT"},
{ "part" : "OR2",
"port" : "IN2"}
]
},
"Q" : { "data_dimension" : 1,
"bounds" : "[(0,1)]",
"connections" : [
{ "part" : "OR2",
"port" : "OUT"}
]
}
}

0 comments on commit 1cfe60a

Please sign in to comment.