+++ /dev/null
-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
--- /dev/null
+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
+++ /dev/null
-qreg q[2];
-h q[0];
-cx q[0], q[1];
-
-sample;
--- /dev/null
+//entanglement
+qreg q[2];
+h q[0];
+cx q[0], q[1];
+sample;
+++ /dev/null
-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;
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))
--- /dev/null
+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
--- /dev/null
+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
+++ /dev/null
-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
+++ /dev/null
-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
--- /dev/null
+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
+++ /dev/null
-qreg q[1];
-h q[0];
-rz($(rand)) q[0];
-density q[0];
\ No newline at end of file
--- /dev/null
+//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;
+++ /dev/null
-qreg q[1];
-creg c[1];
-h q[0];
-measure q[0] -> c[0];
-print c;
-
--- /dev/null
+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;
+
+++ /dev/null
-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
--- /dev/null
+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;
--- /dev/null
+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;
+
--- /dev/null
+#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
+++ /dev/null
-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];
--- /dev/null
+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];
+++ /dev/null
-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];
--- /dev/null
+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];
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];
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)
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);
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)
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);
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)
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)
{
// 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");
}
}
- 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