-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_circuit2.py
More file actions
101 lines (85 loc) · 2.28 KB
/
gen_circuit2.py
File metadata and controls
101 lines (85 loc) · 2.28 KB
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
90
91
92
93
94
95
96
97
98
99
100
from __future__ import print_function
import json
from random import SystemRandom
"""
Generates circuit having N gates
in a linear fashion as shown:
---||
||---||
---|| ||---||
--|| ||--- ....
--||
"""
def get_gate_type():
opt = SystemRandom().randint(1,3)
if opt == 1:
gate_type = "AND"
elif opt == 2:
gate_type = "XOR"
else:
gate_type = "OR"
return gate_type
def input_gates(N):
inputs = []
# adding first gate
gate_type = get_gate_type()
inputs.append([0,gate_type,[0,1]])
if N == 2:
return inputs
"""
for i in range(1,N-1):
ip = []
ip.append(i)
gate_type = get_gate_type()
ip.append(gate_type)
ip.append([i-1,i+1])
inputs.append(ip)
"""
return inputs
def inter_gates(N):
inter = []
# TODO - Take care of corner cases
# like N = 2
for i in range(1, N-1):
it = []
it.append(i)
gate_type = get_gate_type()
it.append(gate_type)
it.append([i-1, i+1])
it.append([False, True])
inter.append(it)
return inter
def output_gates(N,gate_type):
outputs = []
# gate_type of output gate = gate type of last input gate
outputs.append([N-2,gate_type,[N-3,N-1]])
return outputs
if __name__ == "__main__":
while True:
try:
N = int(raw_input("Enter number of bidders: "))
break
except ValueError:
print("Please enter valid input")
continue
on_input_gates = input_gates(N)
print("Input gates: ",on_input_gates)
on_mid_gates = []
print("Mid gates: ",on_mid_gates)
on_inter_gates = inter_gates(N)
print("Inter gates: ",on_inter_gates)
# last inter gate and output gate are the same
# hence gate types should match
op_gate_type = on_inter_gates[-1][1]
on_output_gates = output_gates(N, op_gate_type)
print("Output gates: ",on_output_gates)
# write to json
j = {
"num_inputs": N,
"on_input_gates": on_input_gates,
"mid_gates": on_mid_gates,
"inter_gates": on_inter_gates,
"output_gates": on_output_gates
}
with open("test/circuit.json", 'w') as f:
json.dump(j,f)