-
Notifications
You must be signed in to change notification settings - Fork 2
/
protocol_units_efficient.py
459 lines (394 loc) · 14.6 KB
/
protocol_units_efficient.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import logging
import numpy as np
import numba as nb
__all__ = ["join_links_efficient"]
##############################################################################
"""
Swap and distillation for werner parameter
Parameters
----------
pmf1, pmf2: 1d array-like
The waiting time distribution of the two input links, Pr(T=t).
w_func1, w_func2: 1d array-like
The Werner parameter function, W(t).
t_coh: int or float
The coherence time of the memory.
Returns
-------
Four arrays of the shape `t_trunc` used in efficient computation.
"""
def get_one_werner(pmf1, pmf2, w_func1, w_func2, t_coh):
return pmf1, pmf1, pmf2, pmf2
def get_w1w2_werner(pmf1, pmf2, w_func1, w_func2, t_coh):
"""
func1_later = Pr(T1=t1) * w1 * exp(-t1)
func1_early = Pr(T1=t1) * w1 * exp(+t1)
func2_later = Pr(T2=t2) * w2 * exp(-t2)
func1_early = Pr(T2=t2) * w2 * exp(+t2)
"""
size = len(pmf1)
decay_factors = np.exp(- np.arange(size) / t_coh) # exp(-t)
func1_later = pmf1 * w_func1 * decay_factors
func1_early = pmf1 * w_func1 / decay_factors
func2_later = pmf2 * w_func2 * decay_factors
func2_early = pmf2 * w_func2 / decay_factors
return func1_early, func1_later, func2_early, func2_later
def get_w1_werner(pmf1, pmf2, w_func1, w_func2, t_coh):
"""
func1_later = Pr(T1=t1) * w1 * exp(-t1)
func1_early = Pr(T1=t1) * w1 * exp(+t1)
func2_later = Pr(T2=t2) * exp(-t2)
func1_early = Pr(T2=t2) * exp(+t2)
"""
size = len(pmf1)
decay_factors = np.exp(- np.arange(size) / t_coh)
func1_later = pmf1 * w_func1
func2_early = pmf2
func1_early = pmf1 * w_func1 / decay_factors
func2_later = pmf2 * decay_factors
return func1_early, func1_later, func2_early, func2_later
def get_w2_werner(pmf1, pmf2, w_func1, w_func2, t_coh):
"""
func1_later = Pr(T1=t1) * exp(-t1)
func1_early = Pr(T1=t1) * exp(+t1)
func2_later = Pr(T2=t2) * w2 * exp(-t2)
func1_early = Pr(T2=t2) * w2 * exp(+t2)
"""
size = len(pmf1)
decay_factors = np.exp(- np.arange(size) / t_coh)
func1_later = pmf1 * decay_factors
func2_early = pmf2 * w_func2 / decay_factors
func1_early = pmf1
func2_later = pmf2 * w_func2
return func1_early, func1_later, func2_early, func2_later
##############################################################################
"""
State representation for entanglement swapping
The post swapping state is given by
m2 * (n1 + n2) + m1 * (n3 + n4), where * means element-wise multiplication.
See the Mathematica notebook for detailed calculation.
Parameters
----------
pmf1, pmf2: 1d array-like
The waiting time distribution of the two input links, Pr(T=t).
m, n: 3d array-like
The time dependent array of quantum states
with the shape `[t_trunc,4,4]`.
t_coh: int or float
The coherence time of the memory.
Returns
-------
Four arrays of the shape `[t_trunc,4,4]` used in efficient computation.
"""
@nb.jit(nopython=True, error_model="python")
def identity_matrix_array(shape, dtype):
identity_array = np.zeros(shape, dtype)
for i in range(shape[0]):
for j in range(shape[1]):
identity_array[i, j, j] = 1.
return identity_array
def get_decay_dm(pmf1, pmf2, m, n, t_coh):
""")
func1_later = Pr(T1=t1) * I/4 * exp(-t1)
func1_early = Pr(T1=t1) * I/4 * exp(+t1)
func2_later = Pr(T2=t2) * I/4 * exp(-t2)
func1_early = Pr(T2=t2) * I/4 * exp(+t2)
"""
size = len(pmf1)
decay_factors = np.exp(- np.arange(size) / t_coh)
identity = identity_matrix_array(m.shape, m.dtype)
identity = np.transpose(identity, (1, 2, 0))
func1_later = 1/4. * pmf1 * identity * decay_factors
func1_early = 1/4. * pmf1 * identity / decay_factors
func2_later = pmf2 * identity * decay_factors
func2_early = pmf2 * identity / decay_factors
func1_later = np.transpose(func1_later, (2, 0, 1))
func1_early = np.transpose(func1_early, (2, 0, 1))
func2_later = np.transpose(func2_later, (2, 0, 1))
func2_early = np.transpose(func2_early, (2, 0, 1))
return func1_early, func1_later, func2_early, func2_later
def get_one_dm(pmf1, pmf2, m, n, t_coh):
"""
func1 = Pr(T1=t1) * I/4
func2 = Pr(T2=t2) * I/4
"""
identity = np.zeros(m.shape, m.dtype)
for i in range(m.shape[0]):
for j in range(m.shape[1]):
identity[i, j, j] = 1.
identity = np.transpose(identity, (1, 2, 0))
func1_later = pmf1 * identity * 1/4.
func1_early = pmf1 * identity * 1/4.
func2_later = pmf2 * identity
func2_early = pmf2 * identity
func1_later = np.transpose(func1_later, (2, 0, 1))
func1_early = np.transpose(func1_early, (2, 0, 1))
func2_later = np.transpose(func2_later, (2, 0, 1))
func2_early = np.transpose(func2_early, (2, 0, 1))
return func1_early, func1_later, func2_early, func2_later
def apply_to_all_time_wrapper(func):
"""
Broad cast the given function acting on
an array with the shape `[4, 4]`
to a function acting on an array with the shape `[t_trunc, 4, 4]`.
"""
@nb.jit(nopython=True, error_model="python")
def inner(state_array):
result = np.empty(state_array.shape, dtype=state_array.dtype)
for i in range(state_array.shape[0]):
result[i] = func(state_array[i])
return result
return inner
@apply_to_all_time_wrapper
@nb.jit(nopython=True, error_model="python")
def m1(m):
return np.asarray([
[m[1, 1], m[1, 0], m[1, 3], m[1, 2]],
[m[0, 1], m[0, 0], m[0, 3], m[0, 2]],
[m[3, 1], m[3, 0], m[3, 3], m[3, 2]],
[m[2, 1], m[2, 0], m[2, 3], m[2, 2]]
])
@apply_to_all_time_wrapper
@nb.jit(nopython=True, error_model="python")
def m2(m):
return np.asarray([
[m[0, 0], m[0, 1], m[0, 2], m[0, 3]],
[m[1, 0], m[1, 1], m[1, 2], m[1, 3]],
[m[2, 0], m[2, 1], m[2, 2], m[2, 3]],
[m[3, 0], m[3, 1], m[3, 2], m[3, 3]]
])
@apply_to_all_time_wrapper
@nb.jit(nopython=True, error_model="python")
def n1(n):
return np.asarray([
[n[1, 1], n[1, 2], n[1, 1], n[1, 2]],
[n[2, 1], n[2, 2], n[2, 1], n[2, 2]],
[n[1, 1], n[1, 2], n[1, 1], n[1, 2]],
[n[2, 1], n[2, 2], n[2, 1], n[2, 2]]
])
@apply_to_all_time_wrapper
@nb.jit(nopython=True, error_model="python")
def n2(n):
return np.asarray([
[n[2, 2], n[2, 1], n[2, 2], n[2, 1]],
[n[1, 2], n[1, 1], n[1, 2], n[1, 1]],
[n[2, 2], n[2, 1], n[2, 2], n[2, 1]],
[n[1, 2], n[1, 1], n[1, 2], n[1, 1]]
])
@apply_to_all_time_wrapper
@nb.jit(nopython=True, error_model="python")
def n3(n):
return np.asarray([
[n[0, 0], n[0, 3], n[0, 0], n[0, 3]],
[n[3, 0], n[3, 3], n[3, 0], n[3, 3]],
[n[0, 0], n[0, 3], n[0, 0], n[0, 3]],
[n[3, 0], n[3, 3], n[3, 0], n[3, 3]]
])
@apply_to_all_time_wrapper
@nb.jit(nopython=True, error_model="python")
def n4(n):
return np.asarray([
[n[3, 3], n[3, 0], n[3, 3], n[3, 0]],
[n[0, 3], n[0, 0], n[0, 3], n[0, 0]],
[n[3, 3], n[3, 0], n[3, 3], n[3, 0]],
[n[0, 3], n[0, 0], n[0, 3], n[0, 0]]
])
def get_mn_array(pmf1, pmf2, m, n, t_coh):
"""
Compute
pA M(tA) exp(-tA/t_coh)
pA M(tA) exp(+tA/t_coh)
pB N(tB) exp(-tB/t_coh)
pB N(tB) exp(+tB/t_coh)
They will be used to compute e.g.
pA * pB * M(tA) * N(tB) exp((tA-tB)/t_coh)
"""
size = len(pmf1)
decay_factors = np.exp(- np.arange(size) / t_coh)
m = np.transpose(m, (1, 2, 0))
n = np.transpose(n, (1, 2, 0))
func1_later = pmf1 * m * decay_factors
func1_early = pmf1 * m / decay_factors
func2_later = pmf2 * n * decay_factors
func2_early = pmf2 * n / decay_factors
func1_later = np.transpose(func1_later, (2, 0, 1))
func1_early = np.transpose(func1_early, (2, 0, 1))
func2_later = np.transpose(func2_later, (2, 0, 1))
func2_early = np.transpose(func2_early, (2, 0, 1))
return func1_early, func1_later, func2_early, func2_later
def get_m2n1_array(pmf1, pmf2, m, n, t_coh):
return get_mn_array(pmf1, pmf2, m2(m), n1(n), t_coh)
def get_m2n2_array(pmf1, pmf2, m, n, t_coh):
return get_mn_array(pmf1, pmf2, m2(m), n2(n), t_coh)
def get_m1n3_array(pmf1, pmf2, m, n, t_coh):
return get_mn_array(pmf1, pmf2, m1(m), n3(n), t_coh)
def get_m1n4_array(pmf1, pmf2, m, n, t_coh):
return get_mn_array(pmf1, pmf2, m1(m), n4(n), t_coh)
###############################################################################
# API function for merging two entangled states by swap or distillation.
def join_links_efficient(
pmf1, pmf2, w_func1, w_func2,
cutoff=np.iinfo(np.int32).max, ycut=True,
cut_type=None, evaluate_func=None, t_coh=np.inf):
"""
Calculate P_s and P_f efficiently using cumulative function.
Only memory time cutoff is supported.
The implementation includes both werner parameter representation
and density matrix representation.
Note
----
For swap the success probability p is
considered in the iterative convolution.
For the memory time cut-off,
the constant shift is added in the iterative convolution.
Parameters
----------
pmf1, pmf2: array-like
The waiting time distribution of the two input links, Pr(T=t).
w_func1, w_func2: array-like
The Werner parameter function, W(t).
cutoff: int or float
The cut-off threshold.
ycut: bool
Success ful cut-off or failed cut-off.
cutoff_type: str
Type of cut-off.
`memory_time`, `run_time` or `fidelity`.
evaluate_func: str
The function used to evaluate the distribution.
t_coh: int or float
The coherence time of the memory.
Returns
-------
result: array-like 1-D
The resulting array of joining the two links.
"""
if cut_type == "memory_time":
mt_cut = cutoff
else:
raise NotImplementedError("Unknown cut-off type.")
if evaluate_func == "1":
evaluate_coeff_list = (1,)
final_coeff = 1.
evaluate_func_list = (get_one_werner,)
kind = "probability"
elif evaluate_func == "w1w2":
if len(w_func1.shape) == 1:
evaluate_coeff_list = (1,)
final_coeff = 1.
evaluate_func_list = (get_w1w2_werner,)
else:
# m2 * (n1 + n2) + m1 * (n3 + n4), ELEMENT-WISE
evaluate_coeff_list = (
1.,
-1.,
1.,
1.,
1.,
1.,
)
final_coeff = 1.
evaluate_func_list = (
get_one_dm,
get_decay_dm,
get_m2n1_array,
get_m2n2_array,
get_m1n3_array,
get_m1n4_array,
)
kind = "state"
elif evaluate_func == "0.5+0.5w1w2":
evaluate_coeff_list = (0.5, 0.5)
final_coeff = 1.
evaluate_func_list = (get_one_werner, get_w1w2_werner)
kind = "probability"
elif evaluate_func == "0.5-0.5w1w2":
evaluate_coeff_list = (0.5, -0.5)
final_coeff = 1.
evaluate_func_list = (get_one_werner, get_w1w2_werner)
kind = "probability"
elif evaluate_func == "w1+w2+4w1w2":
evaluate_coeff_list = (1., 1., 4.)
final_coeff = 1./6.
evaluate_func_list = (get_w1_werner, get_w2_werner, get_w1w2_werner)
kind = "state"
elif isinstance(evaluate_func, str):
raise ValueError(evaluate_func)
size = len(pmf1)
if size/t_coh > 300:
logging.warn("Overflow in the exponential function!")
if kind == "probability":
final_result = np.zeros(pmf1.shape, dtype=pmf1.dtype)
elif kind == "state":
final_result = np.zeros(w_func1.shape, dtype=w_func1.dtype)
for evaluate_coeff, evaluate_func in zip(evaluate_coeff_list, evaluate_func_list):
# separate the positive and negative part for numerical stability
if kind == "probability":
result = np.zeros(pmf1.shape, dtype=pmf1.dtype)
minus_result = np.zeros(pmf1.shape, dtype=pmf1.dtype)
elif kind == "state":
result = np.zeros(w_func1.shape, dtype=w_func1.dtype)
minus_result = np.zeros(w_func1.shape, dtype=w_func1.dtype)
func1_early, func1_later, func2_early, func2_later = evaluate_func(
pmf1, pmf2, w_func1, w_func2, t_coh)
if ycut:
cum_func1_early = cumsum(func1_early)
cum_func2_early = cumsum(func2_early)
result = join_with_suc_cutoff(
cutoff, result, minus_result,
cum_func1_early, func1_later, cum_func2_early, func2_later)
else:
# waiting time is min(t1, t2), fix the early link as t
cum_func1_later = cumsum(func1_later)
cum_func2_later = cumsum(func2_later)
result = join_with_fail_cutoff(
cutoff, result, minus_result,
func1_early, func1_later, cum_func1_later,
func2_early, func2_later, cum_func2_later)
final_result += evaluate_coeff * result
final_result = final_coeff * final_result
return final_result
@nb.jit(nopython=True, error_model="python")
def join_with_suc_cutoff(
cutoff, result, minus_result, cum_func1_early, func1_later, cum_func2_early, func2_later):
"""
Core algorithms for efficient computation of
the time and state distribution.
"""
for t in range(1, len(result)):
cut = max(0, t - cutoff - 1)
# link 2 wait
result[t] += func1_later[t] * cum_func2_early[t]
minus_result[t] += func1_later[t] * cum_func2_early[cut]
# link 1 wait
result[t] += func2_later[t] * cum_func1_early[t-1]
minus_result[t] += func2_later[t] * cum_func1_early[cut]
result -= minus_result
return result
@nb.jit(nopython=True, error_model="python")
def join_with_fail_cutoff(
cutoff, result, minus_result,
func1_early, func1_later, cum_func1_later, func2_early, func2_later, cum_func2_later):
"""
waiting time is min(t1, t2), fix the early link as t
"""
for t in range(1, len(result) - cutoff):
cut = t + cutoff
# link 1 wait
result[t] += func2_early[t] * cum_func1_later[-1]
minus_result[t] += func2_later[t] * cum_func1_later[cut]
# link 2 wait
result[t] += func1_early[t] * cum_func2_later[-1]
minus_result[t] += func1_later[t] * cum_func2_later[cut]
result -= minus_result
return result
def cumsum(array):
if len(array.shape) == 1:
return np.cumsum(array)
else:
return state_cumsum(array)
@nb.jit(nopython=True, error_model="python")
def state_cumsum(array):
for i in range(0, array.shape[0]-1):
array[i+1] += array[i]
return array