Sun Feb 11 07:54:20 PM EST 2024
authormiha-q <>
Mon, 12 Feb 2024 00:54:20 +0000 (19:54 -0500)
committermiha-q <>
Mon, 12 Feb 2024 00:54:20 +0000 (19:54 -0500)
src/QAnsel.c

index 9a7ec0c2575a046ba13d84c2e96a54f7432abbed..c8bdead3c283bfaea4a2c932f1afef42033b9f54 100644 (file)
@@ -7,7 +7,7 @@
 #include "display.c"
 #include "chacha20.c"
 #define QUBITS_MAX 11
-double HIDDEN_VARIABLE;
+uint8_t HIDDEN_VARIABLE = 0;
 
 typedef struct
 {
@@ -16,35 +16,20 @@ 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;
@@ -57,10 +42,17 @@ double qansel_rand()
        }
        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);
@@ -305,6 +297,8 @@ void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint8_t* re
 
        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);
@@ -454,6 +448,18 @@ void qansel_run(uint8_t qubitCount, uint8_t bitCount, QInstr* instr, uint8_t* re
                        }
                        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);
@@ -505,7 +511,8 @@ void process(int argc, char** argv)
 {
        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);
@@ -1147,6 +1154,18 @@ void process(int argc, char** argv)
                        //  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)
@@ -1193,7 +1212,8 @@ void process(int argc, char** argv)
        {
                if (strcmp(argv[1], "-h") == 0)
                {
-                       HIDDEN_VARIABLE = atof(argv[2]);
+                       seed = atof(argv[2]);
+                       qansel_rand_s(seed);
                }
        }
        else if (argc == 4)
@@ -1208,11 +1228,13 @@ void process(int argc, char** argv)
                }
                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);
                }
        }