-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.py
231 lines (143 loc) · 5.7 KB
/
cube.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Author: Edison Murairi
# August 12th, 2023
# Implemente the Cube class
from qiskit import QuantumCircuit , QuantumRegister
from qiskit.circuit.library import MCXGate
import string
from numpy import binary_repr
class Cube:
def __init__(self, expression):
""" instantiate the cube class
int n: lenght of the cube
string expression: B3 string
B3 = {0,1,-} -> negative, positive and don't care
"""
self.expression = expression
def weight(self):
return sum(self.expression[c] != "-" \
for c in range(len(self.expression)))
def evaluate_input(self, input):
assert len(self) == len(input)
for i in range(len(self)):
if self.expression[i] == "-" or input[i] == "-":
pass
else:
if self.expression[i] != input[i]:
return 0
return 1
def minterms_noterms(self):
""" return two lists: ones and zeros
ones --> all the inputs that the cube evaluates to 1
zeros --> all the inputs that the cube evaluates to 0
"""
minterms = []
noterms = []
for i in range(2**len(self)):
brepr = binary_repr(i, len(self))
if self.evaluate_input(brepr) == 0:
noterms.append(i)
else:
minterms.append(i)
return minterms, noterms
def distance(self, other):
assert len(self.expression) == len(other.expression)
return sum(self.expression[c] != other.expression[c] \
for c in range(len(self.expression)))
def xor_combine(self, other):
combination_map = {"01":"-", "10":"-", \
"0-":"1", "-0":"1", \
"1-":"0", "-1":"0"}
assert len(self.expression) == len(other.expression)
assert self.distance(other) == 1
new_expression = ""
for i in range(len(self.expression)):
if self.expression[i] != other.expression[i]:
a = self.expression[i]
b = other.expression[i]
try:
new_expression += str(combination_map[f"{a}{b}"])
except:
print("Weird in the combination")
return
else:
new_expression += self.expression[i]
return Cube(new_expression)
def copy(self):
""" create a new cube with the same data """
return Cube(self.expression)
def change_literal(self, pos, newlit):
new_expression = self.expression[:pos] + newlit + self.expression[pos+1:]
self.expression = new_expression
return
def expansion(self):
cubes_list = [self.copy()]
n = len(self.expression)
pos = 0
while pos < n:
tmp = []
for cube in cubes_list:
if cube.expression[pos] == "0":
exp1 = cube.expression[:pos] + "-" + cube.expression[pos+1:]
exp2 = cube.expression[:pos] + "1" + cube.expression[pos+1:]
tmp.append(Cube(exp1))
tmp.append(Cube(exp2))
if cube.expression[pos] == "1":
exp1 = cube.expression[:pos] + "-" + cube.expression[pos+1:]
exp2 = cube.expression[:pos] + "0" + cube.expression[pos+1:]
tmp.append(Cube(exp1))
tmp.append(Cube(exp2))
if cube.expression[pos] == "-":
exp1 = cube.expression[:pos] + "0" + cube.expression[pos+1:]
exp2 = cube.expression[:pos] + "1" + cube.expression[pos+1:]
tmp.append(Cube(exp1))
tmp.append(Cube(exp2))
cubes_list = tmp
pos += 1
return cubes_list
def toffoli_gate(self, labels = None, anc_label = None):
""" convert the cube to a multi-controlled not gate also called Tofolli gate
"""
if labels is None:
labels = 'q'
if anc_label is None:
anc_label = 'anc'
qr = QuantumRegister(len(self.expression), labels)
anc = QuantumRegister(1, anc_label)
qc = QuantumCircuit(qr, anc)
cares = []
negatives = []
for i in range(len(self.expression)):
if self.expression[i] == "0":
qc.x(qr[i])
cares.append(i)
negatives.append(i)
if self.expression[i] == "1":
cares.append(i)
gate = MCXGate(len(cares))
qc.append(gate, cares + [len(self.expression)])
for neg in negatives:
qc.x(neg)
return qc
def __len__(self):
return len(self.expression)
def __str__(self):
if self.expression == "-"*len(self.expression):
return "1"
result = ""
for i in range(len(self.expression) - 1):
if self.expression[i] == "0":
result += "~" + f"x{i} & "
elif self.expression[i] == "1":
result += f"x{i} & "
i = len(self.expression) - 1
if self.expression[i] == "0":
result += "~" + f"x{i}"
elif self.expression[i] == "1":
result += f"x{i}"
if result == "":
return result
if result[-1] == " ":
result = result[:-1]
if result[-1] == "&":
result = result[:-1]
return result