QKD System in Java
/*
Step 1: Create array lists
- Alice Qubits
- Alice basis
- Bob basis
- Bob measurements
- Eve eavesdrop
- Eve basis
Step 2: Eve eavesdrop a random number of events. Use a random array with approximately 1 in 4 chance of eve eavesdropping. If eavesdrop, choose a basis. If same basis keep number the same, if different randomize result.
Step 3: Have Bob 'measure' the qubits. If basis match, bob's measurements equal Alice's. If not, randomize bob's measurement.
Step 4: Delete all elements where basis are different between Alice and Bob
Step 5: parity check of 21 elements each. If correct go to next parity, and delete the last bit in the previously checked parity. If parity is not correct, discard the parity.
Step 6: Secret key is compiled
*/
int[] aliceQubits = new int[500]; //alice's qubit presets
int[] aliceBasis = new int[500]; //alice's basis presets
int[] bobBasis = new int[500];//bob's basis presets
int[] bobMeasurements = new int[500];//Bob's measurements of qubits
int[] eveEavesdrop = new int[500]; //Eve's chance of eavesdropping
int[] eveBasis = new int[500];//Eve's basis presets
int[] bobCode = new int[500];//Bob's string of qubit readings after elimating measurements with incorrect basis
int[] aliceCode = new int[500];//Alice's string of qubit readings after elimating measurements with incorrect basis
int[] aliceCode2 = new int[500]; //Alice's string of qubit readings after parity check
int[] bobCode2 = new int[500]; //Bob's string of qubit readings after parity check
public void setup() {
//Preset the measurements of Alice's Qubits
for (int i = 0; i < 500; i++) {
aliceQubits[i] = round(random(2));
;
}
//Preset the type of Basis Alice measures in
for (int i = 0; i < 500; i++) {
aliceBasis[i] = round(random(2));
}
//Preset the type of Basis Bob measures in
for (int i = 0; i < 500; i++) {
bobBasis[i] = round(random(2));
}
//Preset when Eve eavesdrops (25% chance Eve will eavesdrop)
for (int i = 0; i < 500; i++) {
eveEavesdrop[i] = round(random(4));
}
//Preset basis Eve will measure in
for (int i = 0; i < 500; i++) {
eveBasis[i] = round(random(2));
}
}
public void draw() {
noLoop();//only run the program once
performTheMeasurement(); //code for running the algorithm to determine the measurements of Eve and Bob
checkTheBasis(); //code to check whether the basis for Alice and Bob are the same.
parityCheck(); //perform parity check
}
public void checkTheBasis() {
for (int i = 0; i<500; i++) {
if (aliceBasis[i] == bobBasis[i]) { //if both basis match keep the measurement
aliceCode[i] = aliceQubits[i];
bobCode[i] = bobMeasurements[i];
}
}
}
public void parityCheck() {
int check = ceil(aliceCode.size()/21); //calculate number of parities
int currentNumber = 0; //current number is current element being added to aliceCode2 and bobCode2
for (int i = 1; i <= check; i++) {
int parityAlice = 0;//result of Alice's parity
int parityBob = 0; //result of Bob's parity
for (int k = 0; k < 21; k++) { //calculate the parity by adding the sum of the elements together
parityAlice = parityAlice + aliceCode[i*k];
parityBob = parityBob + bobCode[i*k];
}
//check if parity result is the same
if (parityAlice%2 == parityBob%2) {
//if parity results are equal, add elements in parity to the array
for (int k = 0; k < 20; k++) {
aliceCode2[currentNumber] = aliceCode[i*k];
bobCode2[currentNumber] = parityBob + bobCode[i*k];
currentNumber++;
}
}
}
}
public void performTheMeasurement() {
for (int i = 0; i < 500; i++) {
//Eve Eavesdropping
if (eveEavesdrop[i] == 3) { //Eve will eavesdrop when eveEavesdrop has randmly been set to three
if (aliceBasis[i] == eveBasis[i]) {
//eve has eavesdropped successfully
}
else {
aliceQubits[i] = round(random(2));
//eve eavesdrop in wrong basis resulting in random reset of qubit
}
}
//Bob measuring
if (bobBasis[i] == aliceBasis[i]) {
//if bob measures in same basis, he get's same result as Alice's qubit
bobMeasurements[i] = aliceQubits[i];
}
else {
//if bob chooses the incorrect basis, he get's a random result
bobMeasurements[i] = round(random(2));
}
}
}