-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_protocol.py
220 lines (186 loc) · 7.34 KB
/
test_protocol.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
import numpy as np
from numpy.testing import assert_allclose, run_module_suite, assert_
import pytest
import multiprocessing
from repeater_mc import repeater_mc
from repeater_algorithm import repeater_sim, RepeaterChainSimulation
from protocol_units import *
from logging_utilities import *
from utility_functions import *
from optimize_cutoff import CutoffOptimizer
pytestmark = pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_fidelity_cut_off_function():
w_cut = 0.95
t_coh = 50
assert(fidelity_cut_off(1, 3, 0.98, 0.98, w_cut=w_cut, t_coh=t_coh) == (2, False))
assert(fidelity_cut_off(2, 3, 0.98, 0.98, w_cut=w_cut, t_coh=t_coh) == (3, True))
assert(fidelity_cut_off(4, 3, 0.98, 0.98, w_cut=w_cut, t_coh=t_coh) == (4, True))
assert(fidelity_cut_off(3, 3, 0.98, 0.98, w_cut=w_cut, t_coh=t_coh) == (3, True))
assert(fidelity_cut_off(6, 3, 0.98, 0.98, w_cut=w_cut, t_coh=t_coh) == (4, False))
assert(fidelity_cut_off(1, 4, 0.98, 0.94, w_cut=w_cut, t_coh=t_coh) == (2, False))
assert(fidelity_cut_off(1, 2, 0.98, 0.94, w_cut=w_cut, t_coh=t_coh) == (2, False))
assert(fidelity_cut_off(4, 4, 0.95, 0.95, w_cut=w_cut, t_coh=t_coh) == (4, True))
assert(fidelity_cut_off(4, 4, 0.95, 0.9499, w_cut=w_cut, t_coh=t_coh) == (4, False))
def test_cutoff_dict_generation():
parameters = {
"protocol": (0, 0),
"p_gen": 0.5,
"p_swap": 0.8,
"w0": 1.0,
"t_coh": 20,
"t_trunc": 100,
"cut_type": "fidelity",
}
cutoff_dict = create_cutoff_dict((100, 120, 0.5, 0.4), ["memory_time", "fidelity"], parameters)
assert_allclose(cutoff_dict["memory_time"], np.array([100, 120]))
assert_allclose(cutoff_dict["fidelity"], np.array([0.5, 0.4]))
cutoff_dict = create_cutoff_dict((0.8, ), ["fidelity"], parameters)
assert_allclose(cutoff_dict["fidelity"], np.array([0.8, 0.8]))
cutoff_dict = create_cutoff_dict((100, ), ["memory_time"], parameters)
assert_allclose(cutoff_dict["memory_time"], np.array([100, 100]))
cutoff_dict = create_cutoff_dict((100, 0.5), ["memory_time", "fidelity"], parameters)
assert_allclose(cutoff_dict["memory_time"], np.array([100, 100]))
assert_allclose(cutoff_dict["fidelity"], np.array([0.5, 0.5]))
def test_secret_key_rate():
"""
Test secret key rate with exponential extrapolation.
"""
parameters = {
"protocol": (0, ),
"p_gen": 0.1,
"p_swap": 0.5,
"w0": 0.99,
"tau": 5,
"t_coh": 30,
"t_trunc": 236
}
pmf, w_func = repeater_sim(parameters)
key_rate = secret_key_rate(pmf, w_func, extrapolation=True)
assert_allclose(key_rate, 0.0148367, rtol=1.e-3)
swap_only_protocol = {
"protocol": (0, 0, 0),
"p_gen": 0.5,
"p_swap": 0.8,
"sample_size": 100000,
"w0": 1.,
"t_coh": 50,
"t_trunc": 100
}
dist_only_protocol = {
"protocol": (1, 1),
"p_gen": 0.5,
"p_swap": 0.8,
"sample_size": 100000,
"w0": 1.,
"t_coh": 20,
"t_trunc": 100
}
memory_cutoff_parameters1 = {
"protocol": (0, 0, 0),
"p_gen": 0.5,
"p_swap": 0.8,
"mt_cut": 5,
"sample_size": 200000,
"w0": 1.,
"t_coh": 30,
"t_trunc": 100
}
memory_cutoff_parameters2 = {
"protocol": (1, 0, 1, 0, 1, 0),
"p_gen": 0.5,
"p_swap": 0.8,
"mt_cut": (3, 6, 10, 14, 25, 100),
"sample_size": 200000,
"w0": 1.,
"t_coh": 30,
"t_trunc": 150
}
fidelity_cutoff_parameters = {
"protocol": (1, 0),
"p_gen": 0.5,
"p_swap": 0.8,
"w0": 0.99,
"t_coh": 50,
"t_trunc": 100,
"cut_type": "fidelity",
"w_cut": 0.9,
"sample_size": 1000000,
}
runtime_cutoff_parameters = {
"protocol": (1, 0),
"p_gen": 0.5,
"p_swap": 0.8,
"w0": 0.99,
"t_coh": 50,
"t_trunc": 100,
"cut_type": "run_time",
"rt_cut": (5, 10),
"sample_size": 1000000,
}
_convolution_simulator = RepeaterChainSimulation()
_convolution_simulator.use_fft = False
_fft_simulator = RepeaterChainSimulation()
_fft_simulator.use_fft = True
_gpu_simulator = RepeaterChainSimulation()
_gpu_simulator.use_fft = True
_gpu_simulator.use_gpu = True
_gpu_simulator.gpu_threshold = 1
def default_solution(parameters):
simulator = RepeaterChainSimulation()
simulator.efficient = False
simulator.use_fft = False
pmf, w_func = simulator.nested_protocol(parameters)
return pmf, w_func
@pytest.mark.parametrize("parameters, expect",
[
pytest.param(swap_only_protocol, default_solution(swap_only_protocol), id="swap_only"),
pytest.param(dist_only_protocol, default_solution(dist_only_protocol), id="dist_only"),
pytest.param(memory_cutoff_parameters1, default_solution(memory_cutoff_parameters1), id="swap_memory_cutoff"),
pytest.param(memory_cutoff_parameters2, default_solution(memory_cutoff_parameters2), id="swap_dist_memory_cutoff"),
])
@pytest.mark.parametrize(("simulator", "efficient"),
[
pytest.param(_convolution_simulator, False, id="compartible-conv"),
pytest.param(_convolution_simulator, True, id="efficient-conv"),
pytest.param(_fft_simulator, True, id="compartible-fft"),
pytest.param(_fft_simulator, False, id="efficient-fft"),
pytest.param(_gpu_simulator, True, id="compartible-gpu"),
])
def test_algorithm(parameters, expect, simulator, efficient):
default_pmf, default_w_func = expect
simulator.efficient = True
pmf, w_func = simulator.nested_protocol(parameters)
cdf = np.cumsum(pmf)
start_pos = next(x[0] for x in enumerate(cdf) if x[1] > 1.0e-2)
end_pos = np.searchsorted(cdf, 0.99)
assert_allclose(pmf[start_pos: end_pos], default_pmf[start_pos: end_pos], rtol=1.0e-7)
assert_allclose(w_func[start_pos: end_pos], default_w_func[start_pos: end_pos], rtol=1.0e-7)
@pytest.mark.parametrize(
"parameters, begin, end, rtol_t, rtol_w",
[
pytest.param(swap_only_protocol, 5, 12, 0.03, 0.02, id="Swap only protocol"),
pytest.param(dist_only_protocol, 3, 12, 0.03, 0.02, id="Dist only protocol"),
pytest.param(memory_cutoff_parameters1, 2, 17, 0.04, 0.02, id="Swap with memory cutoff"),
pytest.param(memory_cutoff_parameters2, 10, 40, 0.03, 0.02, id="Mixed protocol with memory cutoff"),
pytest.param(fidelity_cutoff_parameters, 2, 12, 0.03, 0.01, id="Fidelity cutoff"),
pytest.param(runtime_cutoff_parameters, 2, 12, 0.03, 0.01, id="Runtime cutoff"),
])
def test_against_MC(parameters, begin, end, rtol_t, rtol_w):
simulator = RepeaterChainSimulation()
pmf, w_func = simulator.nested_protocol(parameters)
cdf = np.cumsum(pmf)
pmf_sim, w_func_sim = repeater_mc(parameters, return_pmf=True)
cdf_sim = np.cumsum(pmf_sim)
assert_allclose(cdf[begin: end], cdf_sim[begin: end], rtol=rtol_t)
assert_allclose(w_func[begin: end], w_func_sim[begin: end], rtol=rtol_w)
@pytest.mark.skip(reason="Used only locally")
@pytest.mark.filterwarnings("ignore:Record with ID")
def test_record():
parameters1 = {'ID': 'test1', 'remark': 'test_log'}
parameters2 = {'ID': 'test2', 'remark': 'test_log'}
assert(find_record_id("test1") == parameters1)
assert(find_record_id("test2") == parameters2)
assert(len(find_record_patterns({'remark': "test_log"})) == 2)
assert(find_record_id("does not exist") is None)
assert(find_record_patterns({'ID': "does not exist"}) == [])
# # test using cutoff as key