-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtest_two_qubits.py
225 lines (190 loc) · 7.15 KB
/
test_two_qubits.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
"""
integration testing module for C1 optimization through two-qubits example
"""
import pickle
import numpy as np
import pytest
import tensorflow as tf
from numpy.testing import assert_array_almost_equal as almost_equal
import c3.libraries.algorithms as algorithms
from c3.libraries.fidelities import state_transfer_from_states
# Libs and helpers
from c3.libraries.propagation import rk4_unitary
with open("test/two_qubit_data.pickle", "rb") as filename:
test_data = pickle.load(filename)
@pytest.mark.tensorflow
@pytest.mark.unit
def test_signals(get_two_qubit_chip) -> None:
"""Compare generated control signals to stored examples."""
exp = get_two_qubit_chip
pmap = exp.pmap
gen_signal = pmap.generator.generate_signals(pmap.instructions["rx90p[0]"])
ts = gen_signal["d1"]["ts"]
np.testing.assert_allclose(ts, test_data["ts"])
np.testing.assert_allclose(
actual=gen_signal["d1"]["values"].numpy(),
desired=test_data["signal"]["d1"]["values"].numpy(),
)
@pytest.mark.unit
def test_hamiltonians(get_two_qubit_chip) -> None:
"""Compare Hamilonians"""
model = get_two_qubit_chip.pmap.model
hdrift, hks = model.get_Hamiltonians()
assert (hdrift.numpy() - test_data["hdrift"].numpy() < 1).any()
for key in hks:
almost_equal(hks[key], test_data["hks"][key])
@pytest.mark.tensorflow
@pytest.mark.integration
def test_propagation(get_two_qubit_chip) -> None:
"""Test that result of the propagation code does not change."""
GATE_STR = "rx90p[0]"
exp = get_two_qubit_chip
pmap = exp.pmap
instr = pmap.instructions[GATE_STR]
steps = int((instr.t_end - instr.t_start) * exp.sim_res)
result = exp.propagation(
pmap.model,
pmap.generator,
pmap.instructions["rx90p[0]"],
exp.folding_stack[steps],
)
propagator = result["U"]
almost_equal(propagator, test_data["propagator"])
@pytest.mark.unit
def test_init_point(get_OC_optimizer) -> None:
"""Check that a previous best point can be loaded as an initial point."""
opt = get_OC_optimizer
opt.load_best("test/best_point_open_loop.c3log")
@pytest.mark.slow
@pytest.mark.tensorflow
@pytest.mark.optimizers
@pytest.mark.integration
def test_optim(get_OC_optimizer) -> None:
"""
check if optimization result is below 1e-1
"""
opt = get_OC_optimizer
assert opt.evaluation == 0
opt.optimize_controls()
assert opt.current_best_goal < 0.1
maxiterKey = "maxiters" if opt.algorithm == algorithms.tf_sgd else "maxiter"
assert opt.evaluation == opt.options[maxiterKey] - 1
@pytest.mark.slow
@pytest.mark.tensorflow
@pytest.mark.optimizers
@pytest.mark.integration
def test_optim_ode_solver(get_OC_optimizer) -> None:
"""
check if optimization result is below 1e-1
"""
opt = get_OC_optimizer
opt.fid_func = state_transfer_from_states
opt.set_goal_function(ode_solver="rk4", ode_step_function="schrodinger")
model = opt.exp.pmap.model
psi_init = [[0] * model.tot_dim]
psi_init[0][0] = 1 / np.sqrt(2)
index = model.get_state_indeces([(1, 0)])[0]
psi_init[0][index] = 1 / np.sqrt(2)
target_state = tf.transpose(tf.constant(psi_init, tf.complex128))
params = {"target": target_state}
opt.fid_func_kwargs = {"params": params}
assert opt.evaluation == 0
opt.optimize_controls()
if opt.algorithm == algorithms.single_eval:
assert opt.current_best_goal < 0.5
else:
assert opt.current_best_goal < 0.3
maxiterKey = "maxiters" if opt.algorithm == algorithms.tf_sgd else "maxiter"
assert opt.evaluation == opt.options[maxiterKey] - 1
@pytest.mark.slow
@pytest.mark.tensorflow
@pytest.mark.optimizers
@pytest.mark.integration
def test_optim_ode_solver_final(get_OC_optimizer) -> None:
"""
check if optimization result is below 1e-1
"""
opt = get_OC_optimizer
opt.fid_func = state_transfer_from_states
opt.set_goal_function(
ode_solver="rk4", ode_step_function="schrodinger", only_final_state=True
)
model = opt.exp.pmap.model
psi_init = [[0] * model.tot_dim]
psi_init[0][0] = 1 / np.sqrt(2)
index = model.get_state_indeces([(1, 0)])[0]
psi_init[0][index] = 1 / np.sqrt(2)
target_state = tf.transpose(tf.constant(psi_init, tf.complex128))
params = {"target": target_state}
opt.fid_func_kwargs = {"params": params}
assert opt.evaluation == 0
opt.optimize_controls()
if opt.algorithm == algorithms.single_eval:
assert opt.current_best_goal < 0.5
else:
assert opt.current_best_goal < 0.2
maxiterKey = "maxiters" if opt.algorithm == algorithms.tf_sgd else "maxiter"
assert opt.evaluation == opt.options[maxiterKey] - 1
@pytest.mark.tensorflow
@pytest.mark.integration
def test_rk4_unitary(get_two_qubit_chip) -> None:
"""Testing that RK4 exists and runs."""
exp = get_two_qubit_chip
pmap = exp.pmap
exp.set_prop_method(rk4_unitary)
exp.propagation(pmap.model, pmap.generator, pmap.instructions["rx90p[0]"])
@pytest.mark.tensorflow
@pytest.mark.integration
def test_ode_solver(get_two_qubit_chip) -> None:
"""Testing that ODE solver exists and runs for solvers rk4, rk5, tsit5."""
exp = get_two_qubit_chip
exp.set_opt_gates(["rx90p[0]"])
exp.compute_states(solver="rk4")
exp.compute_states(solver="rk5")
exp.compute_states(solver="tsit5")
exp.compute_states(solver="rk4", step_function="von_neumann")
exp.compute_states(solver="rk5", step_function="von_neumann")
exp.compute_states(solver="tsit5", step_function="von_neumann")
@pytest.mark.tensorflow
@pytest.mark.integration
def test_compute_final_state(get_two_qubit_chip) -> None:
"""Testing that compute_final_state exists and runs for solvers rk4, rk5, tsit5."""
exp = get_two_qubit_chip
exp.set_opt_gates(["rx90p[0]"])
exp.compute_final_state(solver="rk4")
exp.compute_final_state(solver="rk5")
exp.compute_final_state(solver="tsit5")
exp.compute_final_state(solver="rk4", step_function="von_neumann")
exp.compute_final_state(solver="rk5", step_function="von_neumann")
exp.compute_final_state(solver="tsit5", step_function="von_neumann")
@pytest.mark.slow
@pytest.mark.tensorflow
@pytest.mark.integration
def test_lindblad_propagation(get_two_qubit_chip) -> None:
"""Test that result of the propagation code does not change."""
GATE_STR = "rx90p[0]"
exp = get_two_qubit_chip
exp.propagate_batch_size = 360
pmap = exp.pmap
model = pmap.model
model.set_lindbladian(True)
instr = pmap.instructions[GATE_STR]
steps = int((instr.t_end - instr.t_start) * exp.sim_res)
result = exp.propagation(
model,
pmap.generator,
pmap.instructions["rx90p[0]"],
exp.folding_stack[steps],
)
propagator = result["U"]
almost_equal(propagator, test_data["lindblad_propagator"])
@pytest.mark.tensorflow
@pytest.mark.integration
def test_ode_solver_lindblad(get_two_qubit_chip) -> None:
"""Testing that ODE solver for lindbladian exists and runs for solvers."""
exp = get_two_qubit_chip
model = exp.pmap.model
model.set_lindbladian(True)
exp.set_opt_gates(["rx90p[0]"])
exp.compute_states(solver="rk4")
exp.compute_final_state(solver="rk4")