-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquinetemp.py
203 lines (168 loc) · 5.12 KB
/
quinetemp.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from qiskit import QuantumProgram
Q_obj = QuantumProgram() #Quantum object created
backend = 'local_qasm_simulator' #Backend is chosen
inputs = []
terms = []
d = []
m = int(input("Enter the number of qubits involved: "))
qr = Q_obj.create_quantum_register("qr", m)
cr = Q_obj.create_classical_register("cr", m)
qc = Q_obj.create_circuit(None, [qr], [cr])
filename = input("Enter name of the file: ")
access_mode = input("Enter file mode")
file_object = open(file_name_input(), access_mode)
for i in range(0, m):
inputs.append(chr(i + 65))
for i in range(pow(2, m)):
row[i] = file_object.readline().rstrip()
row_qubits[i] = row[i].split("-")
if (row_qubits[i][1] == '1'):
terms.append(i)
terms += d
final_exp = '' + booleanReduction(terms)
print(final_exp)
expression = ''.join(expression.split())
list_expression = expression.split('+')
no_of_terms = len(list_expression)
not_gate_qubits = []
for i in range(0, no_of_terms):
sub_term = list_expression[i]
list_sub_term = sub_term.split('.')
no_of_sub_terms = len(list_sub_term)
for j in range(0, no_of_sub_terms):
each_bit = list_sub_term[j]
if (len(each_bit) > 1):
not_gate_qubits.append(each_bit[0])
while (no_of_sub_terms <= 3):
list_sub_term.append(0)
no_of_sub_terms = no_of_sub_terms + 1
for j in range(0, len(not_gate_qubits)):
each_bit = not_gate_qubits[j]
qc.x(qr[ord(each_bit) - 65])
for j in range(0, no_of_sub_terms - 1):
each_bit_1 = list_sub_term[j]
each_bit_2 = list_sub_term[j + 1]
qc.cx(qr[ord(each_bit_1) - 65], qr[ord(each_bit_2) - 65])
list_sub_term.clear()
class Expression:
def __init__(self, x, l):
self.prime = True
self.m = [x]
self.val = bin(x)[2:]
while (len(self.val) < l):
self.val = '0' + self.val
self.ones = self.val.count('1')
def hd(self, x):
hd, pos = 0, 0
for i in range(len(x.val)):
if (self.val[i] != x.val[i]):
hd += 1
pos = i
if (hd > 1):
return -1
if (hd == 1 and (x.val.find('-') == self.val.find('-'))):
return pos
else:
return -1
def msIn(self, x):
ret = []
for i in self.m:
if (i in x):
ret += [i]
return ret, len(ret)
def __str__(self):
return self.val
def __eq__(self, x):
return self.val == x.val
def __lt__(self, x):
return self.m < x.m
def setTerms(terms, inputs):
fo = (len(terms), len(inputs))
for i in range(fo[0]):
terms.append(Expression(terms[i], fo[1]))
return terms[int(len(terms) / 2):]
def combineTerms(x): # O(n^2 +n) :(
ret = []
for i in x:
for j in x:
buf = i.hd(j)
if ((buf != -1) and (j.ones - i.ones == 1)):
i.prime = False
j.prime = False
fo = list(i.val)
v = Expression(1, 4)
v.m = []
v.m += i.m
v.m += j.m
fo[buf] = '-'
v.val = ''.join(fo)
v.ones = v.val.count('1')
ret.append(v)
for i in x:
if i.prime == True:
ret.append(i)
return ret
def lettersFromBinary(x):
ret = ''
for i in range(len(x)):
if (x[i] == '0'):
ret += inputs[i] + '|' + '.'
elif (x[i] == '1'):
ret += inputs[i] + '.'
return ret[:len(ret) - 1]
def expression_output(x):
buf = ''
for i in x:
fo = lettersFromBinary(i.val)
if (fo != ''):
buf += fo + ' + '
return buf[:len(buf) - 3]
def sizeImpl(x):
while (True):
buf = combineTerms(x)
if (x == buf):
break
x = buf
return x
def obtainGroups(x):
buf = list(x)
for i in range(len(x)):
if (x.count(x[i]) == 2) and x[i].val != '':
x[i].val = ''
buf.remove(x[i])
return buf
def manipulation(x):
ms = {}
ret = []
for i in x:
for k in i.m:
try:
ms[k].append(i)
except:
ms[k] = [i]
for i in ms:
if (len(ms[i]) == 1 and i not in d):
for j in ms[i]:
if (j not in ret):
ret.append(j)
for i in ret:
for j in i.m: ms.pop(j, None)
for i in d:
ms.pop(i, None)
while (len(ms) != 0):
currentLength, currentGroups, prime = 0, 0, 0
for i in ms:
for j in ms[i]:
nextGroups, nextLength = j.msIn(ms.keys())
if (nextLength > currentLength):
currentLength = nextLength
currentGroups = nextGroups
prime = j
ret.append(prime)
for i in currentGroups:
ms.pop(i, None)
return ret
def booleanReduction(x):
x = manipulation(obtainGroups(sizeImpl(setTerms(x, inputs))))
reduced_exp = '' + expression_output(x)
return reduced_exp