]> foleosoft.com Git - QAnsel.git/commitdiff
Fri 06 Dec 2024 07:14:11 PM EST
authora <[email protected]>
Sat, 7 Dec 2024 00:14:11 +0000 (19:14 -0500)
committera <[email protected]>
Sat, 7 Dec 2024 00:14:11 +0000 (19:14 -0500)
src/bytecode.c
src/context.c
src/context.h
src/imports/istina-editor.css
src/openqasm.c

index 0e6918de1fdad0ba9d8d26a227169730fceeff9e..ac7a6700554153b8224bd561048287b97aeb5092 100644 (file)
@@ -456,8 +456,18 @@ unsigned char qansel_measure(QAnselContext* ctx, cpx_mtx_t* stateVector, unsigne
                cpx_mtx_get(stateVector, 0, i, &n);
                if (bit == 0) prob0 += cpx_magsqr(&n);
        }
-
        float r = (ctx->hidden_variable) ? qansel_rand_h() : qansel_rand_t(ctx);
+       //add any error skew if desired
+       if (ctx->noise > 0)
+       {
+               float probtot;
+               float prob1 = 1 - prob0;
+               prob0 += ctx->noise;
+               prob1 += ctx->noise;
+               probtot = prob0 + prob1;
+               prob0 /= probtot;
+               prob1 /= probtot;
+       }
        unsigned char newBit = r < prob0 ? 0 : 1;
        float probTot = 0;
        for (unsigned int i = 0; i < qubitCountPow2; i++)
@@ -710,7 +720,27 @@ void qansel_born(QAnselContext* ctx, cpx_mtx_t* stateVector, int PC, int qubitCo
                float *psisquared = malloc(sizeof(float) * qubitCountPow2);
                for (unsigned int j = 0; j < qubitCountPow2; j++)
                {
-                       if (mode == 0)
+                       cpx_t n;
+                       cpx_mtx_get(stateVector, 0, j, &n);
+                       psisquared[j] = cpx_magsqr(&n);
+               }
+               //add any error skew if desired
+               if (ctx->noise > 0)
+               {
+                       float totalprob = 0;
+                       for (unsigned int j = 0; j < qubitCountPow2; j++)
+                       {
+                               psisquared[j] += ctx->noise;
+                               totalprob += psisquared[j];
+                       }
+                       for (unsigned int j = 0; j < qubitCountPow2; j++)
+                       {
+                               psisquared[j] /= totalprob;
+                       }
+               }
+               if (mode == 0)
+               {
+                       for (unsigned int j = 0; j < qubitCountPow2; j++)
                        {
                                unsigned int tmp = j;
                                for (unsigned char k = 0; k < qubitCount; k++)
@@ -718,17 +748,8 @@ void qansel_born(QAnselContext* ctx, cpx_mtx_t* stateVector, int PC, int qubitCo
                                        putchar('0' + (tmp >> (qubitCount - 1) & 1));
                                        tmp <<= 1;
                                }
-                       }
-                       cpx_t n;
-                       cpx_mtx_get(stateVector, 0, j, &n);
-                       psisquared[j] = cpx_magsqr(&n);
-                       if (mode == 0)
-                       {
                                printf("\t%.2f%%\n", psisquared[j] * 100);
                        }
-               }
-               if (mode == 0)
-               {
                        free(psisquared);
                        return;
                }
index ab8bb645b7a90d3bd1d0a962d72d5d87042225e6..78333d093036cd20e988e85e4d4f20ec4c25eadb 100644 (file)
@@ -43,6 +43,7 @@ int qanselContextValidate(QAnselContext* ctx)
 
 int qanselContextBegin(QAnselContext* ctx)
 {
+       ctx->noise = 0;
        ctx->chunk = NULL;
        if (ctx->memory_limit == 0) ctx->memory_limit = QANSEL_QUBITS_MAX;
        if (ctx->memory_limit > QANSEL_QUBITS_MAX)
index 40a4626e2d9a9b869c914a94b7d7a24a925fb694..4c42b29680578c020c85a9a5e7b1e0738fd1b296 100644 (file)
@@ -14,6 +14,7 @@ typedef struct
        unsigned char optimization_level;
        unsigned char sampling_bit;
        float hidden_variable;
+       float noise;
        int display_delay;
        int sampling_shots;
        int bsampling_shots;
index 0b59f37748c638f550fb2b0aabfd690927d7af3d..1b27f37e8caa08160cce488f1c1b671a6ad36e7f 100644 (file)
@@ -5,6 +5,7 @@
        position: absolute;
        font-family: monospace;
        color: rgba(255, 255, 255, 0.5);
+       font-weight: bold;
        width: 100%;
        background-color: rgba(0, 0, 0, 0);
        overflow-y: auto;
index 3366d777f30a68eb393cffbe252b4924b8f86787..10d725d9e0839240dd181aeb09e19007252c2459 100644 (file)
@@ -667,6 +667,7 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        memcpy(tmp, chunk + strbeg, strlen);
                        tmp[strlen] = 0;
                        if (strcmp(tmp, "hvar") == 0) instr = 1;
+                       if (strcmp(tmp, "noise") == 0) instr = 2;
                }
                {
                        int strbeg = regmatches[rmp].rm_so;
@@ -684,6 +685,7 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                switch (instr)
                {
                        case 1: ctx->hidden_variable = d0; break;
+                       case 2: ctx->noise = d0; break;
                }
        }
        else if (index == 12) //lone instructions
@@ -732,7 +734,7 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsign
                "^[ ]*(cx|cnot|swap)[ ]*q[ ]*\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*,[ ]*q[ ]*\\[[ ]*([0-9]*)[ ]*\\][ ]*$",
                "^[ ]*(ccx|toffoli|cswap|fredkin)[ ]*q[ ]*\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*,[ ]*q[ ]*\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*,[ ]*q[ ]*\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*$",
                "^[ ]*if[ ]*\\([ ]*c[ ]*(\\[[ ]*([0-9][0-9]*)[ ]*\\]|)[ ]*(==|!=|<>|>=|<=|>|<)[ ]*([0-9][0-9]*)[ ]*\\).*[ ]*$",
-               "^[ ]*(hvar)[ ]*([-/0-9PI.]*)[ ]*$",
+               "^[ ]*(hvar|noise)[ ][ ]*([-/0-9PI.][-/0-9PI.]*)[ ]*$",
                "^[ ]*(rand|exit)[ ]*$",
        };
        int ret, status = 0, found;