From: a Date: Sat, 7 Dec 2024 00:14:11 +0000 (-0500) Subject: Fri 06 Dec 2024 07:14:11 PM EST X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=1ceaea6921bd25080debf0fa252ed1b5bbbee239;p=QAnsel.git Fri 06 Dec 2024 07:14:11 PM EST --- diff --git a/src/bytecode.c b/src/bytecode.c index 0e6918d..ac7a670 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -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; } diff --git a/src/context.c b/src/context.c index ab8bb64..78333d0 100644 --- a/src/context.c +++ b/src/context.c @@ -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) diff --git a/src/context.h b/src/context.h index 40a4626..4c42b29 100644 --- a/src/context.h +++ b/src/context.h @@ -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; diff --git a/src/imports/istina-editor.css b/src/imports/istina-editor.css index 0b59f37..1b27f37 100644 --- a/src/imports/istina-editor.css +++ b/src/imports/istina-editor.css @@ -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; diff --git a/src/openqasm.c b/src/openqasm.c index 3366d77..10d725d 100644 --- a/src/openqasm.c +++ b/src/openqasm.c @@ -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;