-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantum-maxcut-algorithm.py
359 lines (234 loc) · 7.77 KB
/
quantum-maxcut-algorithm.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
'''
(C) Renata Wong (NCTS-NTU) 2023
This is the accompanying code for the paper "Quantum speedup for the maximum cut problem"
for the example graph given in Fig. 1.
Note: It is impossible to execute it for graphs with more than 2 edges as the number of qubits exceeds the simulator limit.
'''
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
import numpy as np
'''
Create the quantum circuit for the 3-vertex example
num_vertices = n = number of vertices
num_enges = m = number of edges
'''
'''
PLEASE FILL IN THE EDGE LIST FOR YOUR GRAPH IN THE LINE BELOW
'''
permanent_edge_list = [[0,1], [1,2]]
num_vertices = len({x for l in permanent_edge_list for x in l})
num_edges = len(permanent_edge_list)
range_z = (((num_edges + 1) * (num_edges + 2)) / 2) - 1
range_r = 4 * (num_edges * (num_edges + 1)) / 2
range_s = 2 * (num_edges * (num_edges + 1)) / 2
aux = QuantumRegister(1, 'aux')
z_reg = QuantumRegister(range_z, 'z_reg')
s_reg = QuantumRegister(range_s, 's_reg')
r_reg = QuantumRegister(range_r, 'r_reg')
x_reg = QuantumRegister(num_vertices, 'x_reg')
readout = ClassicalRegister(num_vertices, 'out')
qc = QuantumCircuit(aux, x_reg, r_reg, s_reg, z_reg, readout)
qc.x(aux)
qc.h(aux)
# Print this variable to see the system size
system_size = qc.num_qubits
'''
Create z_matrix to store elements of z_reg
'''
z_matrix = [ [ 0 for i in range(num_edges + 1) ] for j in range(num_edges + 1) ]
zij = 0
for i in range(1, num_edges + 1):
for j in range(i + 1):
z_matrix[i][j] = zij
zij += 1
'''
Define the CFE subcircuit
'''
sq = QuantumRegister(10,'sq')
sc = QuantumCircuit(sq, name='CFE')
# EIIAC
sc.x(sq[1])
sc.ccx(sq[0], sq[1], sq[2])
sc.x(sq[0])
sc.x(sq[1])
sc.ccx(sq[0], sq[1], sq[3])
sc.x(sq[0])
sc.x(sq[2])
sc.x(sq[3])
sc.ccx(sq[2], sq[3], sq[6])
sc.x(sq[2])
sc.x(sq[3])
# EINIAC
sc.x(sq[0])
sc.x(sq[1])
sc.ccx(sq[0], sq[1], sq[4])
sc.x(sq[0])
sc.x(sq[1])
sc.ccx(sq[0], sq[1], sq[5])
sc.x(sq[4])
sc.x(sq[5])
sc.ccx(sq[4], sq[5], sq[7])
sc.x(sq[4])
sc.x(sq[5])
# CNOTS
sc.cx(sq[6], sq[9])
sc.cx(sq[7], sq[8])
cfe = sc.to_instruction()
'''
Define the CSE subcircuit
'''
cq = QuantumRegister(11,'cq')
ce = QuantumCircuit(cq, name='CSE')
# EIIAC
ce.x(cq[1])
ce.ccx(cq[0], cq[1], cq[2])
ce.x(cq[0])
ce.x(cq[1])
ce.ccx(cq[0], cq[1], cq[3])
ce.x(cq[0])
ce.x(cq[2])
ce.x(cq[3])
ce.ccx(cq[2], cq[3], cq[6])
ce.x(cq[2])
ce.x(cq[3])
# EINIAC
ce.x(cq[0])
ce.x(cq[1])
ce.ccx(cq[0], cq[1], cq[4])
ce.x(cq[0])
ce.x(cq[1])
ce.ccx(cq[0], cq[1], cq[5])
ce.x(cq[4])
ce.x(cq[5])
ce.ccx(cq[4], cq[5], cq[7])
ce.x(cq[4])
ce.x(cq[5])
# CNOTS
ce.ccx(cq[6], cq[8], cq[9])
ce.ccx(cq[7], cq[8], cq[10])
cse = ce.to_instruction()
'''
Initialize the system and set it in a uniform superpostion -> lines 1 and 2 of Algorithm 1 in paper
'''
for qubit in s_reg:
qc.x(qubit)
for qubit in x_reg:
qc.h(qubit)
qc.barrier()
'''
NOTE: There will always be an even number of solutions, since under maximum cut 101 is the same as 010.
For Fig. 1 in the paper, we set the number of solutions to 2.
YOU MAY NEED TO ADJUST THE NUMBER OF SOLUTIONS.
'''
num_solutions = 2
num_runs = int(np.ceil(np.pi * np.sqrt((2**num_vertices) / num_solutions)) / 4)
'''
Amplitude amplification
'''
for run in range(num_runs):
# Apply CFE to |psi_1> -> line 3 in Algorithm 1
# It is assumed that the two vertices in the x_reg share an edge
r = 4
s = 2
edge_list = permanent_edge_list.copy()
if len(edge_list) > 0:
index_v1 = edge_list[0][0]
index_v2 = edge_list[0][1]
edge_list.pop(0)
cfe_qubits = []
cfe_qubits += [x_reg[index_v1]]
cfe_qubits += [x_reg[index_v2]]
cfe_qubits += [r_reg[i] for i in range(4)]
cfe_qubits += [s_reg[i] for i in range(2)]
cfe_qubits += [z_reg[i] for i in range(2)]
qc.append(cfe, cfe_qubits)
# Apply CSE to |psi_2> --> line 4 in Algorithm 1
# It is assumed that the two vertices in the x_reg share an edge
for i in range(1, num_edges):
index_v1 = edge_list[0][0]
index_v2 = edge_list[0][1]
cse_qubits = []
for j in reversed(range(i+1)):
cse_qubits += [x_reg[index_v1]]
cse_qubits += [x_reg[index_v2]]
cse_qubits += [r_reg[i] for i in range(r, r+4)]
cse_qubits += [s_reg[i] for i in range(s, s+2)]
cse_qubits += [z_reg[z_matrix[i][j]]]
cse_qubits += [z_reg[z_matrix[i+1][j+1]]]
cse_qubits += [z_reg[z_matrix[i+1][j]]]
qc.append(cse, cse_qubits)
cse_qubits.clear()
r += 4
s += 2
edge_list.pop(0)
'''
Which qubit of register z_reg is used here depends on how many edges are there in the cut.
For the example in Fig. 1 we expect 2 edges, and therefore we choose qubit 2 (counting from 0, 1, 2, etc.).
This qubit should be in the state 1.
YOU MAY NEED TO ADJUST THE CONTROL QUBIT IN THE CX GATE.
'''
qc.barrier()
qc.cx(z_reg[len(z_reg)-1], aux)
qc.barrier()
'''
Uncompute CSE and CFE operations
'''
edge_list = permanent_edge_list.copy()
for i in reversed(range(1, num_edges)):
index_v1 = edge_list[len(edge_list) - 1][0]
index_v2 = edge_list[len(edge_list) - 1][1]
cse_qubits = []
for j in range(i+1):
r -= 4
s -= 2
cse_qubits += [x_reg[index_v1]]
cse_qubits += [x_reg[index_v2]]
cse_qubits += [r_reg[i] for i in range(r, r+4)]
cse_qubits += [s_reg[i] for i in range(s, s+2)]
cse_qubits += [z_reg[z_matrix[i][j]]]
cse_qubits += [z_reg[z_matrix[i+1][j+1]]]
cse_qubits += [z_reg[z_matrix[i+1][j]]]
qc.append(cse.inverse(), cse_qubits)
cse_qubits.clear()
edge_list.pop(0)
edge_list = [permanent_edge_list[0]]
if len(edge_list) > 0:
index_v1 = edge_list[0][0]
index_v2 = edge_list[0][1]
cfe_qubits = []
cfe_qubits += [x_reg[index_v1]]
cfe_qubits += [x_reg[index_v2]]
cfe_qubits += [r_reg[i] for i in range(4)]
cfe_qubits += [s_reg[i] for i in range(2)]
cfe_qubits += [z_reg[i] for i in range(2)]
qc.append(cfe.inverse(), cfe_qubits)
edge_list.pop()
'''
Diffusion operations
'''
qc.barrier()
for qubit in x_reg:
qc.h(qubit)
qc.x(qubit)
# apply CZ to x_reg
qc.h(x_reg[len(x_reg) - 1])
multiplexer = [x_reg[i] for i in range(len(x_reg) - 1)]
qc.mcx(multiplexer, x_reg[len(x_reg) - 1])
qc.h(x_reg[len(x_reg) - 1])
for qubit in x_reg:
qc.x(qubit)
qc.h(qubit)
qc.barrier()
'''
Measurement
'''
cuts = []
for i in range(len(x_reg)):
cuts.append(x_reg[i])
# Reverse the order in which the output is shown so that it can be read from left to right.
cuts.reverse()
qc.measure(cuts, readout)
from qiskit import Aer, execute
simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, backend = simulator, shots = 100).result()
counts = result.get_counts()
print(counts)