-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathtest_fgs.py
267 lines (226 loc) · 8.59 KB
/
test_fgs.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
import numpy as np
import pytest
from pytest_lazyfixture import lazy_fixture as lf
import tensorflow as tf
try:
import openfermion as _
except ModuleNotFoundError:
pytestmark = pytest.mark.skip("skip fgs test due to missing openfermion module")
import tensorcircuit as tc
F = tc.fgs.FGSSimulator
FT = tc.fgs.FGSTestSimulator
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
def test_cmatrix(backend, highp):
import openfermion
def asymmetric_hopping(J, gamma, i, j, L):
m0 = tc.fgs.onehot_matrix(i, j, 2 * L) - tc.fgs.onehot_matrix(
j + L, i + L, 2 * L
)
m = (J + gamma) * m0 + (J - gamma) * tc.backend.adjoint(m0)
return m / 2
def asymmetric_hopping_jw(J, gamma, i, j, L):
op = (J + gamma) * openfermion.FermionOperator(f"{str(i)}^ {str(j)}") + np.conj(
J - gamma
) * openfermion.FermionOperator(f"{str(j)}^ {str(i)}")
sop = openfermion.transforms.jordan_wigner(op)
m = openfermion.get_sparse_operator(sop, n_qubits=L).todense()
return m
c = tc.fgs.FGSSimulator(4, [0])
c1 = tc.fgs.FGSTestSimulator(4, [0])
c.evol_hp(0, 1, 1.2)
c1.evol_hp(0, 1, 1.2)
c.evol_icp(2, 0.5)
c1.evol_icp(2, 0.5)
c.evol_cp(0, -0.8)
c1.evol_cp(0, -0.8)
c.evol_sp(1, 0, 0.5)
c1.evol_sp(1, 0, 0.5)
c.evol_ghamiltonian(asymmetric_hopping(1, 0.5, 0, 2, 4))
c1.evol_ghamiltonian(np.array(asymmetric_hopping_jw(1, 0.5, 0, 2, 4)))
c.evol_sp(1, 2, 1.5)
c1.evol_sp(1, 2, 1.5)
c.evol_ihamiltonian(c.chemical_potential(1.0, 1, 4))
c1.evol_ihamiltonian(c1.chemical_potential_jw(1.0, 1, 4))
np.testing.assert_allclose(
c1.get_cmatrix_majorana(), c.get_cmatrix_majorana(), atol=1e-5
)
np.testing.assert_allclose(c1.get_cmatrix(), c.get_cmatrix(), atol=1e-5)
np.testing.assert_allclose(c1.entropy([0, 2]), c.entropy([0, 2]), atol=1e-5)
np.testing.assert_allclose(
c1.renyi_entropy(2, [1]), c.renyi_entropy(2, [1]), atol=1e-5
)
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
def test_otoc(backend, highp):
c = tc.FGSSimulator(4, [0, 1])
c1 = tc.fgs.FGSTestSimulator(4, [0, 1])
h = c.hopping(0.6, 1, 2, 4)
h += c.hopping(-0.8, 3, 2, 4)
h += c.chemical_potential(0.3, 2, 4)
h1 = c1.hopping_jw(0.6, 1, 2, 4)
h1 += c1.hopping_jw(-0.8, 3, 2, 4)
h1 += c1.chemical_potential_jw(0.3, 2, 4)
c.evol_hamiltonian(h)
m = c.get_cmatrix(now_i=True, now_j=False)
m0 = c1.get_ot_cmatrix(h1)
np.testing.assert_allclose(m, m0, atol=1e-5)
m = c.get_cmatrix(now_i=False, now_j=True)
m0 = c1.get_ot_cmatrix(h1, now_i=False)
np.testing.assert_allclose(m, m0, atol=1e-5)
@pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
def test_fgs_ad(backend, highp):
import optax
N = 18
def f(chi):
c = tc.fgs.FGSSimulator(N, [0])
for i in range(N - 1):
c.evol_hp(i, i + 1, chi[i])
cm = c.get_cmatrix()
return -tc.backend.real(1 - cm[N, N])
chi_vg = tc.backend.jit(tc.backend.value_and_grad(f))
if tc.backend.name == "tensorflow":
opt = tc.backend.optimizer(tf.keras.optimizers.Adam(1e-2))
else:
opt = tc.backend.optimizer(optax.adam(1e-2))
param = tc.backend.ones([N - 1], dtype="float64")
for _ in range(300):
vs, gs = chi_vg(param)
param = opt.update(gs, param)
np.testing.assert_allclose(vs, -0.9986, atol=1e-2)
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
def test_hamiltonian_generation(backend):
hc = F.chemical_potential(0.8, 0, 3)
h1 = FT.get_hmatrix(hc, 3) + 0.4 * tc.backend.eye(2**3)
# constant shift
h2 = FT.chemical_potential_jw(0.8, 0, 3)
np.testing.assert_allclose(h1, h2, atol=1e-5)
hc = F.hopping(0.8j, 0, 1, 3)
h1 = FT.get_hmatrix(hc, 3)
h2 = FT.hopping_jw(0.8j, 0, 1, 3)
np.testing.assert_allclose(h1, h2, atol=1e-5)
hc = F.sc_pairing(0.8, 1, 0, 3)
h1 = FT.get_hmatrix(hc, 3)
h2 = FT.sc_pairing_jw(0.8, 1, 0, 3)
np.testing.assert_allclose(h1, h2, atol=1e-5)
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
def test_ground_state(backend, highp):
N = 3
hc = (
F.hopping(1.0, 0, 1, N)
+ F.hopping(1.0, 1, 2, N)
+ F.chemical_potential(0.4, 0, N)
- F.chemical_potential(0.4, 1, N)
+ F.sc_pairing(0.8, 0, 1, N)
)
c = F(N, hc=hc)
c1 = FT(N, hc=hc)
np.testing.assert_allclose(c.get_cmatrix(), c1.get_cmatrix(), atol=1e-6)
np.testing.assert_allclose(c1.entropy([0]), c.entropy([0]), atol=1e-5)
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
def test_post_select(backend, highp):
for ind in [0, 1, 2]:
for keep in [0, 1]:
c = F(3, filled=[0, 2])
c.evol_hp(0, 1, 0.2)
c.evol_cp(0, 0.5)
c.evol_hp(1, 2, 0.3j)
c.evol_cp(1, -0.8)
c.evol_sp(0, 2, 0.3)
c.post_select(ind, keep)
c1 = FT(3, filled=[0, 2])
c1.evol_hp(0, 1, 0.2)
c1.evol_cp(0, 0.5)
c1.evol_hp(1, 2, 0.3j)
c1.evol_cp(1, -0.8)
c1.evol_sp(0, 2, 0.3)
c1.post_select(ind, keep)
np.testing.assert_allclose(c.get_cmatrix(), c1.get_cmatrix(), atol=1e-5)
@pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
def test_jittable_measure(backend):
@tc.backend.jit
def get_cmatrix(status):
r = []
c = F(3, filled=[0])
c.evol_hp(0, 1, 0.2)
c.evol_cp(0, 0.5)
c.evol_hp(1, 2, 0.3j)
r.append(c.cond_measure(1, status[0]))
c.evol_cp(1, -0.8)
r.append(c.cond_measure(2, status[1]))
r.append(c.cond_measure(1, status[3]))
c.evol_sp(0, 2, 0.3)
c.evol_hp(2, 1, 0.2)
c.evol_hp(0, 1, 0.8)
return c.get_cmatrix(), tc.backend.stack(r)
def get_cmatrix_baseline(status):
r = []
c = FT(3, filled=[0])
c.evol_hp(0, 1, 0.2)
c.evol_cp(0, 0.5)
c.evol_hp(1, 2, 0.3j)
r.append(c.cond_measure(1, status[0]))
c.evol_cp(1, -0.8)
r.append(c.cond_measure(2, status[1]))
r.append(c.cond_measure(1, status[3]))
c.evol_sp(0, 2, 0.3)
c.evol_hp(2, 1, 0.2)
c.evol_hp(0, 1, 0.8)
return c.get_cmatrix(), np.array(r)
status = np.array([0.2, 0.83, 0.61, 0.07])
m, his = get_cmatrix(tc.backend.convert_to_tensor(status))
m1, his1 = get_cmatrix_baseline(status)
np.testing.assert_allclose(m, m1, atol=1e-5)
np.testing.assert_allclose(his, his1, atol=1e-5)
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
def test_exp_2body(backend):
c = F(3, filled=[1])
np.testing.assert_allclose(c.expectation_2body(4, 1), 1.0, atol=1e-5)
np.testing.assert_allclose(c.expectation_2body(5, 2), 0.0, atol=1e-5)
np.testing.assert_allclose(c.expectation_2body(1, 4), 0.0, atol=1e-5)
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
def test_exp_4body(backend):
c = F(4, filled=[0, 2])
c.evol_hp(0, 1, 0.3)
c.evol_hp(2, 3, 0.3)
c.evol_sp(0, 3, 0.5)
c.evol_sp(0, 2, 0.9)
c.evol_cp(0, -0.4)
c1 = FT(4, filled=[0, 2])
c1.evol_hp(0, 1, 0.3)
c1.evol_hp(2, 3, 0.3)
c1.evol_sp(0, 3, 0.5)
c1.evol_sp(0, 2, 0.9)
c1.evol_cp(0, -0.4)
np.testing.assert_allclose(
c.expectation_4body(0, 4, 1, 5), c.expectation_4body(0, 4, 1, 5), atol=1e-5
)
np.testing.assert_allclose(
c.expectation_4body(0, 1, 4, 5), c.expectation_4body(0, 1, 4, 5), atol=1e-5
)
np.testing.assert_allclose(
c.expectation_4body(0, 4, 2, 6), c.expectation_4body(0, 4, 2, 6), atol=1e-5
)
np.testing.assert_allclose(
c.expectation_4body(0, 2, 3, 1), c.expectation_4body(0, 2, 3, 1), atol=1e-5
)
np.testing.assert_allclose(
c.expectation_4body(0, 1, 4, 6), c.expectation_4body(0, 1, 4, 6), atol=1e-5
)
np.testing.assert_allclose(
c.expectation_4body(1, 0, 6, 7), c.expectation_4body(1, 0, 6, 7), atol=1e-5
)
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
def test_overlap(backend):
def compute_overlap(FGScls):
c = FGScls(3, filled=[0, 2])
c.evol_hp(0, 1, 1.2)
c.evol_hp(1, 2, 0.3)
c.evol_cp(0, 0.5)
c.evol_sp(0, 2, 0.3)
c1 = FGScls(3, filled=[0, 2])
c1.evol_hp(0, 1, 0.2)
c1.evol_hp(1, 2, 0.6)
c1.evol_sp(1, 0, -1.1)
return tc.backend.abs(c.overlap(c1))
np.testing.assert_allclose(
compute_overlap(tc.FGSSimulator), compute_overlap(tc.fgs.FGSTestSimulator), 1e-5
)