-
Notifications
You must be signed in to change notification settings - Fork 0
/
getKeyRate.m
308 lines (254 loc) · 12.5 KB
/
getKeyRate.m
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
%% FUNCTION NAME: getKeyRate
% The core backend function for calculating key rate. This function takes
% protocol description, channel model (expectations) and error-correction leakage, and returns upper/lower bound and gap
% It calls solver1 and solver2 and returns key rate.
%
% Input Data Structure:
% protocol description: [keyMap,krausOperators,observables,obsMask(optional)]
% channel model: [expectations,expMask(optional)probDist/errorRate,pSift]
% EC description: [leakageEC]
% solverOptions: [globalSetting, solver1, solver2]
% (optional) parameter and name list: can be retrieved using findParameter("PARAMETER_NAME",solverOptions)
%
% Output Data Structure:
% lowerBound, upperBound, FWBound, success flag
%%
function [lowerBound, upperBound, FWBound, debugInfo]=getKeyRate(protocolDescription,channelModel,leakageEC,solverOptions,p,names)
tstart_iteration = tic;
try
cvx_solver(solverOptions.globalSetting.cvxSolver);
cvx_precision(solverOptions.globalSetting.cvxPrecision);
if(solverOptions.globalSetting.verboseLevel==3)
cvx_quiet(false);
else
cvx_quiet(true);
end
catch Error
if(contains(Error.message,'Undefined function'))
fprintf('cvx installation not found\n')
end
if(contains(Error.message,'missing solver'))
fprintf('%s\n',Error.message)
end
upperBound = 0;
lowerBound = 0;
FWBound = 0;
debugInfo.exitStatus = 'cvx solver error';
debugInfo.errorMessage = getReport(Error);
fprintf("**** cvx solver setup error! ****\n")
return
end
if(solverOptions.globalSetting.verboseLevel>=2)
solverOptions.solver1.verbose = 'yes';
else
solverOptions.solver1.verbose = 'no';
end
%%%%%%%%%%%%%% call step 1 solver %%%%%%%%%%%%%%
solver1Status = [];
try
switch(solverOptions.solver1.name)
case 'asymptotic'
rho0 = eye(prod(protocolDescription.dimensions));
[rho,fval,gap,solver1Status]= step1SolverAsymptotic(rho0,protocolDescription.keyMap,protocolDescription.observables,channelModel.expectations,protocolDescription.krausOp,solverOptions.solver1);
case 'asymptotic_inequality'
rho0 = eye(prod(protocolDescription.dimensions));
[rho,fval,gap,solver1Status]= step1SolverAsymptoticInequality(rho0,protocolDescription.keyMap,protocolDescription.observables,protocolDescription.obsMask,channelModel.expectations,channelModel.expMask,protocolDescription.krausOp,solverOptions.solver1);
case 'finite'
%read input data
dim = protocolDescription.dimensions;
mask=protocolDescription.obsMask;
observables = protocolDescription.observables;
expectations = channelModel.expectations;
%additional parameters that can be read from full input parameter list
N=findParameter("N",names,p);
ptest=findParameter("ptest",names,p);
eps=findParameter("eps",names,p);
m=N*ptest; %signals used for testing
L=length(mask(mask>0));
mu=muCheckSimplified(eps.PE,L,m);
%check if the user has selected post-selection technique for coherent attack. If so, output warning message.
if (hasParameter("postselection",names) && findParameter("postselection",names,p) == 1 && solverOptions.globalSetting.verboseLevel>=1)
physDimAB = findParameter("physDimAB",names,p);
fprintf('**** using post-selection technique for coherent attack ****\n')
if(log10(sum(eps))+(physDimAB^2+1)*log10(N+1) >= 0)
fprintf('**** security cannot be guaranteed with coherent attack, please retry with a smaller N or eps ****\n');
fprintf('**** for current N, eps need to be at least smaller than 1e-%d ****\n',ceil((physDimAB^2+1)*log10(N+1)));
error("security parameter too large")
else
fprintf('**** note that the security parameter is now %e ****\n',sum(eps)*(N+1)^(physDimAB^2+1));
end
end
%parse observable-expectation pairs using the masks
%uncertain observables are labeled as 1
uncObs=applyMask(observables,mask,1);
freqs=applyMask(expectations,mask,1);
certObs=applyMask(observables,mask,0);
probs=applyMask(expectations,mask,0);
%check that observables are POVMs
flagTestPOVM = isPOVM2(uncObs);
if(flagTestPOVM~=1)
fprintf("**** Error: set of observables not POVM! ****\n")
end
rho0 = eye(prod(protocolDescription.dimensions));
[rho,fval,~,gap,solver1Status]= step1SolverFinite(rho0,protocolDescription.keyMap,...
uncObs,freqs,certObs,probs,...
mu,protocolDescription.krausOp,solverOptions.solver1);
otherwise
error("step 1 solver name not found")
end
%check validity of rho and perform perturbation if not valid
[rho,~]=perturbation_channel(rho);
debugInfo.solver1Status = solver1Status;
catch Error
debugInfo.exitStatus = 'step 1 solver error';
debugInfo.errorMessage = getReport(Error);
debugInfo.solver1Status = solver1Status;
fprintf("**** step 1 solver error ****\n")
fprintf("**** %s ****\n",Error.message)
upperBound = 0;
lowerBound = 0;
FWBound = 0;
return
end
%%%%%%%%%%%%%% call step 2 solver %%%%%%%%%%%%%%
solver2Status = [];
try
switch(solverOptions.solver2.name)
case 'asymptotic'
N=numel(protocolDescription.observables);
cons = zeros(1, N);
for i =1:N
cons(i) = abs(real(trace(rho * protocolDescription.observables{i}) - channelModel.expectations(i)));
end
solverOptions.solver2.epsilonprime = max(cons);
[val,solver2Status] = step2SolverAsymptotic(rho, protocolDescription.observables,channelModel.expectations,[],[], protocolDescription.keyMap, protocolDescription.krausOp, solverOptions.solver2);
case 'asymptotic_inequality'
%parse observable-expectation pairs using the masks
%uncertain observables are labeled as 1
obsMask=protocolDescription.obsMask;
LCertObs=length(obsMask(obsMask==0));
LUncObs=length(obsMask(obsMask==1));
observables=protocolDescription.observables;
expectations=channelModel.expectations;
CertObs=observables(1:LCertObs);
UncObs=observables(LCertObs+1:end);
CertExp=expectations(1:LCertObs);
UncExpL=expectations(LCertObs+1:LCertObs+LUncObs);
UncExpU=expectations(LCertObs+LUncObs+1:end);
N=numel(channelModel.expectations);
cons = zeros(1, N);
for i =1:LCertObs
cons(i) = abs(real(trace(rho * CertObs{i}) - CertExp(i)));
end
for i =1:LUncObs
cons(i+LCertObs) = real(UncExpL(i)-trace(rho * UncObs{i}));
cons(i+LCertObs+LUncObs) = real(trace(rho * UncObs{i}) - UncExpU(i));
end
solverOptions.solver2.epsilonprime = max(cons);%1e-6
for i=1:length(UncObs)
UncObsNeg{i,1}=-1*UncObs{i,1};
end
[val,solver2Status] = step2SolverAsymptotic(rho, CertObs,CertExp,[UncObs;UncObsNeg],[UncExpU;-UncExpL], protocolDescription.keyMap, protocolDescription.krausOp, solverOptions.solver2);
case 'finite'
cons = zeros(1, (numel(certObs)));
for iBasisElm = 1:numel(certObs)
cons(iBasisElm) = abs(real(trace(rho * certObs{iBasisElm}) - probs(iBasisElm)));
end
solverOptions.solver2.epsilonprime = max(cons);
[val,~,solver2Status] = step2SolverFinite(rho,uncObs,freqs,certObs,probs, protocolDescription.keyMap, mu, protocolDescription.krausOp, solverOptions.solver2);
otherwise
error("step 2 solver name not found")
end
debugInfo.solver2Status = solver2Status;
catch Error
debugInfo.exitStatus = 'step 2 solver error';
debugInfo.errorMessage = getReport(Error);
debugInfo.solver2Status = solver2Status;
fprintf("**** step 2 solver error ****\n")
fprintf("**** %s ****\n",Error.message)
upperBound = 0;
lowerBound = 0;
FWBound = 0;
return
end
%%%%%%%%%%%%%% combine privacy amplification and leakage to form key rate %%%%%%%%%%%%%%
try
if(strcmp(solverOptions.solver1.name,'asymptotic_inequality'))
%here we only calculate the case of decoy states using asymptotic_inequality solver
%(considering single-photon contribution)
pSignal=channelModel.pSignal;
upperBound = pSignal*fval/log(2) - leakageEC;
FWBound = pSignal*(fval-gap)/log(2) - leakageEC;
lowerBound = pSignal*val/log(2) - leakageEC;
elseif(strcmp(solverOptions.solver1.name,'finite'))
%finite size key rate
%considering collective attack
d=findParameter("alphabet",names,p); %the encoding alphabet size
n = N*(1-ptest)*sum(channelModel.pSift); %received coding signals
delta = 2*log2(d+3)*sqrt(log2(2/eps.bar)/n);
correction = (log2(2/eps.EC) + 2*log2(2/eps.PA))/N;
upperBound = (1-ptest)*(fval/log(2)-delta) - correction - (1-ptest)*leakageEC;
FWBound = (1-ptest)*((fval-gap)/log(2)-delta) - correction - (1-ptest)*leakageEC;
lowerBound = (1-ptest)*(val/log(2)-delta) - correction - (1-ptest)*leakageEC;
else
%default key rate (asymptotic, single-photon source)
upperBound = fval/log(2) - leakageEC;
FWBound = (fval-gap)/log(2) - leakageEC;
lowerBound = val/log(2) - leakageEC;
end
upperBound = max(0.0, upperBound);
FWBound = max(0.0, FWBound);
lowerBound = max(0.0, lowerBound);
if(solverOptions.globalSetting.verboseLevel>0)
fprintf("upperBound: %f, lowerBound: %f\n",upperBound,lowerBound);
end
catch Error
upperBound = 0;
lowerBound = 0;
FWBound = 0;
debugInfo.exitStatus = 'key rate calculation error';
debugInfo.errorMessage = getReport(Error);
fprintf("**** key rate calculation error! ****\n")
fprintf("**** %s ****\n",Error.message)
end
debugInfo.exitStatus = 'success';
debugInfo.errorMessage = '';
t_iteration=toc(tstart_iteration);
if(solverOptions.globalSetting.verboseLevel>0)
fprintf('iteration time: %f s\n\n',t_iteration)
end
end
%%%%%%%%%%% helper functions %%%%%%%%%%%%%%%%%
%select part of a cell/numerical array using another same-length mask array
%can be used to parse incoming observable (expectation) into groups with obsMask (expMask)
function a_m=applyMask(a,msk,label)
if(length(a)~=length(msk))
fprintf('**** mask length mismatch! ****\n')
end
a_m=[];
for i=1:length(msk)
if(msk(i)==label)
a_m=[a_m;a(i)];
end
end
end
%retrieve a single parameter with a given "varName" label from the input parameter list p of this iteration
function varValue=findParameter(varName,names,p)
varValues = findVariables(varName,names,p); %returns a cell array
if (length(varValues)==0)
fprintf('**** parameter %s not found! ****\n',varName);
varValue = [];
else
varValue = varValues{1}; %retrieve the parameter value (can be a numerical value or an array/matrix)
end
end
%checks whether a single parameter with a given "varName" label exists in the input parameter list p of this iteration
function found=hasParameter(varName,names)
found = false;
for j=1:length(names)
if(strcmp(varName,names(j)))
found = true;
break; %repeated entries will be ignored
end
end
end