1#include "Quantum/Quantum.h"
34double SafeLog(
double x)
38 return log(x) / log(2.0);
40double entropy(
double x)
42 return -x*SafeLog(x) - (1-x)*SafeLog(1-x);
44double entropy(
double a,
double b,
double c,
double d)
46 return -a*SafeLog(a) - b*SafeLog(b) - c*SafeLog(c) - d*SafeLog(d);
50std::vector <algebra::mat> EVec;
51bool COMPUTE_ACTUAL_RATE =
false;
56 for(
int i=0; i<EVec.size(); ++i)
62double ComputeFitness(std::list <QKD::Gate>& A, std::list <QKD::Gate>& B)
67 const int numExtraA = 0;
69 const int T = extraA+numExtraA;
73 const int extraB = T+3;
74 const int numExtraB = 0;
76 const int E = extraB+numExtraB;
78 const int NUM_WIRES = E+1;
84 for(
int i=0; i<NUM_WIRES; ++i){
86 dl.dimension.push_back(2);
88 dl.dimension.push_back(4);
93 dl.density.push_back(kb);
97 std::list<QKD::Gate>::iterator Iter;
98 for(Iter = A.begin(); Iter != A.end(); ++Iter){
99 int target = Iter->target;
100 int control = Iter->control;
101 if(!Iter->controlMode)
105 if(target > ASpace || control > ASpace)
107 if(target == E || control == E)
110 if(Iter->controlMode && control == target)
114 case QKD::Hadamard: dl.applyHadamard(target, control);
117 case QKD::NOT: dl.applyNOT(target, control);
120 case QKD::Rotation: dl.applySU2(target, control, Iter->p, Iter->theta, Iter->psi);
123 case QKD::Measure: dl.measure(target);
129 dl.applyAttackOp(T, E);
133 std::list<QKD::Gate>::iterator Iter;
134 for(Iter = B.begin(); Iter != B.end(); ++Iter){
135 int target = Iter->target;
136 int control = Iter->control;
137 if(!Iter->controlMode)
141 if(target < T || (control < T && control >= 0))
143 if(target == E || control == E)
146 if(Iter->controlMode && control == target)
150 case QKD::Hadamard: dl.applyHadamard(target, control);
153 case QKD::NOT: dl.applyNOT(target, control);
156 case QKD::Rotation: dl.applySU2(target, control, Iter->p, Iter->theta, Iter->psi);
159 case QKD::Measure: dl.measure(target);
176 if(dl.density.empty())
184 while(!atk->getNext(EVec[0], EVec[1], EVec[2], EVec[3])){
186 double paccept = dl.trace(EVec, E);
189 double p00 = dl.calculatePr(keyA, 0, keyB, 0, EVec, E);
190 double p01 = dl.calculatePr(keyA, 0, keyB, 1, EVec, E);
191 double p10 = dl.calculatePr(keyA, 1, keyB, 0, EVec, E);
192 double p11 = dl.calculatePr(keyA, 1, keyB, 1, EVec, E);
194 double HAB = entropy(p00, p01, p10, p11);
195 double HB = entropy(p00 + p10);
199 for(
int w=keyA+1; w<E; ++w)
206 double SAE = dl.entropy(EVec, E);
208 double SE = dl.entropy(EVec, E);
211 if(COMPUTE_ACTUAL_RATE)
212 rate = (SAE - SE) - (HAB - HB);
214 rate = paccept * ( (SAE-SE) - (HAB-HB) );
226 std::list <QKD::Gate> A, B;
236 std::list <QKD::Gate> A, B;
240 g.controlMode =
false;
244 g.type = QKD::Hadamard;
246 g.controlMode =
false;
252 g.controlMode =
true;
261 g.controlMode =
true;
267 g.controlMode =
false;
272 std::cout <<
"Test 1: key-rate (should be 1) = " << ComputeFitness(A,B) <<
"\n\n";
278 std::list <QKD::Gate> A, B;
283 g.controlMode =
false;
288 g.controlMode =
true;
294 g.controlMode =
false;
302 g.controlMode =
true;
307 g.controlMode =
false;
312 std::cout <<
"Test 1: key-rate = " << ComputeFitness(A,B) <<
"\n\n";
316int main(
int argc,
char** argv)
320 stats.symmetric(.01);
322 setupAttackVectors(stats);
326 std::cout <<
"Above value should be = " << stats.BB84Rate() <<
"\n";