forked from qurist/LongRangeIsingVQE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqaoaLibrary.py
297 lines (254 loc) · 9.86 KB
/
qaoaLibrary.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
#----------------------------------------------------------------------------#
# Title: A python helper library for QAOA-type optimization
# Author: Aniruddha Bapat
# Date: 05-28-2018
#
# Description: Here, I will maintain a library of frequently used functions
# in QAOA-type optimizations.
#----------------------------------------------------------------------------#
from ctypes import *
import numpy as np
import scipy.sparse as sp
from scipy.optimize import basinhopping, minimize
import itertools as it
from numpy import pi, sqrt
# Load c functions in qaoa
# I think this is OS-dependent. Create the appropriate shared c object
# for your OS (.so, .dll etc.) and load it into qc
qc = cdll.LoadLibrary('./lib2local-qaoa.so')
# States and Hamiltonian structs, ctyped into python
class state(Structure):
_fields_ = [('n', c_int),
('N',c_int),
('realcur',POINTER(c_double)),
('imagcur',POINTER(c_double)),
('realbuf',POINTER(c_double)),
('imagbuf',POINTER(c_double))]
class ham(Structure):
_fields_ = [('n',c_int),
('N',c_int),
('zzc',POINTER(c_double)),
('xc',POINTER(c_double)),
('zc',POINTER(c_double))]
# Bound class for basinhopping
class MaxTbound(object):
"""random displacement with bounds"""
def __init__(self, T, stepsize=0.5):
self.maxT= T
def __call__(self, **kwargs):
"""take a random step but ensure the new position is within the bounds"""
x = kwargs["x_new"]
return (sum(x)<self.maxT)
# Set input and return types
qc.expectH.restype = c_double
qc.overlap.resype = c_double
qc.energy.restype = c_double
qc.qaoa1energy.restype = c_double
# Initialize state and hamiltonian
def initialize(n):
psi = state()
H = ham()
qc.allocate(byref(psi),n)
qc.allocateH(byref(H),n)
return (psi, H)
# Generate Ham object from coefficients zzc, xc and zc (given as np arrays)
def hamGen(H, zzc, xc, zc):
n = len(xc)
H.zzc = (c_double * n**2)(*zzc.flatten().tolist())
H.xc = (c_double * n)(*xc.tolist())
H.zc = (c_double * n)(*zc.tolist())
# Function to compute Ham's k smallest eigenvalues and eigenvectors
def ground(H, Neigs):
n = H.n
energies= map(lambda i:qc.energy(i,byref(H)), range(1<<n))
Ham = np.diag(energies)
for i in range(1<<n):
for j in range(n):
Ham[i,i^(1<<j)] = H.xc[j]
return sp.linalg.eigsh(Ham, k=Neigs, which='SA')
# Inner product magnitude of two states given as complex np arrays
def overlap(psi1, psi2):
return np.abs(np.vdot(psi1,psi2))
# Convert C state object into numpy array of computational basis amplitudes
def c2pyState(psi):
n = psi.n
return np.array(psi.realcur[:1<<n]) + 1.0j*np.array(psi.imagcur[:1<<n])
# Overlap of psi with the Neigs smallest ground states of H
def gsOverlap(psi, H, Neigs):
val, vec = ground(H, Neigs)
final = c2pyState(psi)
retvalue = 0
for k in range(Neigs):
retvalue += overlap(final, vec[:,k])**2
return np.sqrt(retvalue)
#betagamma = all betas first, then all gammas
def expectQAOAp(psi, H, betagamma):
ppsi = pointer(psi)
pH = pointer(H)
qc.uniform(ppsi)
p = len(betagamma)/2
qc.evolveQAOA(ppsi, pH, (c_double * p)(*betagamma[:p]), (c_double * p)(*betagamma[p:]), p)
return qc.expectH(psi, pH)
# return squared overlap with the ground state vectors
def overlapQAOAp(psi, H, betagamma, vec):
ppsi = pointer(psi)
pH = pointer(H)
csp.uniform(ppsi)
p = len(betagamma)/2
csp.evolveQAOA(ppsi, pH, (c_double * p)(*betagamma[:p]), (c_double * p)(*betagamma[p:]), p)
sqoverlap=0
psivec = c2pyState(psi)
for i in len(vec.T):
sqoverlap += overlap(psivec, vec[:,i])**2
return sqoverlap
# Perform an inductive local optimization of QAOA angles. The code returns
# optimal angles and energy. Psi is now the result of the optimal evolution.
def optQAOA(psi, H, pmax, typeOfOpt='BFGS'):
ppsi = pointer(psi)
pH = pointer(H)
fOpt = lambda bg: expectQAOAp(psi, H, bg.tolist())
bg0 = 0.5*np.ones(2)
bgCur= bg0
for p in range(1,pmax+1):
bgNew = np.concatenate((bgCur[:p-1], [bgCur[p-1]], bgCur[p-1:], [bgCur[-1]]))
opt = minimize(fOpt, bg0 if p==1 else bgNew, method=typeOfOpt)
bgCur = opt.x
E = expectQAOAp(psi, H, bgCur.tolist())
#if E!=opt.fun: return "Error: Energy expectation does not match optimized value"
return (bgCur, E)
def optQAOAglobal(psi, H, p, Nit=10, Temp=1.0, Tmax=20):
ppsi = pointer(psi)
pH = pointer(H)
fOpt = lambda bg: expectQAOAp(psi, H, bg.tolist())
bg0 = 2.0*np.ones(2*p)
bounds = [(0,pi/2)]*p + [(0,np.inf)]*p
minKwargs = dict(method="L-BFGS-B", bounds=bounds)
stepCond = MaxTbound(Tmax)
opt = basinhopping(fOpt, bg0, niter=Nit, T=Temp, minimizer_kwargs=minKwargs, accept_test=stepCond)
bgOpt= opt.x
E = expectQAOAp(psi, H, bgOpt.tolist())
return (opt.x, E)
# Full search of angles, with resolution of M for every pi/2 angles
# beta range: [0,pi/2]
# Gamma range: [0, 3*pi]
def optQAOAgreedy(psi, H, pmax, typeOfOpt='BFGS'):
ppsi = pointer(psi)
pH = pointer(H)
bg0 = [0.5]*2
bgOpt = []
for p in range(pmax):
fOpt = lambda bg: expectQAOAp(psi, H, np.concatenate((bgOpt,bg)).tolist())
opt = minimize(fOpt, bg0, method=typeOfOpt)
bgOpt += opt.x.tolist()
E = expectQAOAp(psi, H, bgOpt)
return (np.array(bgOpt), E)
def optQAOAfull(psi, H, p, M=10, trunc=-np.inf):
S = 2 # scale factor gamma:beta
bMax = pi/2
gMax = S*bMax
indOpt= np.inf
Opt = np.inf
configs = it.product(*([np.linspace(0,bMax, M,endpoint=False)]*p + [np.linspace(0,gMax, S*M,endpoint=False)]*p))
for ind, i in enumerate(configs):
Ecur = expectQAOAp(psi, H, i)
if trunc!=np.inf and Ecur < trunc:
Opt = Ecur
indOpt = ind
break
indOpt= indOpt if Opt < Ecur else ind
Opt = min(Opt, Ecur)
configs = it.product(*([np.linspace(0,bMax, M,endpoint=False)]*p + [np.linspace(0,gMax, 6*M,endpoint=False)]*p))
return (list(configs)[indOpt], Opt)
# Prints useful information
def printOut(psi, val, vec, bgOpt, Eopt):
final = c2pyState(psi)
final2gs = 0
sumval = 0
Neigs = len(vec.T)
for k in range(Neigs):
sumval += overlap(final, vec[:,k])**2
final2gs=np.sqrt(sumval)
final2initial = 1./sqrt(psi.N)*overlap(final, np.ones(psi.N))
print "Optimal schedule:",
for i in range(len(bgOpt)):
print "%5.4f "%bgOpt[i],
print
#print "%5.4f %5.4f"%(Eopt, val[0])
print "Energy ratio: %7.6f, Overlap: %5.4f"%(np.abs(Eopt/(val[0])), final2gs)
print "Final to initial state overlap: %5.4f"%final2initial
# Symmetrize a state w.r.t to the global Z2 symmetry present in 2-local Hamiltonians
# Only physical when you are in the symmetric sector
def z2symmetrize(psi):
sym = psi + psi[::-1]
return sym/np.sqrt(overlap(sym,sym))
# Entanglement entropy
# Input: a state psi (as an np.arrar) in the computational basis, and the cut site k
def entent(psi, k):
N = len(psi)
n = len(format(N,'b'))-1 # effectively n = log2(N) for powers of two
m = n-k
prho = np.zeros((1<<k,1<<k),dtype=np.complex_) # partial density matrix
x = np.arange(1<<k)
for i in range(1<<m):
ppsi = psi[x*(1<<m)+i] # Partial state vector
prho += np.outer(ppsi,np.conj(ppsi))
val, vec = eigh(prho)
return sum(map(lambda x: -x*np.log(abs(x)+0.000000000001), val))
# The analytical formula for energy under QAOA1
# betagamma = beta first, then gamma
def expectQAOA1(H, betagamma):
return qc.qaoa1energy(byref(H), c_double(betagamma[0]), c_double(betagamma[1]))
##############################################################################
# ZZ + X + Z Hamiltonian coefficient generators
# Output format: a list of lists of the form ((Z, ZZ, ..), (X, XX, ..))
# So, for a 2 local Hamiltonian, expect ((Z, ZZ), (X,))
# The standard 2-local long-range model with power law interactions
def lr(n, alpha, J, B):
Ki = np.zeros(n)
Jij = np.array([[(J/(abs(i-j))**alpha if i!=j else 0) for i in range(n)] for j in range(n)])
Bi = B*np.ones(n)
return ((Ki, Jij), (Bi,))
# Read parameters and prepare coefficients
def readParams():
return
# Construct an unweighted MaxSat instance from a cnf file
# NOTE1: cnf Clauses must be 1-indexed.
# NOTE2: This method is pretty memory-inefficient for
# large clause sizes. Ideal for up to ~4Sat
def readCnf(path2file):
f=open(path2file,'r')
clauses = []
maxLength = 0
while True:
line = f.readline()
if not line: break
spline = line.split()
if spline[0]=='c':
continue
elif spline[0]=='p':
n = int(spline[2])
M = int(spline[3])
else:
for c in spline[:-1]:
if abs(int(c))>n or int(c)==0:
print("Error: variable indices must lie between 0 and %d"%n)
break;
if spline[-1]!='0':
print("Error: clause descriptions must have a terminal 0")
break;
maxLength = max(maxLength, len(spline)-1)
clauses.append(map(int,spline[:-1]))
if len(clauses)!=M: print("Error: Need %d clauses"%M)
f.close()
const = 0
Bi = np.zeros(n)
Jall = [np.zeros([n]*i) for i in range(1,maxLength+1)]
for i in range(M):
clause=map(lambda x: abs(x)-1,clauses[i])
signs=map(np.sign,clauses[i])
K = len(clause)
for k in range(1, K+1):
for inds in it.combinations(range(K), k):
Jall[k-1][inds] += (1./(1<<K))*np.prod([signs[x] for x in inds])
const += 1/(1<<K)
return (Jall, (Bi,))