Skip to content

Commit a46ef11

Browse files
Merge branch 'calib' of https://github.com/yutuer21/tensorcircuit into tqlpr62
2 parents 0bc1506 + 0d4ef7b commit a46ef11

File tree

1 file changed

+79
-52
lines changed

1 file changed

+79
-52
lines changed

tests/test_calibrating.py

+79-52
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,15 @@
1313
import tensorcircuit as tc
1414

1515

16-
def exp_fit(x, y):
17-
def fit_function(x_values, y_values, function, init_params):
18-
fitparams, _ = curve_fit(function, x_values, y_values, init_params)
19-
return fitparams
16+
def fit_function(x_values, y_values, function, init_params):
17+
fitparams, _ = curve_fit(function, x_values, y_values, init_params)
18+
return fitparams
2019

21-
fit_params = fit_function(
22-
x, y, lambda x, A, C, T: (A * np.exp(-x / T) + C), [-3, 0, 100]
23-
)
24-
25-
_, _, T = fit_params
2620

27-
return T
28-
29-
30-
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
31-
def test_calib_t1(backend):
32-
33-
t1 = 300
34-
t2 = 100
35-
time = 100
36-
nstep = int(4 * t1 / time)
21+
def T1_cali(t1, t2, time, method, excitedstatepopulation):
3722

3823
# calibrating experiments
24+
nstep = int(4 * t1 / time)
3925
pex = []
4026
for i in range(nstep):
4127

@@ -44,38 +30,42 @@ def test_calib_t1(backend):
4430
for _ in range(i):
4531
dmc.i(0)
4632
dmc.thermalrelaxation(
47-
0, t1=t1, t2=t2, time=time, method="AUTO", excitedstatepopulation=0
33+
0,
34+
t1=t1,
35+
t2=t2,
36+
time=time,
37+
method=method,
38+
excitedstatepopulation=excitedstatepopulation,
4839
)
4940

5041
val = dmc.expectation_ps(z=[0])
5142
p = (1 - val) / 2.0
5243
pex.append(p)
5344

54-
# data fitting
55-
x1 = np.array([i * time for i in range(nstep)])
56-
y1 = np.array(np.real(pex))
45+
timelist = np.array([i * time for i in range(nstep)])
46+
measurement = np.array(np.real(pex))
5747

58-
T1 = exp_fit(x1, y1)
59-
np.testing.assert_allclose(t1, T1, atol=1e-1)
48+
return measurement, timelist
6049

6150

62-
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
63-
def test_calib_t2(backend):
51+
def T2_cali(t1, t2, time, method, excitedstatepopulation):
6452

65-
pex = []
66-
t1 = 300
67-
t2 = 280
68-
time = 50
53+
# calibrating experiments
6954
nstep = int(4 * t2 / time)
70-
55+
pex = []
7156
for i in range(nstep):
7257

7358
dmc = tc.DMCircuit(1)
7459
dmc.h(0)
7560
for _ in range(0, i):
7661
dmc.i(0)
7762
dmc.thermalrelaxation(
78-
0, t1=t1, t2=t2, time=time, method="AUTO", excitedstatepopulation=0
63+
0,
64+
t1=t1,
65+
t2=t2,
66+
time=time,
67+
method=method,
68+
excitedstatepopulation=excitedstatepopulation,
7969
)
8070
# dmc.rz(0,theta = i*np.pi/1.5)
8171
dmc.h(0)
@@ -84,44 +74,81 @@ def test_calib_t2(backend):
8474
p = (1 - val) / 2.0
8575
pex.append(p)
8676

87-
# data fitting
88-
x1 = np.array([i * time for i in range(nstep)])
89-
y1 = np.array(np.real(pex))
90-
91-
T2 = exp_fit(x1, y1)
92-
np.testing.assert_allclose(t2, T2, atol=1e-1)
77+
timelist = np.array([i * time for i in range(nstep)])
78+
measurement = np.array(np.real(pex))
9379

80+
return measurement, timelist
9481

95-
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
96-
def test_calib_dep(backend):
9782

83+
def dep_cali(dep, nqubit):
9884
pex = []
99-
pz = 0.02
10085
nstep = 40
10186
for i in range(nstep):
10287

10388
dmc = tc.DMCircuit(1)
10489
dmc.x(0)
10590
for _ in range(i):
10691
dmc.s(0)
107-
dmc.generaldepolarizing(0, p=pz, num_qubits=1)
92+
dmc.generaldepolarizing(0, p=dep, num_qubits=nqubit)
10893

10994
val = dmc.expectation_ps(z=[0])
11095
p = (1 - val) / 2.0
11196
if i % 2 == 0:
11297
pex.append(p)
11398

114-
# data fitting
115-
x1 = np.array([i for i in range(0, nstep, 2)])
116-
y1 = np.array(np.real(pex))
99+
timelist = np.array([i for i in range(0, nstep, 2)])
100+
measurement = np.array(np.real(pex))
101+
102+
return measurement, timelist
103+
104+
105+
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
106+
def test_cali_t1(backend):
107+
t1 = 300
108+
t2 = 100
109+
time = 100
110+
method = "AUTO"
111+
excitedstatepopulation = 0
112+
measurement, timelist = T1_cali(t1, t2, time, method, excitedstatepopulation)
113+
114+
fit_params = fit_function(
115+
timelist, measurement, lambda x, A, C, T: (A * np.exp(-x / T) + C), [-3, 0, 100]
116+
)
117+
118+
_, _, T = fit_params
119+
120+
np.testing.assert_allclose(t1, T, atol=1e-1)
121+
122+
123+
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
124+
def test_cali_t2(backend):
125+
t1 = 300
126+
t2 = 280
127+
time = 50
128+
method = "AUTO"
129+
excitedstatepopulation = 0
130+
measurement, timelist = T2_cali(t1, t2, time, method, excitedstatepopulation)
117131

118-
def fit_function(x_values, y_values, function, init_params):
119-
fitparams, _ = curve_fit(function, x_values, y_values, init_params)
120-
return fitparams
132+
fit_params = fit_function(
133+
timelist, measurement, lambda x, A, C, T: (A * np.exp(-x / T) + C), [-3, 0, 100]
134+
)
135+
136+
_, _, T = fit_params
121137

122-
fit_params = fit_function(x1, y1, lambda x, A, B, C: (A * B**x + C), [-0, 0, 0])
138+
np.testing.assert_allclose(t2, T, atol=1e-1)
139+
140+
141+
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
142+
def test_cali_dep(backend):
143+
dep = 0.02
144+
nqubit = 1
145+
measurement, timelist = dep_cali(dep, nqubit)
146+
147+
fit_params = fit_function(
148+
timelist, measurement, lambda x, A, B, C: (A * B**x + C), [-0, 0, 0]
149+
)
123150

124151
_, B, _ = fit_params
125-
pz1 = (1 - B) / 4.0
152+
dep1 = (1 - B) / 4.0**nqubit
126153

127-
np.testing.assert_allclose(pz, pz1, atol=1e-1)
154+
np.testing.assert_allclose(dep, dep1, atol=1e-1)

0 commit comments

Comments
 (0)