diff --git a/pysheaf/logic_example.py b/pysheaf/logic_example.py new file mode 100644 index 0000000..721f119 --- /dev/null +++ b/pysheaf/logic_example.py @@ -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)) + diff --git a/pysheaf/logic_netlist.json b/pysheaf/logic_netlist.json new file mode 100644 index 0000000..296db62 --- /dev/null +++ b/pysheaf/logic_netlist.json @@ -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"} + ] + } +}