From c87da45282b167a03ca37d5d09a5da7cd9299dfd Mon Sep 17 00:00:00 2001 From: miha-q <> Date: Sun, 11 Feb 2024 17:59:06 -0500 Subject: [PATCH] Sun Feb 11 05:59:06 PM EST 2024 --- examples/{bb84.qsm => bb84.txt} | 0 examples/{bellstate.qsm => bellstate.txt} | 2 +- examples/belltest.qsm | 188 ------------------ examples/belltest.sh | 5 +- examples/belltest_c.txt | 60 ++++++ examples/belltest_q.txt | 70 +++++++ examples/big.qsm | 32 --- examples/{bombtester.qsm => bombtester.txt} | 0 examples/decoherence.qsm | 4 - examples/decoherence.txt | 13 ++ examples/encryption.qsm | 6 - examples/encryption.txt | 12 ++ examples/{halfadder.qsm => halfadder.txt} | 2 +- examples/randombyte.txt | 12 ++ examples/send.sh | 28 +++ examples/{swaptest.qsm => swaptest.txt} | 0 .../{teleportation.qsm => teleportation.txt} | 0 src/QAnsel.c | 104 ++++++++-- 18 files changed, 291 insertions(+), 247 deletions(-) rename examples/{bb84.qsm => bb84.txt} (100%) rename examples/{bellstate.qsm => bellstate.txt} (73%) delete mode 100644 examples/belltest.qsm create mode 100644 examples/belltest_c.txt create mode 100644 examples/belltest_q.txt delete mode 100644 examples/big.qsm rename examples/{bombtester.qsm => bombtester.txt} (100%) delete mode 100644 examples/decoherence.qsm create mode 100644 examples/decoherence.txt delete mode 100644 examples/encryption.qsm create mode 100644 examples/encryption.txt rename examples/{halfadder.qsm => halfadder.txt} (98%) create mode 100644 examples/randombyte.txt create mode 100644 examples/send.sh rename examples/{swaptest.qsm => swaptest.txt} (100%) rename examples/{teleportation.qsm => teleportation.txt} (100%) diff --git a/examples/bb84.qsm b/examples/bb84.txt similarity index 100% rename from examples/bb84.qsm rename to examples/bb84.txt diff --git a/examples/bellstate.qsm b/examples/bellstate.txt similarity index 73% rename from examples/bellstate.qsm rename to examples/bellstate.txt index 9168026..d10fea4 100644 --- a/examples/bellstate.qsm +++ b/examples/bellstate.txt @@ -1,5 +1,5 @@ +//entanglement qreg q[2]; h q[0]; cx q[0], q[1]; - sample; diff --git a/examples/belltest.qsm b/examples/belltest.qsm deleted file mode 100644 index 4a028c3..0000000 --- a/examples/belltest.qsm +++ /dev/null @@ -1,188 +0,0 @@ -qreg q[3]; -creg c[4]; - -//Host generates random bits -// for the two players. -h q[0]; -h q[1]; -measure q[0] -> c[0]; -measure q[1] -> c[1]; -reset q[0]; -reset q[1]; -barrier q; - -//Two players are also provided -// an entangled qubit. -h q[0]; -cx q[0], q[1]; -barrier q; - -//Player X strategy -if(c==1) ry(pi/2) q[0]; -if(c==3) ry(pi/2) q[0]; -barrier q; - -//Player Y strategy -if(c==0) ry(pi/4) q[1]; -if(c==1) ry(pi/4) q[1]; -if(c==2) ry(-pi/4) q[1]; -if(c==3) ry(-pi/4) q[1]; -barrier q; - -//Transfer to host -measure q[0] -> c[2]; -measure q[1] -> c[3]; -barrier q; - -//a xor b -cx q[0], q[1]; -measure q[1] -> c[3]; -barrier q; - -//Load x and y -reset q[0]; -if(c==1) x q[0]; -if(c==3) x q[0]; -if(c==5) x q[0]; -if(c==7) x q[0]; -if(c==9) x q[0]; -if(c==11) x q[0]; -if(c==13) x q[0]; -if(c==15) x q[0]; -reset q[1]; -if(c==2) x q[1]; -if(c==3) x q[1]; -if(c==6) x q[1]; -if(c==7) x q[1]; -if(c==10) x q[1]; -if(c==11) x q[1]; -if(c==14) x q[1]; -if(c==15) x q[1]; -barrier q; - -//x and y -ccx q[0], q[1], q[2]; -measure q[2] -> c[2]; -barrier q; - -//Load (a xor b) and (x and y) -reset q[0]; -if(c==4) x q[0]; -if(c==5) x q[0]; -if(c==6) x q[0]; -if(c==7) x q[0]; -if(c==12) x q[0]; -if(c==13) x q[0]; -if(c==14) x q[0]; -if(c==15) x q[0]; -reset q[1]; -if(c==8) x q[1]; -if(c==9) x q[1]; -if(c==10) x q[1]; -if(c==11) x q[1]; -if(c==12) x q[1]; -if(c==13) x q[1]; -if(c==14) x q[1]; -if(c==15) x q[1]; -barrier q; - -//(a xor b) = (x and y) -cx q[0], q[1]; -x q[1]; -barrier q; - -//Store final results -measure q[1] -> c[0]; -measure q[1] -> c[1]; -measure q[1] -> c[2]; -measure q[1] -> c[3]; - -//Print results, 1111 is win, 0000 is loss -print c; -reset; - -//Classic Strategy - -//Host generates random bits -// for the two players. -h q[0]; -h q[1]; -measure q[0] -> c[0]; -measure q[1] -> c[1]; -barrier q; - -//Player X strategy -x q[0]; - -//Player Y strategy - -//Transfer to host -measure q[0] -> c[2]; -measure q[1] -> c[3]; -barrier q; - -//a xor b -cx q[0], q[1]; -measure q[1] -> c[3]; -barrier q; - -//Load x and y -reset q[0]; -if(c==1) x q[0]; -if(c==3) x q[0]; -if(c==5) x q[0]; -if(c==7) x q[0]; -if(c==9) x q[0]; -if(c==11) x q[0]; -if(c==13) x q[0]; -if(c==15) x q[0]; -reset q[1]; -if(c==2) x q[1]; -if(c==3) x q[1]; -if(c==6) x q[1]; -if(c==7) x q[1]; -if(c==10) x q[1]; -if(c==11) x q[1]; -if(c==14) x q[1]; -if(c==15) x q[1]; -barrier q; - -//x and y -ccx q[0], q[1], q[2]; -measure q[2] -> c[2]; -barrier q; - -//Load (a xor b) and (x and y) -reset q[0]; -if(c==4) x q[0]; -if(c==5) x q[0]; -if(c==6) x q[0]; -if(c==7) x q[0]; -if(c==12) x q[0]; -if(c==13) x q[0]; -if(c==14) x q[0]; -if(c==15) x q[0]; -reset q[1]; -if(c==8) x q[1]; -if(c==9) x q[1]; -if(c==10) x q[1]; -if(c==11) x q[1]; -if(c==12) x q[1]; -if(c==13) x q[1]; -if(c==14) x q[1]; -if(c==15) x q[1]; -barrier q; - -//(a xor b) = (x and y) -cx q[0], q[1]; -x q[1]; -barrier q; - -//Store final results -measure q[1] -> c[0]; -measure q[1] -> c[1]; -measure q[1] -> c[2]; -measure q[1] -> c[3]; - -//Print results, 1111 is win, 0000 is loss -print c; diff --git a/examples/belltest.sh b/examples/belltest.sh index a507e0a..c0efe05 100644 --- a/examples/belltest.sh +++ b/examples/belltest.sh @@ -8,9 +8,8 @@ do clear echo "$(( (100 * i) / trials ))%" - results="$(../bin/QAnsel < belltest.qsm | xargs)" - resultsQ="$(echo "$results" | sed 's/ .*//')" - resultsC="$(echo "$results" | sed 's/.* //')" + resultsC="$(../bin/QAnsel < belltest_c.txt | xargs | sed 's/ //g')" + resultsQ="$(../bin/QAnsel < belltest_q.txt | xargs | sed 's/ //g')" if [ "$resultsQ" == "1111" ] then qwins=$((qwins+1)) diff --git a/examples/belltest_c.txt b/examples/belltest_c.txt new file mode 100644 index 0000000..e80d1da --- /dev/null +++ b/examples/belltest_c.txt @@ -0,0 +1,60 @@ +qreg q[3]; +creg c[4]; + +//Classic Strategy + +//Host generates random bits +// for the two players. +h q[0]; +h q[1]; +measure q[0] -> c[0]; +measure q[1] -> c[1]; +barrier q; + +//Player X strategy +x q[0]; + +//Player Y strategy + +//Transfer to host +measure q[0] -> c[2]; +measure q[1] -> c[3]; +barrier q; + +//a xor b +cx q[0], q[1]; +measure q[1] -> c[3]; +barrier q; + +//Load x and y +reset q[0]; + +if(c[0]==1) x q[0]; +reset q[1]; +if(c[1]==1) x q[1]; +barrier q; + +//x and y +ccx q[0], q[1], q[2]; +measure q[2] -> c[2]; +barrier q; + +//Load (a xor b) and (x and y) +reset q[0]; +if(c[2]==1) x q[0]; +reset q[1]; +if(c[3]==1) x q[1]; +barrier q; + +//(a xor b) = (x and y) +cx q[0], q[1]; +x q[1]; +barrier q; + +//Store final results +measure q[1] -> c[0]; +measure q[1] -> c[1]; +measure q[1] -> c[2]; +measure q[1] -> c[3]; + +fullsample; \ No newline at end of file diff --git a/examples/belltest_q.txt b/examples/belltest_q.txt new file mode 100644 index 0000000..e0a862d --- /dev/null +++ b/examples/belltest_q.txt @@ -0,0 +1,70 @@ +qreg q[3]; +creg c[4]; + +//Host generates random bits +// for the two players. +h q[0]; +h q[1]; +measure q[0] -> c[0]; +measure q[1] -> c[1]; +reset q[0]; +reset q[1]; +barrier q; + +//Two players are also provided +// an entangled qubit. +h q[0]; +cx q[0], q[1]; +barrier q; + +//Player X strategy +//if(c[0]==0) do nothing +if(c[0]==1) ry(pi/2) q[0]; +barrier q; + +//Player Y strategy +if(c[1]==0) ry(pi/4) q[1]; +if(c[1]==1) ry(-pi/4) q[1]; +barrier q; + +//Transfer to host +measure q[0] -> c[2]; +measure q[1] -> c[3]; +barrier q; + +//a xor b +cx q[0], q[1]; +measure q[1] -> c[3]; +barrier q; + +//Load x and y +reset q[0]; +if(c[0]==1) x q[0]; +reset q[1]; +if(c[1]==1) x q[1]; +barrier q; + +//x and y +ccx q[0], q[1], q[2]; +measure q[2] -> c[2]; +barrier q; + +//Load (a xor b) and (x and y) +reset q[0]; +if(c[2]==1) x q[0]; +reset q[1]; +if(c[3]==1) x q[1]; +barrier q; + +//(a xor b) = (x and y) +cx q[0], q[1]; +x q[1]; +barrier q; + +//Store final results +measure q[1] -> c[0]; +measure q[1] -> c[1]; +measure q[1] -> c[2]; +measure q[1] -> c[3]; + +fullsample; \ No newline at end of file diff --git a/examples/big.qsm b/examples/big.qsm deleted file mode 100644 index 73b7c35..0000000 --- a/examples/big.qsm +++ /dev/null @@ -1,32 +0,0 @@ -qreg q[2]; - -//10 -x q[0]; - -print q[0]; -print q[1]; - -//beam splitter -cx q[0], q[1]; -ry(pi/4) q[0]; -cx q[1], q[0]; -ry(-pi/4) q[0]; -cx q[1], q[0]; -cx q[0], q[1]; -swap q[0], q[1]; - -print q; -//print q[0]; -//print q[1]; -// -////beam splitter -//cx q[0], q[1]; -//ry(pi/4) q[0]; -//cx q[1], q[0]; -//ry(-pi/4) q[0]; -//cx q[1], q[0]; -//cx q[0], q[1]; -//swap q[0], q[1]; - -print q[0]; -print q[1]; \ No newline at end of file diff --git a/examples/bombtester.qsm b/examples/bombtester.txt similarity index 100% rename from examples/bombtester.qsm rename to examples/bombtester.txt diff --git a/examples/decoherence.qsm b/examples/decoherence.qsm deleted file mode 100644 index 510fc71..0000000 --- a/examples/decoherence.qsm +++ /dev/null @@ -1,4 +0,0 @@ -qreg q[1]; -h q[0]; -rz($(rand)) q[0]; -density q[0]; \ No newline at end of file diff --git a/examples/decoherence.txt b/examples/decoherence.txt new file mode 100644 index 0000000..d5b10d7 --- /dev/null +++ b/examples/decoherence.txt @@ -0,0 +1,13 @@ +//Even if we don't bother reading the contents of +// c[1], the very fact the state of q[0] was +// recorded into something at all leads it +// to lose its ability to interfere with +// itself. +qreg q[1]; +creg c[2]; +h q[0]; +measure q[0] -> c[1]; +h q[0]; +measure q[0] -> c[0]; +reset c[1]; +fullsample; diff --git a/examples/encryption.qsm b/examples/encryption.qsm deleted file mode 100644 index 6be398b..0000000 --- a/examples/encryption.qsm +++ /dev/null @@ -1,6 +0,0 @@ -qreg q[1]; -creg c[1]; -h q[0]; -measure q[0] -> c[0]; -print c; - diff --git a/examples/encryption.txt b/examples/encryption.txt new file mode 100644 index 0000000..e26b2cb --- /dev/null +++ b/examples/encryption.txt @@ -0,0 +1,12 @@ +qreg q[1]; +creg c[8]; +h q[0]; measure q[0] -> c[0]; +h q[0]; measure q[0] -> c[1]; +h q[0]; measure q[0] -> c[2]; +h q[0]; measure q[0] -> c[3]; +h q[0]; measure q[0] -> c[4]; +h q[0]; measure q[0] -> c[5]; +h q[0]; measure q[0] -> c[6]; +h q[0]; measure q[0] -> c[7]; +print c; + diff --git a/examples/halfadder.qsm b/examples/halfadder.txt similarity index 98% rename from examples/halfadder.qsm rename to examples/halfadder.txt index af0f80b..2ba484f 100644 --- a/examples/halfadder.qsm +++ b/examples/halfadder.txt @@ -37,4 +37,4 @@ ccx q[0], q[1], q[2]; cx q[0], q[1]; measure q[1] -> c[0]; measure q[2] -> c[1]; -print c; \ No newline at end of file +print c; diff --git a/examples/randombyte.txt b/examples/randombyte.txt new file mode 100644 index 0000000..e26b2cb --- /dev/null +++ b/examples/randombyte.txt @@ -0,0 +1,12 @@ +qreg q[1]; +creg c[8]; +h q[0]; measure q[0] -> c[0]; +h q[0]; measure q[0] -> c[1]; +h q[0]; measure q[0] -> c[2]; +h q[0]; measure q[0] -> c[3]; +h q[0]; measure q[0] -> c[4]; +h q[0]; measure q[0] -> c[5]; +h q[0]; measure q[0] -> c[6]; +h q[0]; measure q[0] -> c[7]; +print c; + diff --git a/examples/send.sh b/examples/send.sh new file mode 100644 index 0000000..9549658 --- /dev/null +++ b/examples/send.sh @@ -0,0 +1,28 @@ +#Setup +username=amihart +authkey=d080a67eb0001182051047c1dee089f08cfb3881ef9d25e9aa6b643ef1918880 +#a5b07afd5bda06e9b188a13e450b1998875ae8f4c40b04b0206152d132f12250 +service=http://foleosoft.com/QAnsel +#file="qreg q[2]; creg c[2]; h q[0]; cx q[0], q[1]; measure q[0] -> c[0]; print c[0];" +file="qreg q[2]; creg c[2]; h q[0]; cx q[0], q[1]; measure q[0] -> c[0]; print c[0];" +#Send Request +sess=ba1f34dff1b1d1cefb72282b +#$(openssl rand -hex 12) +data=$(echo -n "$file" | openssl enc -chacha20 -K $authkey -iv 00000000$sess | base64 | tr -d '\n') +dgst=$(echo -n "$data" | base64 -d | openssl sha256 -hex -mac HMAC -macopt hexkey:$authkey | cut -d '=' -f 2 | xargs) + +resp=$(curl -s -X POST -d "user=$username&sess=$sess&dgst=$dgst&data=$data" "$service") + +#Parse response +for param in user sess dgst data +do + eval $param=$(echo "$resp" | sed -e "s/.*$param=//" -e "s/&.*//") 2>/dev/null +done +if [ "$user" = "$username" ] && [ "$(echo -n "$data" | base64 -d | openssl sha256 -hex -mac HMAC -macopt hexkey:$authkey | cut -d '=' -f 2 | xargs)" = "$dgst" ] +then + echo $resp + echo -n "$data" | base64 -d | openssl enc -d -chacha20 -K $authkey -iv 00000000$sess +else + #echo "An error was encountered!" + echo "$resp" +fi diff --git a/examples/swaptest.qsm b/examples/swaptest.txt similarity index 100% rename from examples/swaptest.qsm rename to examples/swaptest.txt diff --git a/examples/teleportation.qsm b/examples/teleportation.txt similarity index 100% rename from examples/teleportation.qsm rename to examples/teleportation.txt diff --git a/src/QAnsel.c b/src/QAnsel.c index b54c3a5..9a7ec0c 100644 --- a/src/QAnsel.c +++ b/src/QAnsel.c @@ -16,9 +16,8 @@ typedef struct double arg0, arg1, arg2; } QInstr; -double qansel_rand() +double qansel_rand_h() { - /* static uint32_t blockNumber = 0; uint8_t key[32]; uint8_t nonce[12]; @@ -40,15 +39,26 @@ double qansel_rand() num = (num << 8) | block[i]; } free(block); - */ + return ((double)num) / ((double)UINT32_MAX); +} + +double qansel_rand() +{ FILE* f = fopen("/dev/TrueRNG0", "r"); - uint32_t num = 0; - for (uint8_t i = 0; i < 4; i++) + if (f) { - num = (num << 8) | fgetc(f); + uint32_t num = 0; + for (uint8_t i = 0; i < 4; i++) + { + num = (num << 8) | fgetc(f); + } + fclose(f); + return ((double)num) / ((double)UINT32_MAX); + } + else + { + return qansel_rand_h(); } - fclose(f); - return ((double)num) / ((double)UINT32_MAX); } void qansel_cnot(cpx_mtx_t* stateVector, uint8_t qubitCount, uint8_t bitA, uint8_t bitB) @@ -280,7 +290,7 @@ uint8_t qansel_measure(cpx_mtx_t* stateVector, uint8_t qubitCount, uint8_t qubit return newBit; } -void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint32_t instrLen, uint8_t gfx) +void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint8_t* retBitVect, uint32_t instrLen, uint8_t gfx) { uint32_t qubitCountPow2 = (uint32_t)pow(2, qubitCount); @@ -292,6 +302,7 @@ void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint32_t in uint8_t flags; for (uint8_t i = 0; i < bitCount; i++) bitVector[i] = 0; + for (uint32_t i = 0; i < instrLen; i++) { if (strcmp(instr[i].n, "measure") == 0) @@ -479,9 +490,18 @@ void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint32_t in display(NULL, -1); cpx_mtx_free(&stateVector); + + if (retBitVect != NULL) + { + for (uint32_t i = 0; i < bitCount; i++) + { + retBitVect[i] = bitVector[i]; + } + } + } -void main(int argc, char** argv) +void process(int argc, char** argv) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); @@ -498,6 +518,7 @@ void main(int argc, char** argv) uint32_t lineID = 1; uint8_t skipSpaces = 1; uint8_t inGate = 0; + uint8_t fullSample = 0; while ( (c = getchar()) != EOF ) { if (c == '/' && commentM == 0 && comment == 0) @@ -1024,7 +1045,7 @@ void main(int argc, char** argv) strcpy(instr[instrLen].n, "printc"); instr[instrLen++].q0 = q0; } - else if (strcmp(lines[i], "sample") == 0) + else if (strcmp(lines[i], "sample") == 0 || strcmp(lines[i], "sample q") == 0) { if (qubitCount == 0xFF) { @@ -1126,6 +1147,22 @@ void main(int argc, char** argv) // optimizations that this instruction // would prevent } + else if (strcmp(lines[i], "fullsample") == 0) + { + if (i != linesLen - 1) + { + fprintf(stderr, "QAnsel: fullsample should be used at the end of the program."); + errFound = 1; + break; + } + if (bitCount == 0xFF) + { + fprintf(stderr, "QAnsel: fullsample cannot be used without initializing classical bits."); + errFound = 1; + break; + } + fullSample = 1; + } else { fprintf(stderr, "QAnsel: Syntax error"); @@ -1179,7 +1216,50 @@ void main(int argc, char** argv) } } - qansel_run(qubitCount, bitCount, instr, instrLen, doDisplay); + if (fullSample) + { + uint16_t stats[65536]; + for (uint32_t i = 0; i < (1 << bitCount); i++) + { + stats[i] = 0; + } + uint8_t bitVect[bitCount]; + for (int i = 0; i < bitCount; i++) bitVect[i] = 0; + + uint8_t* dat = malloc(instrLen * sizeof(QInstr)); + memcpy(dat, instr, instrLen * sizeof(QInstr)); + for (uint32_t i = 0; i < 1000; i++) + { + qansel_run(qubitCount, bitCount, instr, bitVect, instrLen, doDisplay); + memcpy(instr, dat, instrLen * sizeof(QInstr)); + uint16_t stat = 0; + for (uint8_t j = 0; j < bitCount; j++) + { + stat = (stat << 1) | bitVect[j]; + } + stats[stat]++; + } + free(dat); + for (uint32_t i = 0; i < (1 << bitCount); i++) + { + uint32_t tmp = i; + for (uint8_t j = 0; j < bitCount; j++) + { + putchar('0' + (tmp >> (bitCount - 1) & 1)); + tmp <<= 1; + } + printf(": %.00f%%\n", (double)stats[i] / (double)10); + } + } + else + { + qansel_run(qubitCount, bitCount, instr, NULL, instrLen, doDisplay); + } free(instr); free(lineIDs); } + +void main(int argc, char** argv) +{ + process(argc, argv); +} \ No newline at end of file -- 2.39.5