From: miha-q <> Date: Sun, 11 Feb 2024 22:59:06 +0000 (-0500) Subject: Sun Feb 11 05:59:06 PM EST 2024 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=c87da45282b167a03ca37d5d09a5da7cd9299dfd;p=QAnsel.git Sun Feb 11 05:59:06 PM EST 2024 --- diff --git a/examples/bb84.qsm b/examples/bb84.qsm deleted file mode 100644 index a283806..0000000 --- a/examples/bb84.qsm +++ /dev/null @@ -1,26 +0,0 @@ -qreg q[2]; -creg c[1]; - -//Alice sends two bits 00 -h q[0]; -h q[1]; - -//Eve tries to measure the first -measure q[0] -> c[0]; - -//Bob applies the gate before measuring -h q[0]; -h q[1]; - -//The first should now be random noise -// while the second unmeasured one -// should be 0 -sample; - -//In a full BB84 Alice would randomly -// apply the gate or not apply the -// gate so Eve would have to guess -// and would inevitably end up -// turning the transferred bits -// into random noise whenever there -// is a wrong guess. \ No newline at end of file diff --git a/examples/bb84.txt b/examples/bb84.txt new file mode 100644 index 0000000..a283806 --- /dev/null +++ b/examples/bb84.txt @@ -0,0 +1,26 @@ +qreg q[2]; +creg c[1]; + +//Alice sends two bits 00 +h q[0]; +h q[1]; + +//Eve tries to measure the first +measure q[0] -> c[0]; + +//Bob applies the gate before measuring +h q[0]; +h q[1]; + +//The first should now be random noise +// while the second unmeasured one +// should be 0 +sample; + +//In a full BB84 Alice would randomly +// apply the gate or not apply the +// gate so Eve would have to guess +// and would inevitably end up +// turning the transferred bits +// into random noise whenever there +// is a wrong guess. \ No newline at end of file diff --git a/examples/bellstate.qsm b/examples/bellstate.qsm deleted file mode 100644 index 9168026..0000000 --- a/examples/bellstate.qsm +++ /dev/null @@ -1,5 +0,0 @@ -qreg q[2]; -h q[0]; -cx q[0], q[1]; - -sample; diff --git a/examples/bellstate.txt b/examples/bellstate.txt new file mode 100644 index 0000000..d10fea4 --- /dev/null +++ b/examples/bellstate.txt @@ -0,0 +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.qsm deleted file mode 100644 index fb71f5c..0000000 --- a/examples/bombtester.qsm +++ /dev/null @@ -1,33 +0,0 @@ -qreg q[2]; -creg c[3]; - -//Get random bomb -h q[0]; -measure q[0] -> c[0]; -if(c==1) x q[0]; - -//10 -x 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[0], q[1]; - -//If bomb, measure and store -if(c==1) measure q[0] -> c[2]; - -//beam splitter -cx q[0], q[1]; -ry(pi/4) q[0]; -cx q[1], q[0]; -ry(-pi/4) q[0]; -cx q[0], q[1]; - -//Final measurement -measure q[0] -> c[0]; -measure q[1] -> c[1]; - -print c; \ No newline at end of file diff --git a/examples/bombtester.txt b/examples/bombtester.txt new file mode 100644 index 0000000..fb71f5c --- /dev/null +++ b/examples/bombtester.txt @@ -0,0 +1,33 @@ +qreg q[2]; +creg c[3]; + +//Get random bomb +h q[0]; +measure q[0] -> c[0]; +if(c==1) x q[0]; + +//10 +x 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[0], q[1]; + +//If bomb, measure and store +if(c==1) measure q[0] -> c[2]; + +//beam splitter +cx q[0], q[1]; +ry(pi/4) q[0]; +cx q[1], q[0]; +ry(-pi/4) q[0]; +cx q[0], q[1]; + +//Final measurement +measure q[0] -> c[0]; +measure q[1] -> c[1]; + +print c; \ No newline at end of file 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.qsm deleted file mode 100644 index af0f80b..0000000 --- a/examples/halfadder.qsm +++ /dev/null @@ -1,40 +0,0 @@ -qreg q[3]; -creg c[2]; - -//0+0 -ccx q[0], q[1], q[2]; -cx q[0], q[1]; -measure q[1] -> c[0]; -measure q[2] -> c[1]; -print c; - -reset; - -//0+1 -x q[0]; -ccx q[0], q[1], q[2]; -cx q[0], q[1]; -measure q[1] -> c[0]; -measure q[2] -> c[1]; -print c; - -reset; - -//1+0 -x q[1]; -ccx q[0], q[1], q[2]; -cx q[0], q[1]; -measure q[1] -> c[0]; -measure q[2] -> c[1]; -print c; - -reset; - -//1+1 -x q[0]; -x q[1]; -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 diff --git a/examples/halfadder.txt b/examples/halfadder.txt new file mode 100644 index 0000000..2ba484f --- /dev/null +++ b/examples/halfadder.txt @@ -0,0 +1,40 @@ +qreg q[3]; +creg c[2]; + +//0+0 +ccx q[0], q[1], q[2]; +cx q[0], q[1]; +measure q[1] -> c[0]; +measure q[2] -> c[1]; +print c; + +reset; + +//0+1 +x q[0]; +ccx q[0], q[1], q[2]; +cx q[0], q[1]; +measure q[1] -> c[0]; +measure q[2] -> c[1]; +print c; + +reset; + +//1+0 +x q[1]; +ccx q[0], q[1], q[2]; +cx q[0], q[1]; +measure q[1] -> c[0]; +measure q[2] -> c[1]; +print c; + +reset; + +//1+1 +x q[0]; +x q[1]; +ccx q[0], q[1], q[2]; +cx q[0], q[1]; +measure q[1] -> c[0]; +measure q[2] -> c[1]; +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.qsm deleted file mode 100644 index 19454e0..0000000 --- a/examples/swaptest.qsm +++ /dev/null @@ -1,9 +0,0 @@ -qreg q[3]; -u(1,2,3) q[1]; -u(1,2,3) q[2]; - -h q[0]; -cswap q[0], q[1], q[2]; -h q[0]; - -sample q[0]; diff --git a/examples/swaptest.txt b/examples/swaptest.txt new file mode 100644 index 0000000..19454e0 --- /dev/null +++ b/examples/swaptest.txt @@ -0,0 +1,9 @@ +qreg q[3]; +u(1,2,3) q[1]; +u(1,2,3) q[2]; + +h q[0]; +cswap q[0], q[1], q[2]; +h q[0]; + +sample q[0]; diff --git a/examples/teleportation.qsm b/examples/teleportation.qsm deleted file mode 100644 index 68ea158..0000000 --- a/examples/teleportation.qsm +++ /dev/null @@ -1,31 +0,0 @@ -qreg q[3]; -creg c[2]; - -//qubit to teleport -u(1.6,1.6,1.6) q[0]; -print q[0]; //show qubit to teleport - -//shared entangled qubit -h q[1]; -cx q[1], q[2]; - -//correlate qubit to teleport -cx q[0], q[1]; - -//weaken correlation -h q[0]; - -//measure qubits -measure q[0] -> c[0]; -measure q[1] -> c[1]; - -//rotate based on measurement results -if(c==1) z q[2]; -if(c==2) x q[2]; -if(c==3) x q[2]; -if(c==3) z q[2]; - -//show teleported qubit -reset q[0]; -reset q[1]; -print q[2]; diff --git a/examples/teleportation.txt b/examples/teleportation.txt new file mode 100644 index 0000000..68ea158 --- /dev/null +++ b/examples/teleportation.txt @@ -0,0 +1,31 @@ +qreg q[3]; +creg c[2]; + +//qubit to teleport +u(1.6,1.6,1.6) q[0]; +print q[0]; //show qubit to teleport + +//shared entangled qubit +h q[1]; +cx q[1], q[2]; + +//correlate qubit to teleport +cx q[0], q[1]; + +//weaken correlation +h q[0]; + +//measure qubits +measure q[0] -> c[0]; +measure q[1] -> c[1]; + +//rotate based on measurement results +if(c==1) z q[2]; +if(c==2) x q[2]; +if(c==3) x q[2]; +if(c==3) z q[2]; + +//show teleported qubit +reset q[0]; +reset q[1]; +print q[2]; 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