#include "display.c"
#include "chacha20.c"
#define QUBITS_MAX 11
-double HIDDEN_VARIABLE;
+uint8_t HIDDEN_VARIABLE = 0;
typedef struct
{
double arg0, arg1, arg2;
} QInstr;
+double qansel_rand_s(float s)
+{
+ uint32_t tmp;
+ memcpy(&tmp, &s, sizeof(uint32_t));
+ srand(tmp);
+}
double qansel_rand_h()
{
- static uint32_t blockNumber = 0;
- uint8_t key[32];
- uint8_t nonce[12];
- uint64_t tmpVariable;
- for (uint8_t i = 0; i < 32; i++)
- {
- if (i % 8 == 0) memcpy(&tmpVariable, &HIDDEN_VARIABLE, sizeof(uint64_t));
- key[i] = tmpVariable & 0xFF;
- tmpVariable = tmpVariable >> 8;
- }
- for (uint8_t i = 0; i < 12; i++)
- {
- nonce[i] = 0;
- }
- uint8_t* block = foleo_chacha20(key, nonce, blockNumber++, 4);
- uint32_t num = 0;
- for (uint8_t i = 0; i < 4; i++)
- {
- num = (num << 8) | block[i];
- }
- free(block);
- return ((double)num) / ((double)UINT32_MAX);
+ return ((double)rand()) / ((double)RAND_MAX);
}
-
-double qansel_rand()
+double qansel_rand_t()
{
FILE* f = fopen("/dev/TrueRNG0", "r");
+ if (!f) f = fopen("/dev/random", "r");
if (f)
{
uint32_t num = 0;
}
else
{
+ HIDDEN_VARIABLE = 1;
return qansel_rand_h();
}
}
+
+double qansel_rand()
+{
+ return HIDDEN_VARIABLE ? qansel_rand_h() : qansel_rand_t();
+}
+
void qansel_cnot(cpx_mtx_t* stateVector, uint8_t qubitCount, uint8_t bitA, uint8_t bitB)
{
uint32_t retLen = (uint32_t)pow(2, qubitCount);
for (uint32_t i = 0; i < instrLen; i++)
{
+ cpx_t qqq;
+ cpx_mtx_get(&stateVector, 0, 0, &qqq);
if (strcmp(instr[i].n, "measure") == 0)
{
bitVector[instr[i].q1] = qansel_measure(&stateVector, qubitCount, instr[i].q0);
}
printf("%.00f%%\n", prob * 100.0);
}
+ else if (strcmp(instr[i].n, "hiddenvar") == 0)
+ {
+ HIDDEN_VARIABLE = 1;
+ float tmp1 = (float)instr[i].arg0;
+ uint32_t tmp2;
+ memcpy(&tmp2, &tmp1, sizeof(uint32_t));
+ srand(tmp2);
+ }
+ else if (strcmp(instr[i].n, "truerand") == 0)
+ {
+ HIDDEN_VARIABLE = 0;
+ }
else if (strcmp(instr[i].n, "reset_all") == 0)
{
cpx_mtx_set2(&stateVector, 0, 0, 1, 0);
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
- HIDDEN_VARIABLE = (double)((uint64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec);
+ double seed = (double)((uint64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec);
+ qansel_rand_s(seed);
char** lines = malloc(0);
uint32_t* lineIDs = malloc(0);
char* text = malloc(0);
// optimizations that this instruction
// would prevent
}
+ else if (sscanf(lines[i], "hiddenvar %f", &a0) == 1)
+ {
+ instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
+ strcpy(instr[instrLen].n, "hiddenvar");
+ instr[instrLen++].arg0 = a0;
+ }
+ else if (strcmp(lines[i], "truerand") == 0)
+ {
+ instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
+ strcpy(instr[instrLen].n, "truerand");
+ instrLen++;
+ }
else if (strcmp(lines[i], "fullsample") == 0)
{
if (i != linesLen - 1)
{
if (strcmp(argv[1], "-h") == 0)
{
- HIDDEN_VARIABLE = atof(argv[2]);
+ seed = atof(argv[2]);
+ qansel_rand_s(seed);
}
}
else if (argc == 4)
}
if (strcmp(argv[1], "-h") == 0)
{
- HIDDEN_VARIABLE = atof(argv[2]);
+ seed = atof(argv[2]);
+ qansel_rand_s(seed);
}
else if (strcmp(argv[2], "-h") == 0)
{
- HIDDEN_VARIABLE = atof(argv[3]);
+ seed = atof(argv[3]);
+ qansel_rand_s(seed);
}
}