-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwfieldrun.py
375 lines (307 loc) · 12.2 KB
/
wfieldrun.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
import numpy as np
from numpy import linalg as LA
from numpy import count_nonzero
import math, cmath
from scipy.optimize import fmin, minimize, rosen, rosen_der
from itertools import product, combinations
from copy import copy
import matplotlib.pyplot as plt
from scipy import interpolate
from scipy.interpolate import make_lsq_spline, BSpline
from scipy.interpolate import make_interp_spline
from scipy.interpolate import Rbf, InterpolatedUnivariateSpline
from scipy.interpolate import interp1d
from scipy.sparse import csr_matrix
import numdifftools as nd
import scipy.optimize as optimize
import pickle
#FUNCTIONS
def dimensionH(Nn):
cont = 0
for i in range (Nn):
cont += math.factorial(L)/(math.factorial(i)*math.factorial(L-i))
return int(cont)
def expf(x,L):
return complex(np.cos(2*np.pi*x/L),np.sin(2*np.pi*x/L))
def common_member(a, b):
a_set = set(a)
b_set = set(b)
if (a_set & b_set):
return True
else:
return False
def Ham(H1,H2,U):
return (H1+np.multiply(H2,U))[ni:ni+nf,ni:ni+nf]
def UnD(a1,a2,a3,a4):
return np.matmul(np.matmul(np.matmul(np.transpose(Op[a1]),np.transpose(Op[a2])),Op[a3]),Op[a4])-np.matmul(np.matmul(np.matmul(np.transpose(Op[a4]),np.transpose(Op[a3])),Op[a2]),Op[a1])
def UnS(a1,a2):
return np.matmul(np.transpose(Op[a1]),Op[a2])-np.matmul(np.transpose(Op[a2]),Op[a1])
def Unitary(th,OpAux):
return np.identity(nf)+np.multiply(OpAux,np.sin(th))-np.multiply((np.cos(th)-1),np.matmul(OpAux,OpAux))
def vecf(w,res1):
vec = []
for i in range(len(res1)):
cont = 1
for x in range(len(w)):
cont *= w[x]**res1[i][x]*(1-w[x])**(1-res1[i][x])
vec.append(cont)
return vec
class function():
def __init__(self, weig,res,Hamil):
self.res = res
self.Hamil = Hamil
self.weig = weig
def evalua(self,seed):
matrizSD = Unit(seed,self.res,self.Hamil)
elem = 0
for ji in range(len(self.weig)):
vec=np.zeros(len(self.weig))
vec[ji]=1
elem += self.weig[ji]*np.matmul(np.matmul(vec,matrizSD),vec)
return elem
def grad(self,seed):
return nd.Gradient(self.evalua)(seed)
#NUMBER OF SITES, WEIGHTS and TROTTER STEPS:
L = int(input("L number (integer) of sites: "))
trotter = int(input("Trotter (integer) steps: "))
answer = input("Do you want to introduce the w-values (y/n): ")
w = list(np.arange(0.5/L,0.5+0.01,0.5/L))
Num = int(input("Number of particles: "))
ni = dimensionH(Num)
nf = int(math.factorial(L)/(math.factorial(Num)*math.factorial(L-Num)))
print(Num,ni,nf)
if answer == "y":
for i in range(L):
w[i] = float(input("Please enter a weight between 0 and 0.5: "))
elif answer == "n":
w = list(np.arange(0.5/L,0.5+0.01,0.5/L))
else:
print("Please next time enter (y/n). I take my weights.")
round_to_w = [round(num, 3) for num in w]
print("************************")
print("************************")
print("The weights are: ", round_to_w)
print("Now the calculations start (relax)")
print("****************************")
print("* * * *** * *** * *** ")
print(" * * * * ** * ** * * * ")
print(" * * * * *** *** *** ")
print("***************************")
#GENERATION OF THE HILBERT (FOCK) SPACE
## This generates the Hilbert space {|000>,|001>,...} but in a non-organized way
res = [ele for ele in product([0,1], repeat = L)]
res1 = [ele for ele in product([0,0], repeat = L)]
res = np.array(res)
res1 = np.array(res1)
#ORGANIZING THIS IN N-PARTICLE SECTORS
##res1 contains ALL the states in the Hilbert space
sumsec= np.sum(res, axis=1)
vec = []
for j in range(L+1):
vec.append([i for i,x in enumerate(sumsec) if x == j])
order= sum(vec,[])
for j in range(2**L):
res1[j] = res[order[j]]
#GENERATION OF THE ANHILITATION OPERATORS
##Op[0],Op[1]... are the anhilitation operators for sites 0,1,...
Op = np.zeros((L+1,2**L,2**L))
OpS = []
for j in range(L):
res2 = [ele for ele in product([0,0], repeat = L)]
res2 = copy(res1)
res2[:,j] = np.zeros(2**L)
aux2 = copy(res1[:,j])
if j == 0:
sta= copy(res1[:,0])
else:
sta = [math.pow(-1,value) for value in sum(res1[:,x] for x in range(j))]
for j1 in range(2**L):
for j2 in range(2**L):
if np.array_equal(res1[j1],res2[j2]):
Op[j,j1,j2] = sta[j2]*aux2[j2]
Op[L] = Op[0]
##This is optional to verify the fermionic commutation rules:
#print(np.sum(Op[0],axis=1))
#print(np.diag(np.matmul(np.transpose(Op[0]), Op[0])+np.matmul(Op[0],np.transpose(Op[0]))))
#CONSTRUCTION OF THE HAMILTONIANS
Ham1 =-sum(np.multiply(expf((k1-k2)*jj,L)*expf(-k2,L)/L,np.matmul(np.transpose(Op[k1]), Op[k2]))+np.multiply(expf((k1-k2)*jj,L)*expf(k1,L)/L,np.matmul(np.transpose(Op[k1]), Op[k2])) for k1 in range(L) for k2 in range(L) for jj in range(L))
Ham2 =sum(np.multiply(expf((k1-k2+k3-k4)*jj,L)*expf(k3-k4,L)/L**2,np.matmul(np.matmul(np.matmul(np.transpose(Op[k1]), Op[k2]),np.transpose(Op[k3])),Op[k4])) for k1 in range(L) for k2 in range(L) for k3 in range(L) for k4 in range(L) for jj in range(L))
#Ham1S =-sum(np.multiply(expf((k1-k2)*jj,L)*expf(-k2,L)/L,np.multiply(csr_matrix.transpose(OpS[k1]), OpS[k2]))+np.multiply(expf((k1-k2)*jj,L)*expf(k1,L)/L,np.multiply(csr_matrix.transpose(OpS[k1]), OpS[k2])) for k1 in range(L) for k2 in range(L) for jj in range(L))
#Ham2S =sum(np.multiply(expf((k1-k2+k3-k4)*jj,L)*expf(k3-k4,L)/L**2,np.multiply(np.multiply(np.multiply(csr_matrix.transpose(OpS[k1]), OpS[k2]),csr_matrix.transpose(OpS[k3])),OpS[k4])) for k1 in range(L) for k2 in range(L) for k3 in range(L) for k4 in range(L) for jj in range(L))
eigen = []
entan = []
for u in range(11):
v1, v2 = LA.eig(Ham(Ham1,Ham2,u))
eigen.append(v1)
eigen[u] = eigen[u].real
prov = []
for s in range(len(v2[:,1])):
prov.append(sum([abs(num)**4 for num in v2[:,s]]))
entan.append(prov)
ordering= sorted(range(len(eigen[u])), key=lambda k: eigen[u].real[k])
eigen[u].real.sort()
aa = copy(entan[u])
for s in range(len(entan[u])):
aa[s] = entan[u][ordering[s]]
entan[u] = aa
eigen = np.array(eigen)
entan = np.array(entan)
#plt.rc('axes', labelsize=15)
#plt.rc('font', size=15)
#plt.plot(eigen[3:11,0], entan[3:11,0],'bo',label='gs')
#plt.plot(eigen[3:11,1], entan[3:11,1],'bo',mfc='none',label='1s')
#plt.plot(eigen[3:11,2], entan[3:11,2],'ro',mfc='none',label='2s')
#plt.plot(eigen[3:11,3], entan[3:11,3],'ko', label='3s')
#plt.plot(eigen[3:11,4], entan[3:11,4],'ko',mfc='none',label='4s')
#plt.plot(eigen[3:11,5], entan[3:11,5],'go', mfc='none',label='5s')
#plt.plot(eigen[3:11,6], entan[3:11,6],'go',label='6s')
#plt.plot(eigen[3:11,7], entan[3:11,7],'yo', mfc='none',label='7s')
#plt.plot(eigen[3:11,8], entan[3:11,8],'b+',label='8s')
#plt.plot(eigen[3:11,9], entan[3:11,9],'b*', mfc='none',label='9s')
#plt.legend(prop={"size":15},loc='center right')
#plt.xlabel("$U$")
#plt.title("L = 5")
#plt.show()
FI1 =[0,1,2,3,4,5, 6, 7, 8, 9,10]
xnew = np.linspace(0, 10, 300)
FI1 = np.array(FI1)
plt.rc('axes', labelsize=15)
plt.rc('font', size=15)
for i in range(1,nf-1):
spl = make_interp_spline(FI1, eigen[:,i], k=3)
powersmooth = spl(xnew)
plt.plot(xnew,powersmooth,'k-', mfc='none')
#plt.plot(FI1, eigen[:,i],'r-', mfc='none')
spl = make_interp_spline(FI1, eigen[:,nf-1], k=3)
powersmooth = spl(xnew)
plt.plot(xnew,powersmooth,'k-', mfc='none')
spl = make_interp_spline(FI1, eigen[:,0], k=3)
powersmooth = spl(xnew)
plt.plot(xnew,powersmooth,'r-', mfc='none',lw=4)
plt.show()
#CONSTRUCTION OF UNITARIES
#Generation of all Doubles Excitations
test_list = np.arange(0, L, 1).tolist()
res2 = list(combinations(test_list,2))
Doubles = []
for j1 in range(len(res2)):
for k1 in range(j1+1,len(res2)):
if(common_member(res2[j1],res2[k1])==False):
#print(res2[j1],res2[k1],common_member(res2[j1],res2[k1]),j1,k1)
Doubles.append((res2[j1],res2[k1]))
AllD = np.zeros((len(Doubles),nf,nf))
AllS = np.zeros((len(res),nf,nf))
#AllDsparse = []
#AllSsparse = []
OpS = []
for i in range(L+1):
OpS.append(csr_matrix(Op[i]))
for j1 in range(len(Doubles)):
AllD[j1] = UnD(Doubles[j1][0][0],Doubles[j1][0][1],Doubles[j1][1][0],Doubles[j1][1][1])[ni:ni+nf,ni:ni+nf]
#AllDsparse.append(csr_matrix(AllD[j1]))
for j1 in range(len(res2)):
AllS[j1] = UnS(res2[j1][0],res2[j1][1])[ni:ni+nf,ni:ni+nf]
#AllSsparse.append(csr_matrix(AllS[j1]))
FI1 =[0,1,2,3,4,5, 6, 7, 8, 9,10]
FI1 = np.array(FI1)
#QUANTUM ALGORITHM: here starts the quantum calculation
#Generation of all Doubles Excitations
test_list = np.arange(0, L, 1).tolist()
res2 = list(combinations(test_list,2))
Doubles = []
for j1 in range(len(res2)):
for k1 in range(j1+1,len(res2)):
if(common_member(res2[j1],res2[k1])==False):
#print(res2[j1],res2[k1],common_member(res2[j1],res2[k1]),j1,k1)
Doubles.append((res2[j1],res2[k1]))
print(len(Doubles),len(res2))
def Unit(params,res2,Hamil):
x = params
Full = np.identity(nf)
Full1 = np.identity(nf)
FullS = np.identity(nf)
Full1S = np.identity(nf)
for j1 in range(len(Doubles)):
Full = np.matmul(Unitary(x[j1],AllD[j1]),Full)
Full1 = np.matmul(Full1, Unitary(-x[j1],AllD[j1]))
for j1 in range(len(Doubles),2*len(Doubles)):
j11 = j1-len(Doubles)
Full = np.matmul(Unitary(x[j1],AllD[j11]),Full)
Full1 = np.matmul(Full1, Unitary(-x[j1],AllD[j11]))
for j1 in range(2*len(Doubles),2*len(Doubles)+len(res2)):
j11 = j1-2*len(Doubles)
FullS = np.matmul(Unitary(x[j1],AllS[j11]),FullS)
Full1S = np.matmul(Full1S, Unitary(-x[j1],AllS[j11]))
for j1 in range(2*len(Doubles)+len(res2),2*len(Doubles)+2*len(res2)):
j11 = j1-2*len(Doubles)-len(res2)
FullS = np.matmul(Unitary(x[j1],AllS[j11]),FullS)
Full1S = np.matmul(Full1S, Unitary(-x[j1],AllS[j11]))
Full = np.matmul(LA.matrix_power(FullS,trotter),np.matmul(LA.matrix_power(Full, trotter),FullS))
Full1 = np.matmul(np.matmul(Full1S,LA.matrix_power(Full1, trotter)),LA.matrix_power(Full1S,trotter))
#Full = np.matmul(LA.matrix_power(np.matmul(FullS,Full),trotter),FullS)
#Full1 = np.matmul(Full1S,LA.matrix_power(np.matmul(Full1,Full1S),trotter))
return np.matmul(np.matmul(Full1,Hamil),Full)
def gradient_descent(gradient, start, learn_rate, n_iter, tolerance):
vector = start
for _ in range(n_iter):
diff = -learn_rate * gradient(vector).real
if np.all(np.abs(diff) <= tolerance):
break
vector += diff
return vector
weights = vecf(w,res1)
eigennum = np.zeros((11,nf))
eigenor= np.zeros((11,nf))
eigennumor = np.zeros((11,nf))
gap = np.zeros(11)
gapnum = np.zeros(11)
gap2 = np.zeros(11)
gapnum2 = np.zeros(11)
seed=list(np.full(2*len(Doubles)+2*len(res2),0))
Hamil=Ham(Ham1,Ham2,0)
for u in range(11):
print("I am computing the energies for the coupling u: ", u)
Hamil=Ham(Ham1,Ham2,u)
fun = function(weights[ni:ni+nf],res2,Hamil)
#seed = gradient_descent(fun.grad,seed,0.2,20,1e-02)
seed = optimize.fmin(fun.evalua, seed,maxfun=200000,maxiter=200000,ftol=1e-4,xtol=1e-4)
vec=np.zeros(nf)
vecaux=np.zeros(nf)
for i in range(nf):
vec=np.zeros(nf)
vec[i]=1
eigennum[u,i] = np.matmul(np.matmul(vec,Unit(seed,res2,Hamil)),vec)
eigenor[u] = list(eigen.real[u])
eigenor[u].sort()
eigennumor[u] = list(eigennum.real[u])
eigennumor[u].sort()
gap[u] = eigenor[u,1]-eigenor[u,0]
gapnum[u] = eigennumor[u][1]-eigennumor[u][0]
gap2[u] = eigenor[u,2]-eigenor[u,0]
gapnum2[u] = eigennumor[u][2]-eigennumor[u][0]
pickle.dump(eigen, open( "list3.p", "wb" ) )
pickle.dump(eigennum, open( "list4.p", "wb" ) )
plt.rc('axes', labelsize=15)
plt.rc('font', size=15)
for i in range(nf-1):
plt.plot(FI1, eigen[:,i],'bo', mfc='none')
plt.plot(FI1, eigennum[:,i],'r*')
plt.plot(FI1, eigen[:,nf-1],'bo', mfc='none',label='exact')
plt.plot(FI1, eigennum[:,nf-1],'r*', label='UCC')
plt.legend(prop={"size":15},loc='upper left')
plt.xlabel("$U/t$")
plt.show()
plt.rc('axes', labelsize=15)
plt.rc('font', size=15)
plt.plot(FI1, gap,'bo', mfc='none',label='exact')
plt.plot(FI1, gapnum,'r*',label='UCC')
plt.legend(prop={"size":15},loc='upper left')
plt.xlabel("$U/t$")
plt.show()
plt.rc('axes', labelsize=15)
plt.rc('font', size=15)
plt.plot(FI1, gap2,'bo', mfc='none',label='exact')
plt.plot(FI1, gapnum2,'r*',label='UCC')
plt.legend(prop={"size":15},loc='upper left')
plt.xlabel("$U/t$")
plt.show()