From: miha-q <> Date: Wed, 7 Feb 2024 04:10:47 +0000 (-0500) Subject: Tue Feb 6 11:10:47 PM EST 2024 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=a15e632647efd5f5d7353a7eeb2065c4218b58ff;p=QAnsel.git Tue Feb 6 11:10:47 PM EST 2024 --- diff --git a/bin/QAnsel b/bin/QAnsel index b479cbb..da2cdeb 100755 Binary files a/bin/QAnsel and b/bin/QAnsel differ diff --git a/examples/belltest.qsm b/examples/belltest.qsm new file mode 100644 index 0000000..4a028c3 --- /dev/null +++ b/examples/belltest.qsm @@ -0,0 +1,188 @@ +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 788d048..a507e0a 100644 --- a/examples/belltest.sh +++ b/examples/belltest.sh @@ -1,93 +1,5 @@ #!/bin/bash -function classic { -../bin/QAnsel << HERE -qreg q[2]; -creg c[2]; - -h q[0]; -h q[1]; -measure q[0] -> c[0]; -measure q[1] -> c[1]; -x q[0]; -if(c[1]==1) x q[1]; -print c; - -//Player X rules -if(c[0]==1) x q[0]; - -//Player Y rules -x q[1]; - -//Results -measure q[0] -> c[0]; -measure q[1] -> c[1]; -print c; - -HERE -} - -function quantum { -../bin/QAnsel << HERE -qreg q[2]; -creg c[2]; - -h q[0]; -h q[1]; -measure q[0] -> c[0]; -measure q[1] -> c[1]; -print c; -reset q[0]; -reset q[1]; - -h q[0]; -cx q[0], q[1]; - -//Player X rules -if(c[0]==1) ry(pi/2) q[0]; - -//Player Y rules -if(c[1]==0) ry(pi/4) q[1]; -if(c[1]==1) ry(-pi/4) q[1]; - -measure q[0] -> c[0]; -measure q[1] -> c[1]; -print c; - -HERE -} - -function test -{ - if [ "$1" == "00 00" ] - then - echo 1 - elif [ "$1" == "00 11" ] - then - echo 1 - elif [ "$1" == "01 00" ] - then - echo 1 - elif [ "$1" == "01 11" ] - then - echo 1 - elif [ "$1" == "10 00" ] - then - echo 1 - elif [ "$1" == "10 11" ] - then - echo 1 - elif [ "$1" == "11 10" ] - then - echo 1 - elif [ "$1" == "11 01" ] - then - echo 1 - else - echo 0 - fi -} - trials=1000 cwins=0 qwins=0 @@ -96,13 +8,17 @@ do clear echo "$(( (100 * i) / trials ))%" - trial=$(classic | xargs) - results=$(test "$trial") - cwins=$((cwins+results)) - - trial=$(quantum | xargs) - results=$(test "$trial") - qwins=$((qwins+results)) + results="$(../bin/QAnsel < belltest.qsm | xargs)" + resultsQ="$(echo "$results" | sed 's/ .*//')" + resultsC="$(echo "$results" | sed 's/.* //')" + if [ "$resultsQ" == "1111" ] + then + qwins=$((qwins+1)) + fi + if [ "$resultsC" == "1111" ] + then + cwins=$((cwins+1)) + fi done clear diff --git a/examples/big.qsm b/examples/big.qsm index 583730e..73b7c35 100644 --- a/examples/big.qsm +++ b/examples/big.qsm @@ -1,52 +1,32 @@ qreg q[2]; -creg c[2]; - -h q[0]; -cx q[0], q[1]; - -//00 -h q[0]; -cx q[0], q[1]; - -ry(pi/4) q[1]; -rz(pi) q[1]; - -sample; -reset; - -//01 -h q[0]; -cx q[0], q[1]; - -ry(pi/2) q[0]; -rz(pi) q[0]; - -ry(pi/4) q[1]; -rz(pi) q[1]; - -sample; -reset; //10 -h q[0]; -cx q[0], q[1]; +x q[0]; -ry(-pi/4) q[1]; -rz(pi) q[1]; +print q[0]; +print q[1]; -sample; -reset; - -//11 -h q[0]; +//beam splitter cx q[0], q[1]; - -ry(pi/2) q[0]; -rz(pi) q[0]; - -ry(-pi/4) q[1]; -rz(pi) q[1]; - -sample; -reset; - +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 new file mode 100644 index 0000000..fb71f5c --- /dev/null +++ b/examples/bombtester.qsm @@ -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 new file mode 100644 index 0000000..510fc71 --- /dev/null +++ b/examples/decoherence.qsm @@ -0,0 +1,4 @@ +qreg q[1]; +h q[0]; +rz($(rand)) q[0]; +density q[0]; \ No newline at end of file diff --git a/examples/decoherence.sh b/examples/decoherence.sh new file mode 100644 index 0000000..66f3d74 --- /dev/null +++ b/examples/decoherence.sh @@ -0,0 +1,46 @@ +function rand +{ + RAND_NUM_HEX=$(openssl rand 4 | od -A n -t x1) + RAND_NUM_DEC=$(( 16#${RAND_NUM_HEX// /} )) + MAX_4BYTE_VAL=4294967295 + echo "scale=10; 8*a(1)*$RAND_NUM_DEC/$MAX_4BYTE_VAL" | bc -l +} + +function kick +{ + ../bin/QAnsel << HERE + qreg q[1]; + h q[0]; + u($(rand),$(rand),$(rand)) q[0]; + density q[0]; +HERE +} + + +tota="0" +totb="0" +totc="0" +totd="0" + +for i in {1..1000} +do + sample=$(kick | xargs) + a=$(echo $sample | sed 's/,.*//' | sed 's/.* //' | sed 's/i/j/') + b=$(echo $sample | sed 's/\] \[.*//' | sed 's/.*, //' | sed 's/i/j/') + c=$(echo $sample | sed 's/.*\] \[ //' | sed 's/,.*//' | sed 's/i/j/') + d=$(echo $sample | sed 's/.*, //' | sed 's/ .*//' | sed 's/i/j/') + + tota=$(python -c "print(($tota) + ($a))" | sed 's/[()]//g') + totb=$(python -c "print(($totb) + ($b))" | sed 's/[()]//g') + totc=$(python -c "print(($totc) + ($c))" | sed 's/[()]//g') + totd=$(python -c "print(($totd) + ($d))" | sed 's/[()]//g') + + outa=$(python -c "print(($tota) / ($i))" | sed 's/[()]//g' | sed 's/j/i/') + outb=$(python -c "print(($totb) / ($i))" | sed 's/[()]//g' | sed 's/j/i/') + outc=$(python -c "print(($totc) / ($i))" | sed 's/[()]//g' | sed 's/j/i/') + outd=$(python -c "print(($totd) / ($i))" | sed 's/[()]//g' | sed 's/j/i/') + + echo "[ $outa, $outb ]" + echo "[ $outc, $outd ]" + echo '---------' +done \ No newline at end of file diff --git a/src/QAnsel.c b/src/QAnsel.c index 30337ca..dd55b81 100644 --- a/src/QAnsel.c +++ b/src/QAnsel.c @@ -285,6 +285,7 @@ void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint32_t in for (uint8_t i = 0; i < bitCount; i++) bitVector[i] = 0; for (uint32_t i = 0; i < instrLen; i++) { + printf(">%s<\n", instr[i].n); if (strcmp(instr[i].n, "measure") == 0) { bitVector[instr[i].q1] = qansel_measure(&stateVector, qubitCount, instr[i].q0); @@ -330,7 +331,12 @@ void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint32_t in } putchar('\n'); } - else if (strcmp(instr[i].n, "printq") == 0) + else if (strcmp(instr[i].n, "printc") == 0) + { + putchar('0' + bitVector[instr[i].q0]); + putchar('\n'); + } + else if (strcmp(instr[i].n, "printq") == 0 || strcmp(instr[i].n, "density") == 0) { cpx_mtx_t tmp; cpx_mtx_init(&tmp, 1, 2); @@ -370,7 +376,34 @@ void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint32_t in n.imaginary *= multiplier; cpx_mtx_set(&tmp, 0, 0, &n); - printf("[ "); cpx_mtx_print(&tmp); printf(" ]\n"); + if (strcmp(instr[i].n, "density") == 0) + { + cpx_t a, b, c, d, x, y, z, w; + cpx_mtx_get(&tmp, 0, 0, &a); + cpx_mtx_get(&tmp, 0, 1, &b); + cpx_mtx_get(&tmp, 0, 0, &c); + cpx_mtx_get(&tmp, 0, 1, &d); + c.imaginary *= -1; + d.imaginary *= -1; + cpx_mul(&x, &a, &c); + cpx_mul(&y, &a, &d); + cpx_mul(&z, &b, &c); + cpx_mul(&w, &b, &d); + char* sx = cpx_str(&x); + char* sy = cpx_str(&y); + char* sz = cpx_str(&z); + char* sw = cpx_str(&w); + printf("[ %s, %s ]\n", sx, sy); + printf("[ %s, %s ]\n", sz, sw); + free(sx); + free(sy); + free(sz); + free(sw); + } + else + { + printf("[ "); cpx_mtx_print(&tmp); printf(" ]\n"); + } cpx_mtx_free(&tmp); } else if (strcmp(instr[i].n, "sample_all") == 0) @@ -516,6 +549,7 @@ void main(int argc, char** argv) text[textLen++] = 0; if (strlen(text) > 0) { + printf(">%s<\n", text); free(text); fprintf(stderr, "QAnsel: Invalid trailing text"); exit(1); @@ -665,7 +699,7 @@ void main(int argc, char** argv) { angle = -M_PI; } - else if (sscanf(lines[i], "r%c(%f) q[%i]", &ty, &a0, &q0) == 2) + else if (sscanf(lines[i], "r%c(%f) q[%i]", &ty, &a0, &q0) == 3) { angle = a0; } @@ -712,6 +746,25 @@ void main(int argc, char** argv) } instrLen++; } + else if (sscanf(lines[i], "density q[%i]", &q0) == 1) + { + g = lines[i][0]; + if (qubitCount == 0xFF) + { + fprintf(stderr, "QAnsel: Instruction before initialization"); + errFound = 1; + break; + } + if (q0 < 0 || q0 >= qubitCount) + { + fprintf(stderr, "QAnsel: Invalid index"); + errFound = 1; + break; + } + instr = realloc(instr, (instrLen + 1) * sizeof(QInstr)); + strcpy(instr[instrLen].n, "density"); + instr[instrLen++].q0 = (uint8_t)q0; + } else if ( sscanf(lines[i], "h q[%i]", &q0) == 1 @@ -748,7 +801,7 @@ void main(int argc, char** argv) errFound = 1; break; } - if (q0 > qubitCount || q1 > qubitCount | q0 < 0 || q1 < 0 || q0 == q1) + if (q0 >= qubitCount || q1 >= qubitCount | q0 < 0 || q1 < 0 || q0 == q1) { fprintf(stderr, "QAnsel: Invalid index"); errFound = 1; @@ -767,7 +820,7 @@ void main(int argc, char** argv) errFound = 1; break; } - if (q0 > qubitCount || q1 > qubitCount | q0 < 0 || q1 < 0 || q0 == q1) + if (q0 >= qubitCount || q1 >= qubitCount | q0 < 0 || q1 < 0 || q0 == q1) { fprintf(stderr, "QAnsel: Invalid index"); errFound = 1; @@ -894,7 +947,7 @@ void main(int argc, char** argv) instr[instrLen].q0 = q0; instr[instrLen++].q1 = q1; } - else if (strcmp(lines[i], "display") == 0) + /*else if (strcmp(lines[i], "display") == 0) { if (doDisplay) { @@ -903,7 +956,7 @@ void main(int argc, char** argv) break; } doDisplay = 1; - } + }*/ else if (strcmp(lines[i], "print q") == 0) { if (qubitCount == 0xFF) @@ -962,7 +1015,7 @@ void main(int argc, char** argv) } instr = realloc(instr, (instrLen + 1) * sizeof(QInstr)); strcpy(instr[instrLen].n, "printc"); - instr[instrLen++].q0 = q0; + instr[instrLen++].q0 = q0; } else if (strcmp(lines[i], "sample") == 0) { @@ -1099,7 +1152,7 @@ void main(int argc, char** argv) HIDDEN_VARIABLE = atof(argv[2]); } } - else if (argv == 4) + else if (argc == 4) { if (strcmp(argv[1], "-d") == 0) {