/
builder.py
89 lines (78 loc) · 2.86 KB
/
builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import qm
import wire
import basics
import agenda
def get_wire(variable, bit_val, wires, the_agenda):
in_wire = wire.wire()
output_wire = in_wire
# check whether the bit corresponding to the
# variable is turned on or not
if bit_val == qm.bit_one:
strvar = variable
else: # bit_zero
strvar = '~' + variable
output_wire = wire.wire()
basics.inverter(in_wire, output_wire, the_agenda)
# cache the wires to avoid duplicating inputs
if strvar not in wires:
wires[strvar] = output_wire
return wires[strvar]
def build_circuitry(wires, the_agenda, gate):
ckt_output = wires.pop()
while len(wires) > 0:
cur_output = wire.wire()
cur_wire = wires.pop()
gate(ckt_output, cur_wire, cur_output, the_agenda)
ckt_output = cur_output
return ckt_output
def build_and_circuitry(wires, the_agenda):
return build_circuitry(wires, the_agenda, basics.and_gate)
def build_or_circuitry(wires, the_agenda):
return build_circuitry(wires, the_agenda, basics.or_gate)
def build_minterm(minterm, variables, wires, the_agenda):
index, mwires = len(variables) - 1, []
for i in range(len(minterm)-1, -1, -1):
if minterm[i] != qm.bit_both: # don't need to add a wire for a bit not needed
mwires.append(get_wire(variables[index], minterm[i], wires, the_agenda))
index -= 1
return build_and_circuitry(mwires, the_agenda)
def make_circuit(minterms, variables, the_agenda):
wires = {}
mckts = [build_minterm(minterm, variables, wires, the_agenda)
for minterm in minterms]
return wires, build_or_circuitry(mckts, the_agenda)
def all_subsets(variables, wires, circuit, the_agenda):
import utils
index, numvars = 0, len(variables)
for subset in range(0, 2 ** numvars):
mask = 1 << (numvars - 1)
index = 0
while mask > 0:
variable = variables[index]
if mask & subset:
try:
wires[variable].set_signal(1)
print(variable, 1, end=' ')
except Exception: pass
else:
try:
wires[variable].set_signal(0)
print(variable, 0, end=' ')
except Exception: pass
mask >>= 1
index += 1
print("\n===========================================")
utils.propagate(the_agenda)
def main():
import utils
variables = ['A','B','C','D']
minterms = [[-1, 1, 0, 0], [1, -1, 1, -1], [1, 0, -1, -1]]
the_agenda = agenda.agenda()
wires, circuit = make_circuit(minterms, variables, the_agenda)
print(wires, circuit)
utils.probe('ckt', circuit, the_agenda)
wires['B'].set_signal(1)
utils.propagate(the_agenda)
#all_subsets(variables, wires, circuit, the_agenda)
if __name__ == '__main__':
main()