]> foleosoft.com Git - QAnsel.git/commitdiff
Sun Feb 11 09:37:18 PM EST 2024
authormiha-q <>
Mon, 12 Feb 2024 02:37:18 +0000 (21:37 -0500)
committermiha-q <>
Mon, 12 Feb 2024 02:37:18 +0000 (21:37 -0500)
examples/belltest_c.txt
examples/belltest_q.txt
src/QAnsel.c

index fd0282f78d096e7e100a51a678ef08852ac4c2ee..8218c240b3598d043d97a235f616ec23cdf094f0 100644 (file)
@@ -51,10 +51,6 @@ cx q[0], q[1];
 x q[1];
 barrier q;
 
-//Store final results
+//Sample results
 measure q[1] -> c[0];
-measure q[1] -> c[1];
-measure q[1] -> c[2];
-measure q[1] -> c[3];
-
-sample;
\ No newline at end of file
+sample c[0];
\ No newline at end of file
index f8695695b91449b965e69dbd8ca896306b5ccb70..8dab919f6be6bc7a59480f3e7825467bf0819c3f 100644 (file)
@@ -63,8 +63,5 @@ 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];
 
-sample;
\ No newline at end of file
+sample c[0];
\ No newline at end of file
index 95d0cfc3c68d04d330af73caa9b94e3ac6cb901e..f757aa8df52e0182e16ec81f67300740e7159c74 100644 (file)
@@ -446,7 +446,8 @@ void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint8_t* re
                                        prob += cpx_magsqr(&n);
                                }
                        }
-                       printf("%.00f%%\n", prob * 100.0);
+                       printf("0: %.00f%%\n", (1 - prob) * 100.0);
+                       printf("1: %.00f%%\n", prob * 100.0);
                }
                else if (strcmp(instr[i].n, "hvar") == 0)
                {
@@ -1166,7 +1167,7 @@ void process(int argc, char** argv)
                        strcpy(instr[instrLen].n, "rand");
                        instrLen++;
                }
-               else if (strcmp(lines[i], "sample") == 0)
+               else if (strcmp(lines[i], "sample") == 0 || strcmp(lines[i], "sample c") == 0)
                {
                        if (i != linesLen - 1)
                        {
@@ -1180,7 +1181,23 @@ void process(int argc, char** argv)
                                errFound = 1;
                                break;
                        }
-                       fullSample = 1;
+                       fullSample = 255;
+               }
+               else if (sscanf(lines[i], "sample c[%i]", &q0) == 1)
+               {
+                       if (i != linesLen - 1)
+                       {
+                               fprintf(stderr, "QAnsel: Sampling should be performed at the end of the program;");
+                               errFound = 1;
+                               break;
+                       }
+                       if (bitCount == 0xFF)
+                       {
+                               fprintf(stderr, "QAnsel: Sampling cannot be used without initializing classical bits; ");
+                               errFound = 1;
+                               break;
+                       }
+                       fullSample = q0 + 1;
                }
                else
                {
@@ -1263,15 +1280,33 @@ void process(int argc, char** argv)
                        stats[stat]++;
                }
                free(dat);
+               uint32_t count = 0;
                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));
+                               uint8_t bit = (tmp >> (bitCount - 1) & 1);
+                               if (j == fullSample - 1 && bit)
+                               {
+                                       count += stats[i];
+                               }
+                               if (fullSample == 255)
+                               {
+                                       putchar('0' + bit);
+                               }
                                tmp <<= 1;
                        }
-                       printf(": %.00f%%\n", ((double)stats[i] / (double)shots) * (double)100);
+                       if (fullSample == 255)
+                       {
+                               printf(": %.00f%%\n", ((double)stats[i] / (double)shots) * (double)100);
+                       }
+               }
+               if (fullSample != 255)
+               {
+                       double prob = ((double)count / (double)shots) * (double)100;
+                       printf("0: %.00f%%\n", ((double)100)-prob);
+                       printf("1: %.00f%%\n", prob);
                }
        }
        else