forked from eqclabs/tight_bound_leakage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeakage_Calculation_module.py
More file actions
157 lines (137 loc) · 4.23 KB
/
Leakage_Calculation_module.py
File metadata and controls
157 lines (137 loc) · 4.23 KB
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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors: Panagiotis Papanastasiou, Carlo Ottaviani, Marco Lucamarini
# Institution: Experimental Quantum Communications Lab, University of York
# Project: CHEDDAR (EPSRC and DSIT TMF-uplift, EP/X040518/1 and EP/Y037421/1)
# Intellectual Property: Held by the University of York
# Year: 2025
#################################################################
import numpy as np
import scipy
from scipy.integrate import nquad, dblquad,quad
from scipy.special import erfinv
from scipy import integrate,linalg
from scipy.special import erfinv, erf
#################################################################
#probabilities
def pk(k,alpha,p):
if k==0:
ak=-np.infty
bk=((2/2**p)*(k+1)-1)*alpha
elif k==2**p-1:
ak=((2/2**p)*k-1)*alpha
bk=np.infty
else:
ak=((2/2**p)*k-1)*alpha
bk=((2/2**p)*(k+1)-1)*alpha
A=erf((ak)/np.sqrt(2))
B=erf((bk)/np.sqrt(2))
return (1/2)*(B-A)
def pk_x(k,x,rho,alpha,p):
if k==0:
ak=-np.infty
bk=((2/2**p)*(k+1)-1)*alpha
elif k==2**p-1:
ak=((2/2**p)*k-1)*alpha
bk=np.infty
else:
ak=((2/2**p)*k-1)*alpha
bk=((2/2**p)*(k+1)-1)*alpha
A=erf((ak-x*rho)/np.sqrt(2*(1-rho**2)))
B=erf((bk-x*rho)/np.sqrt(2*(1-rho**2)))
return (1/2)*(B-A)
def px(x):
return (1/np.sqrt((2*np.pi)))*np.exp(-x**2/2)
def pkx(k,x,rho,alpha,p):
return px(x)*pk_x(k,x,rho,alpha,p)
#conditional entropy, conditioanl variance, & finite-size leakage
def cond_ent_fun(x,k,rho,alpha,p):
A=pk_x(k,x,rho,alpha,p)
if A==0:
S=0
else:
S=pkx(k,x,rho,alpha,p)*(-np.log2(pk_x(k,x,rho,alpha,p)))
return S
def cond_var_fun(x,k,rho,alpha,p):
A=pk_x(k,x,rho,alpha,p)
if A==0:
S=0
else:
S=pkx(k,x,rho,alpha,p)*(-np.log2(pk_x(k,x,rho,alpha,p)))**2
return S
def sum_ent(x,rho,alpha,p):
S=0
for k in range(2**p):
S+=cond_ent_fun(x,k,rho,alpha,p)
return S
def sum_var(x,rho,alpha,p):
S=0
for k in range(2**p):
S+=cond_var_fun(x,k,rho,alpha,p)
return S
def cond_ent(rho,alpha,p):
return integrate.quad(sum_ent,-4,4,args=(rho,alpha,p))[0]
def cond_var(rho,alpha,p):
return integrate.quad(sum_var,-4,4,args=(rho,alpha,p))[0]
def leak(n,p_ec,rho,alpha,p):
H=cond_ent(rho,alpha,p)
V=cond_var(rho,alpha,p)-H**2
revPhi=erfinv(p_ec)
RestTerms=(1/2)*np.log2(n)
return n*H+np.sqrt(n*V)*revPhi
#descretization & beta_quant
def Hk(alpha,p):
S=0
for k in range(2**p):
A=pk(k,alpha,p)
if A==0 or A==1:
S+=0
else:
S+=-A*np.log2(A)
return S
def sum_Hkx(x,rho,alpha,p):
S=0
for k in range(2**p):
A=pkx(k,x,rho,alpha,p)
if A==0 or A==1:
S+=0
else:
S+=-A*np.log2(A)
return S
def Hkx(rho,alpha,p):
return integrate.quad(sum_Hkx,-4,4,args=(rho,alpha,p))[0]
def IMxk(rho,alpha,p):
Hx=(1/2)*np.log2(2*np.pi*np.exp(1))
return Hx+Hk(alpha,p)-Hkx(rho,alpha,p)
def IMxy(rho):
return -(1/2)*np.log2(1-rho**2)
def beta_quant(rho,alpha,p):
return IMxk(rho,alpha,p)/IMxy(rho)
#===============================================================
# ###################################
# n=10**4
# p_ec=0.99
# rho=0.4
# alpha=8
# p=10
# x=0.3
# k=0
# start=time.time()
# #print(cond_ent_fun(x,k,rho,alpha,p))
# print(leak(n,p_ec,rho,alpha,p))
# #print(cond_ent_0(rho,alpha,p))
# print("Time",time.time()-start)
#-----------------------------------------
# start=time.time()
# print(cond_ent_1(rho,alpha,p))
# print("Time",time.time()-start)