-
Notifications
You must be signed in to change notification settings - Fork 0
/
VQE.py
58 lines (51 loc) · 1.73 KB
/
VQE.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
import tqix, constant, base, lagrange_psr
import numpy as np
n = 5
lambdax = 0.05
def H_LMG(h, lambdax, n, Jx, Jy, Jz):
return -2*h*Jz - 2*lambdax/n*(Jx**2 - Jy**2)
def cost_function_noise(thetas, h):
qc = tqix.circuit(n)
for i in range(0, n):
noises = np.random.uniform(0, 1, 3)
qc.RX(thetas[i], noises[0])
qc.RZ(thetas[i + n], noises[1])
qc.RX(thetas[i + 2 * n], noises[2])
Jx = qc.Jx()
Jy = qc.Jy()
Jz = qc.Jz()
h_LMG = H_LMG(h, lambdax, n, Jx, Jy, Jz)
return np.real(np.trace((h_LMG @ qc.state).toarray()))
def cost_function(thetas, h):
qc = tqix.circuit(n)
for i in range(0, n):
qc.RX(thetas[i])
qc.RZ(thetas[i + n])
qc.RX(thetas[i + 2 * n])
Jx = qc.Jx()
Jy = qc.Jy()
Jz = qc.Jz()
h_LMG = H_LMG(h, lambdax, n, Jx, Jy, Jz)
return np.real(np.trace((h_LMG @ qc.state).toarray()))
def optimal(h):
costs = []
thetass = []
thetas = np.random.uniform(0, 2*np.pi, n*3)
for i in range(0, 30):
print("Iteration: ", i)
thetass.append(thetas)
thetas = thetas - constant.learning_rate*base.two_prx_hLMG(cost_function, thetas, h)
costs.append(cost_function(thetas, h))
np.savetxt("cost_" + str(h) + ".txt", costs)
np.savetxt("thetas_" + str(h) + ".txt", thetass)
def optimal_noise(h):
costs = []
thetass = []
thetas = np.random.uniform(0, 2*np.pi, n*3)
for i in range(0, 30):
print("Iteration: ", i)
thetass.append(thetas)
thetas = thetas - constant.learning_rate*base.two_prx_hLMG(cost_function_noise, thetas, h)
costs.append(cost_function(thetas, h))
np.savetxt("cost_" + str(h) + ".txt", costs)
np.savetxt("thetas_" + str(h) + ".txt", thetass)