Skip to content

Commit

Permalink
Merge pull request #84 from GabrieleMessina/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
GabrieleMessina authored Aug 19, 2024
2 parents 5dbe902 + 1b7044b commit 01ce0f1
Show file tree
Hide file tree
Showing 43 changed files with 2,299 additions and 1,733 deletions.
47 changes: 47 additions & 0 deletions playground/examples/arrays.qut
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// string[] bob_bases = ["1","0","1","0","1","0","1","0"];
// foreach string base in bob_bases{
// if(bob_bases[0] == base){
// print base;
// }
// else{
// print base;
// }
// }
// foreach string base, int index in bob_bases{
// print "start";
// print index;
// print base;
// print bob_bases[index];
// print "end";
// }

qubit b = 1q;
qubit c = 0q;
qubit[] a = [b, c, |+>];
qubit d = a[2];

foreach qubit q in a{
print q;
}

int[] ba = [2, 3];
foreach int q in ba{
print q;
}

// print d;
// print a;
// print c;

//=========================================================

// int[] foo = [6, 2, 3];
// print foo;
// print foo[1];

// int count=0;
// foreach int i in foo{
// print i;
// count = count + i;
// }
// print count;
229 changes: 35 additions & 194 deletions playground/examples/quantum_key_distribution_bb84.qut
Original file line number Diff line number Diff line change
@@ -1,212 +1,53 @@
int n = 8; //number of qubits

//Alice create a random binary sequence
string message_0 = "1";
string message_1 = "0";
string message_2 = "1";
string message_3 = "0";
string message_4 = "0";
string message_5 = "1";
string message_6 = "0";
string message_7 = "1";
qubit[] original_message = [0q, 0q, 0q, 0q, 0q, 0q, 0q, 0q];
qubit[] message = [0q, 0q, 0q, 0q, 0q, 0q, 0q, 0q];

//Alice create a random binary sequence of basis
string alice_bases0 = "1";
string alice_bases1 = "1";
string alice_bases2 = "0";
string alice_bases3 = "0";
string alice_bases4 = "0";
string alice_bases5 = "1";
string alice_bases6 = "1";
string alice_bases7 = "1";
string[] alice_bases = ["1","1","0","0","0","1","1","1"];

//Alice encodes the message with the basis
//for each character in message, if the corresponding character in bases is 0, then encode the character in Z basis, otherwise in X basis
qubit alice_0 = 1q;
qubit alice_1 = 0q;
qubit alice_2 = 1q;
qubit alice_3 = 0q;
qubit alice_4 = 0q;
qubit alice_5 = 1q;
qubit alice_6 = 0q;
qubit alice_7 = 1q;

//Bob create a random binary sequence of basis
string bob_bases0 = "1";
string bob_bases1 = "0";
string bob_bases2 = "1";
string bob_bases3 = "0";
string bob_bases4 = "1";
string bob_bases5 = "0";
string bob_bases6 = "1";
string bob_bases7 = "0";


if (alice_bases0 == "0"){
//encode message_1 in Z basis
//do nothing.
} else {
//encode message_0 in X basis
hadamard alice_0;
}
if (alice_bases1 == "0"){
//encode message_1 in Z basis
//do nothing.
} else {
//encode message_1 in X basis
hadamard alice_1;
}
if (alice_bases2 == "0"){
//encode message_2 in Z basis
//do nothing.
} else {
//encode message_2 in X basis
hadamard alice_2;
}
if (alice_bases3 == "0"){
//encode message_3 in Z basis
//do nothing.
} else {
//encode message_3 in X basis
hadamard alice_3;
}
if (alice_bases4 == "0"){
//encode message_4 in Z basis
//do nothing.
} else {
//encode message_4 in X basis
hadamard alice_4;
foreach string base, int index in alice_bases{
if(base == "0"){
//encode message in Z basis
//do nothing
} else {
//encode message in X basis
hadamard message[index];
}
}
if (alice_bases5 == "0"){
//encode message_5 in Z basis
//do nothing.
} else {
//encode message_5 in X basis
hadamard alice_5;
}
if (alice_bases6 == "0"){
//encode message_6 in Z basis
//do nothing.
} else {
//encode message_6 in X basis
hadamard alice_6;
}
if (alice_bases7 == "0"){
//encode message_7 in Z basis
//do nothing.
} else {
//encode message_7 in X basis
hadamard alice_7;
}


//Alice sends the encoded message to Bob
// -----> Alice sends the encoded message to Bob
// <----- Bob receives the encoded message from Alice


//Bob create a random binary sequence of basis
string[] bob_bases = ["1","0","1","0","1","0","1","0"];



if (bob_bases0 == "0"){
//encode message_1 in Z basis
//do nothing.
} else {
//encode message_0 in X basis
hadamard alice_0;
}

if (bob_bases1 == "0"){
//encode message_1 in Z basis
//do nothing.
} else {
//encode message_1 in X basis
hadamard alice_1;
}
if (bob_bases2 == "0"){
//encode message_2 in Z basis
//do nothing.
} else {
//encode message_2 in X basis
hadamard alice_2;
}
if (bob_bases3 == "0"){
//encode message_3 in Z basis
//do nothing.
} else {
//encode message_3 in X basis
hadamard alice_3;
}
if (bob_bases4 == "0"){
//encode message_4 in Z basis
//do nothing.
} else {
//encode message_4 in X basis
hadamard alice_4;
}
if (bob_bases5 == "0"){
//encode message_5 in Z basis
//do nothing.
} else {
//encode message_5 in X basis
hadamard alice_5;
foreach string base, int index in bob_bases{
if(base == "0"){
//encode message in Z basis
//do nothing
} else {
//encode message in X basis
hadamard message[index];
}
}
if (bob_bases6 == "0"){
//encode message_6 in Z basis
//do nothing.
} else {
//encode message_6 in X basis
hadamard alice_6;
}
if (bob_bases7 == "0"){
//encode message_7 in Z basis
//do nothing.
} else {
//encode message_7 in X basis
hadamard alice_7;
}




// <----> Alice and Bob reveal their bases to each other

if(bob_bases0 == alice_bases0){
print "Index 0 match so it can be used to create a key";
print alice_0;
print message_0;
}
if(bob_bases1 == alice_bases1){
print "Index 1 match so it can be used to create a key";
print alice_1;
print message_1;
}
if(bob_bases2 == alice_bases2){
print "Index 2 match so it can be used to create a key";
print alice_2;
print message_2;
}
if(bob_bases3 == alice_bases3){
print "Index 3 match so it can be used to create a key";
print alice_3;
print message_3;
}
if(bob_bases4 == alice_bases4){
print "Index 4 match so it can be used to create a key";
print alice_4;
print message_4;
}
if(bob_bases5 == alice_bases5){
print "Index 5 match so it can be used to create a key";
print alice_5;
print message_5;
}
if(bob_bases6 == alice_bases6){
print "Index 6 match so it can be used to create a key";
print alice_6;
print message_6;
}
if(bob_bases7 == alice_bases7){
print "Index 7 match so it can be used to create a key";
print alice_7;
print message_7;
//Alice and Bob compare their bases and discard the bits where their bases do not match
foreach string base, int index in bob_bases{
if(alice_bases[index] == base){
print "Index match so it can be used to create a key, index: ";
print index;
print " that correspond to this original message: ";
print original_message[index];
println;
}
}

measure;

print "If the above values matches, then no one is eyesdropping.";
55 changes: 55 additions & 0 deletions playground/examples/quantum_key_distribution_bb84_eyesdropping.qut
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//Alice create a random binary sequence
string[] original_message = ["1", "0", "1", "0", "0", "1", "0", "1"];
qubit[] message = [1q, 0q, 1q, 0q, 0q, 1q, 0q, 1q];

//Alice create a random binary sequence of basis
string[] alice_bases = ["1","1","0","0","0","1","1","1"];

//Alice encodes the message with the basis
//for each character in message, if the corresponding character in bases is 0, then encode the character in Z basis, otherwise in X basis
foreach string base, int index in alice_bases{
if(base == "0"){
//encode message in Z basis
//do nothing
} else {
//encode message in X basis
hadamard message[index];
}
}

// -----> Alice sends the encoded message to Bob
// --X--> But Eve intercepts the message and tries to decode it
measure;
// <----- Bob receives the encoded message from Alice


//Bob create a random binary sequence of basis
string[] bob_bases = ["1","0","1","0","1","0","1","0"];

foreach string base, int index in bob_bases{
if(base == "0"){
//encode message in Z basis
//do nothing
} else {
//encode message in X basis
hadamard message[index];
}
}


// <----> Alice and Bob reveal their bases to each other

//Alice and Bob compare their bases and discard the bits where their bases do not match
foreach string base, int index in bob_bases{
if(alice_bases[index] == base){
print "Index match so it can be used to create a key, index: ";
print index;
print " that correspond to this original message: ";
print original_message[index];
println;
}
}

measure;

print "If the measured values matches the printed ones, then no one is eyesdropping.";
Loading

0 comments on commit 01ce0f1

Please sign in to comment.