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)
{
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)
{
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
{
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