From: miha-q <> Date: Mon, 12 Feb 2024 02:37:18 +0000 (-0500) Subject: Sun Feb 11 09:37:18 PM EST 2024 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=21570ac121cf958dea3f464754e099abe29752f9;p=QAnsel.git Sun Feb 11 09:37:18 PM EST 2024 --- diff --git a/examples/belltest_c.txt b/examples/belltest_c.txt index fd0282f..8218c24 100644 --- a/examples/belltest_c.txt +++ b/examples/belltest_c.txt @@ -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 diff --git a/examples/belltest_q.txt b/examples/belltest_q.txt index f869569..8dab919 100644 --- a/examples/belltest_q.txt +++ b/examples/belltest_q.txt @@ -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 diff --git a/src/QAnsel.c b/src/QAnsel.c index 95d0cfc..f757aa8 100644 --- a/src/QAnsel.c +++ b/src/QAnsel.c @@ -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