#!/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
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
--- /dev/null
+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
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);
}
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);
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)
text[textLen++] = 0;
if (strlen(text) > 0)
{
+ printf(">%s<\n", text);
free(text);
fprintf(stderr, "QAnsel: Invalid trailing text");
exit(1);
{
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;
}
}
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
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;
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;
instr[instrLen].q0 = q0;
instr[instrLen++].q1 = q1;
}
- else if (strcmp(lines[i], "display") == 0)
+ /*else if (strcmp(lines[i], "display") == 0)
{
if (doDisplay)
{
break;
}
doDisplay = 1;
- }
+ }*/
else if (strcmp(lines[i], "print q") == 0)
{
if (qubitCount == 0xFF)
}
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)
{
HIDDEN_VARIABLE = atof(argv[2]);
}
}
- else if (argv == 4)
+ else if (argc == 4)
{
if (strcmp(argv[1], "-d") == 0)
{