-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZ2_symmetryBreak.py
executable file
·582 lines (342 loc) · 18.2 KB
/
Z2_symmetryBreak.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 16 09:14:38 2022
@author: ph30n1x
"""
"""
This code is for Calculating the P = 1 Landscape of MaxCut problems
that are defined using adjacency matrices C.
Input Needed::
1.) C: The adjacency matrix describing the MaxCut problem instance.
2.) Instance_list: The list of instances to be studied.
3.) N: The number of grid points along each gamma or beta axis.
4.) DList: The list of different bond-dimensions to be studied
Output Generated::
1.) Cost_mps: An [N x N] matrix storing the cost values corresponding to each (gamma,beta) for each Dmax studied.
This data is saved as a .npy file in the respective instance folders.
"""
import os
import tensornetwork as tn
from Gates import Gates as g
from Expectation import Expectation as exp
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
tn.set_default_backend("numpy")
#%%
##############################
## Parameter Initialization ##
##############################
q = 12 # number of Qubits
N = 100 # Number of sampling points in Gamma and Beta
pi = np.pi
Gamma = [0.0 + x*pi/(N-1) for x in range(N)] # List of 100 Gamma angles between [0 , pi]
Beta = [0.0 + x*0.50*pi/(N-1) for x in range(N)] # List of 100 Beta angles between [0 , pi/2]
G,B = np.meshgrid(Beta,Gamma) # Creating a mesh for the 3D plots
# DList = [64, 48, 32, 24, 16, 12, 8, 6, 4, 2] # List of Bond-dimensions to be simulated
DList = [64] # List of Bond-dimensions to be simulated
folder_location = os.path.dirname(os.path.realpath(__file__)) # Location of the MPS_QAOA Folder
local_location = '/QAOA/MaxCut/Erdos/MxC_Q'+str(q)+'/Q' #Location of files within the MPS_QAOA Folder
location = folder_location + local_location
tag = 'R' # R for Random
D_highest = int(2**np.floor(q/2)) # Highest possible bond-dimension for a q-qubit MPS
Normalize = False # Normalization constraint for the MPS-QAOA states. IMPORTANT: Keep False
Instance_list = [0] # The indices of the instances one wishes to run the calculations for.
# Eg: If q = 12, enter [0,42] if the instances of interest are 12R0 and 12R42
#%%
def get_unsymmetric_C(C):
'''
Function that takes an n-qubit Z2 symmetric MaxCut adjacency matrix C as input and returns
matrices J and h describing a modified n-1 qubit Ising Hamiltonian with broken Z2 symmetry
by classically fixing the first qubit as '0' and hence first spin == Z_0 as 1.
Input::
C : The adjacency matrix describing the q-qubit MaxCut problem instance.
Output::
J : The q-1 qubit Interactions matrix.
h : The q-1 qubit field interaction terms.
'''
J = 0.5*C[1:,1:]
h = 0.5*C[0,1:]
return J, h
#%%
def get_Cost_Z2broken(gamma_beta, J, h):
'''
Function that takes in the parameterized state gamma_beta and perturbation constant as
inputs to return the corresponding cost.
Input::
J : The q-1 qubit Interactions matrix.
h : The q-1 qubit field interaction terms.
gamma_beta : The parametrized q-1 qubit QAOA state whose cost is to be calculated.
Output::
Cost : The cost corresponding to the gamma_beta state.
'''
n = len(h)
Cost = 0
z_i = g.get_Z()
z_j = g.get_Z()
for i in range(n):
if (h[i] != 0):
g_b = gamma_beta.tensors
GB = tn.FiniteMPS(g_b, canonicalize = False)
GB_copy = tn.FiniteMPS(g_b, canonicalize = False)
GB.apply_one_site_gate(z_i,i)
ci = exp.exp_MPS(GB_copy,GB)
ci = np.real(ci)
Cost = Cost + h[i]*ci
for i in range(n-1):
for j in range((n-1),i,-1):
if (J[i,j] != 0):
g_b = gamma_beta.tensors
GB = tn.FiniteMPS(g_b, canonicalize = False)
GB_copy = tn.FiniteMPS(g_b, canonicalize = False)
GB.apply_one_site_gate(z_i,i)
GB.apply_one_site_gate(z_j,j)
ci = exp.exp_MPS(GB_copy,GB)
ci = np.real(ci)
Cost = Cost + J[i,j]*ci
return Cost
#%%
def get_Cost_Z2symmetric(gamma_beta, C):
'''
Function that takes in the parameterized state gamma_beta and perturbation constant as
inputs to return the corresponding cost.
Input::
C : The adjacency matrix describing the original q-qubit MaxCut problem instance.
gamma_beta : The parametrized q-1 qubit QAOA state whose cost is to be calculated.
Output::
Cost : The cost corresponding to the original MaxCut problem with |0> tensor |gamma,beta>.
'''
n = len(C)
if (len(gamma_beta.tensors) != n):
Zero = np.array([[[1],
[0]]],
dtype = np.complex128)
GB_list = gamma_beta.tensors.copy() # Copying as otherwise gamma_beta state is altered by insert.
GB_list.insert(0,Zero)
gamma_beta = tn.FiniteMPS(GB_list, canonicalize=False)
Cost = 0
z_i = g.get_Z()
z_j = g.get_Z()
for i in range(n-1):
for j in range((n-1),i,-1):
if (C[i,j] != 0):
g_b = gamma_beta.tensors
GB = tn.FiniteMPS(g_b, canonicalize = False)
GB_copy = tn.FiniteMPS(g_b, canonicalize = False)
GB.apply_one_site_gate(z_i,i)
GB.apply_one_site_gate(z_j,j)
ci = exp.exp_MPS(GB_copy,GB)
ci = np.real(ci)
Cost = Cost - 0.5*C[i,j]*(1 - ci)
return Cost
#%%
def QAOA_gamma_block_Z2broken(gamma_beta, gamma, J, h, Dmax):
'''
Function that takes in the parameter gamma and applies
a single layer of the gamma block within a single QAOA layer.
Input::
gamma_beta : An existing QAOA state upon which one applies the Gamma layer
gamma : The free parameter that needs to be optimized
J : The Interaction Energies matrix describing the EC3 problem instance.
h : The self-energy matrix describing the EC3 problem instance.
Dmax : The maximum bond-dimension limit imposed on the MPSs
Output::
gamma_beta : The QAOA state with an additional Cost layer of the QAOA added to it
'''
n = len(h)
l1 = int(np.floor((n)/2)) # Paramteres required for the SWAP network
l2 = int(np.floor((n-1)/2)) # Paramteres required for the SWAP network
gamma_beta = tn.FiniteMPS(gamma_beta.tensors, canonicalize=False)
gamma_beta.canonicalize(normalize=Normalize)
Q_ord = np.linspace(start = 0, stop = (n-1), num = n, dtype = int)
SWAP = g.get_SWAP()
for i in range(n):
Rz = g.get_Rz(2*gamma*h[i])
gamma_beta.apply_one_site_gate(Rz, i)
for i in range(n):
if (i < (n-1)):
for k in range(n-1):
if (Q_ord[k] < Q_ord[k+1]):
Jij = g.get_Jij(gamma, J[Q_ord[k]][Q_ord[k+1]])
gamma_beta.position(site=k, normalize=Normalize)
gamma_beta.apply_two_site_gate(Jij, site1 = k, site2 = (k+1),
max_singular_values=Dmax, center_position=k)
if (i%2 == 0):
for s in range(l1):
Q_ord[2*s],Q_ord[2*s+1] = Q_ord[2*s+1],Q_ord[2*s]
gamma_beta.position(site=(2*s), normalize=Normalize)
gamma_beta.apply_two_site_gate(SWAP, site1 = (2*s), site2 = (2*s+1),
max_singular_values=Dmax, center_position=(2*s))
else:
for s in range(l2):
Q_ord[2*s+1],Q_ord[2*s+2] = Q_ord[2*s+2],Q_ord[2*s+1]
gamma_beta.position(site=(2*s+1), normalize=Normalize)
gamma_beta.apply_two_site_gate(SWAP, site1 = (2*s+1), site2 = (2*s+2),
max_singular_values=Dmax, center_position=(2*s+1))
gamma_beta = gamma_beta.tensors[::-1]
gamma_beta = [gamma_beta[x].transpose([2,1,0]) for x in range(n)]
gamma_beta = tn.FiniteMPS(gamma_beta, canonicalize = False)
return gamma_beta
#%%
def QAOA_gamma_block_Z2symmetric(gamma_beta, gamma, C, Dmax):
'''
Function that takes in the parameter gamma and applies
a single layer of the gamma block within a single QAOA layer.
Input::
gamma_beta : An existing QAOA state upon which one applies the Gamma layer
gamma : The free parameter that needs to be optimized
C : The adjacency matrix describing the MaxCut problem instance.
Dmax : The maximum bond-dimension limit imposed on the MPSs
Perturbation_const : The perturbation constant added to break Z2 symmetry
Output::
gamma_beta : The QAOA state with an additional Cost layer of the QAOA added to it
'''
n = len(C)
l1 = int(np.floor((n)/2)) # Paramteres required for the SWAP network
l2 = int(np.floor((n-1)/2)) # Paramteres required for the SWAP network
gamma_beta = tn.FiniteMPS(gamma_beta.tensors, canonicalize=False)
gamma_beta.canonicalize(normalize=Normalize)
# Defining the SWAP network
Q_ord = np.linspace(start = 0, stop = (n-1), num = n, dtype = int)
SWAP = g.get_SWAP()
for i in range(n): #applying all the nearest neighbout gates
if (i < (n-1)):
for k in range(n-1):
if (Q_ord[k] < Q_ord[k+1]):
Cij = g.get_Cij(gamma, C[Q_ord[k]][Q_ord[k+1]])
gamma_beta.position(site=k, normalize=Normalize)
if (Dmax == D_highest):
gamma_beta.apply_two_site_gate(Cij, site1 = k,
site2 = (k+1), center_position=k)
else:
gamma_beta.apply_two_site_gate(Cij, site1 = k, site2 = (k+1),
max_singular_values=Dmax, center_position=k)
if (i%2 == 0): #Doing the even round of SWAPs from the SWAP network
for s in range(l1):
Q_ord[2*s],Q_ord[2*s+1] = Q_ord[2*s+1],Q_ord[2*s]
gamma_beta.position(site=(2*s), normalize=Normalize)
if (Dmax == D_highest):
gamma_beta.apply_two_site_gate(SWAP, site1 = (2*s),
site2 = (2*s+1), center_position=(2*s))
else:
gamma_beta.apply_two_site_gate(SWAP, site1 = (2*s), site2 = (2*s+1),
max_singular_values=Dmax, center_position=(2*s))
else: #Doing the odd round of SWAPs from the SWAP network
for s in range(l2):
Q_ord[2*s+1],Q_ord[2*s+2] = Q_ord[2*s+2],Q_ord[2*s+1]
gamma_beta.position(site=(2*s+1), normalize=Normalize)
if (Dmax == D_highest):
gamma_beta.apply_two_site_gate(SWAP, site1 = (2*s+1),
site2 = (2*s+2), center_position=(2*s+1))
else:
gamma_beta.apply_two_site_gate(SWAP,site1 = (2*s+1), site2 = (2*s+2),
max_singular_values=Dmax, center_position=(2*s+1))
gamma_beta = gamma_beta.tensors[::-1]
gamma_beta = [gamma_beta[x].transpose([2,1,0]) for x in range(n)]
gamma_beta = tn.FiniteMPS(gamma_beta, canonicalize = False)
return gamma_beta
#%%
def QAOA_beta_block(gamma_beta, beta):
'''
Function that takes in the parameter beta and applies
a single layer of the mixing block within a single QAOA layer.
Input::
gamma_beta: An existing QAOA state upon which one applies the Gamma layer
beta: The free parameter that needs to be optimized
Output::
gamma_beta: The QAOA state with an additional mixing layer of the QAOA added to it
'''
n = len(gamma_beta.tensors)
gamma_beta = tn.FiniteMPS(gamma_beta.tensors, canonicalize=False)
Rx = g.get_Rx(2*beta)
for i in range(n):
gamma_beta.apply_one_site_gate(Rx, i)
return gamma_beta
#%%
def get_plots(X, Y, Z, Title, File_name):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.ylabel('Gamma')
plt.xlabel('Beta')
plt.title(Title)
ax.plot_surface(X,Y,Z, cmap = 'jet', rstride=1, cstride=1, linewidth=0, antialiased=False)
plt.show()
plt.savefig(File_name)
plt.close(fig)
#%%
for r in Instance_list:
C = np.load(location+str(q)+tag+str(r)+'/C_Q'+str(q)+tag+str(r)+'.npy')
[J,h] = get_unsymmetric_C(C)
n = len(h)
for Dmax in DList:
Cost_strd_qaoa = np.zeros([N,N])
Cost_strd_broken = np.zeros([N,N])
Cost_broken = np.zeros([N,N])
Cost_diff_strd = np.zeros([N,N])
Cost_diff_broken = np.zeros([N,N])
for i in range(N):
print('\nQ'+str(q)+'R'+str(r)+', D = '+str(Dmax)+', i = '+str(i)+'\n')
plus_q = tn.FiniteMPS([np.array([[[1/np.sqrt(2)],
[1/np.sqrt(2)]]],
dtype = np.complex128) for x in range(q)])
plus_n = tn.FiniteMPS([np.array([[[1/np.sqrt(2)],
[1/np.sqrt(2)]]],
dtype = np.complex128) for x in range(n)])
Fail = True
attempt = 1
while (Fail):
try:
gamma_strd = QAOA_gamma_block_Z2symmetric(plus_q, Gamma[i], C, Dmax)
gamma_broken = QAOA_gamma_block_Z2broken(plus_n, Gamma[i], J, h, Dmax)
Fail = False
except:
print('\nSVD Error!! for D = '+str(Dmax)+
', and attempt = '+str(attempt)+'!\n')
attempt += 1
Gamma[i] = np.round(Gamma[i], decimals=(11-attempt))
if (attempt > 10):
print('Stopping Repetition at attempt = ',attempt,'\n')
raise
for j in range(N):
gamma_beta_strd = QAOA_beta_block(gamma_strd, Beta[j])
# gamma_beta.canonicalize(normalize=Normalize)
# print(exp.exp_MPS(gamma_beta,gamma_beta))
gamma_beta_broken = QAOA_beta_block(gamma_broken, Beta[j])
# gamma_beta_pert.canonicalize(normalize=Normalize)
# print(exp.exp_MPS(gamma_beta_pert,gamma_beta_pert))
Cost_strd_qaoa[i,j] = get_Cost_Z2symmetric(gamma_beta_strd, C)
Cost_strd_broken[i,j] = get_Cost_Z2symmetric(gamma_beta_broken, C)
Cost_broken[i,j] = get_Cost_Z2broken(gamma_beta_broken, J, h)
del gamma_beta_strd, gamma_beta_broken
del gamma_strd, gamma_broken
Cost_diff_strd = Cost_strd_broken - Cost_strd_qaoa
Cost_diff_broken = Cost_broken - Cost_strd_qaoa
#%%
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.ylabel('Gamma')
plt.xlabel('Beta')
plt.title('Q'+str(n)+'R'+str(r)+'\n Standard Cost for D = '+str(Dmax))
ax.plot_surface(G,B,Cost_strd_qaoa, cmap = 'jet', rstride=1, cstride=1, linewidth=0, antialiased=False)
plt.show()
# plt.savefig(location+str(n)+tag+str(r)+'/GridData/N100D'+str(Dmax)+'.png')
# plt.close(fig)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.ylabel('Gamma')
plt.xlabel('Beta')
plt.title('Q'+str(n)+'R'+str(r)+'\n Standard broken')
ax.plot_surface(G,B,Cost_strd_broken, cmap = 'jet', rstride=1, cstride=1, linewidth=0, antialiased=False)
plt.show()
# plt.savefig(location+str(n)+tag+str(r)+'/GridData/N100D'+str(Dmax)+'.png')
# plt.close(fig)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.ylabel('Gamma')
plt.xlabel('Beta')
plt.title('Q'+str(n)+'R'+str(r)+'\n Fully broken')
ax.plot_surface(G,B,-1*Cost_broken, cmap = 'jet', rstride=1, cstride=1, linewidth=0, antialiased=False)
plt.show()
# plt.savefig(location+str(n)+tag+str(r)+'/GridData/N100D'+str(Dmax)+'.png')
# plt.close(fig)