]> foleosoft.com Git - QAnsel.git/commitdiff
Sat 07 Dec 2024 07:30:19 PM EST
authora <[email protected]>
Sun, 8 Dec 2024 00:30:19 +0000 (19:30 -0500)
committera <[email protected]>
Sun, 8 Dec 2024 00:30:19 +0000 (19:30 -0500)
src/imports/istina-editor.js
src/index.html
src/openqasm.c
src/openqasm.h

index 6159c692dda4066950c0ac2b4e90820b15cf4d82..b1f8d67c7cf61bafa7f6acc6d72dd44330c37980 100644 (file)
@@ -247,13 +247,14 @@ IstinaEditor.highlightForQAnsel = function(txt)
                "barrier",
                "hvar",
                "noise",
+               "shots",
                "rand",
                "born"
        ];
                
        for (var i = 0; i < keywords.length; i++)
        {
-               if (keywords[i] == "hvar" || keywords[i] == "noise") continue;
+               if (keywords[i] == "hvar" || keywords[i] == "noise" || keywords[i] == "shots") continue;
                var tmp = new RegExp("(^|[ ]|[;]|[>])" + keywords[i] + "($|[ ]|[<]|[&])", "g");
                txt = txt.replaceAll(tmp, x =>
                {
@@ -290,7 +291,7 @@ IstinaEditor.highlightForQAnsel = function(txt)
        );
        txt = txt.replaceAll
        (
-               /(noise|hvar)( |&nbsp;)[.\-0-9][.\-0-9]*/g,
+               /(noise|hvar|shots)( |&nbsp;)[.\-0-9][.\-0-9]*/g,
                x =>
                {
                        x = x.replace(" ", "&nbsp;");
index 3e455284fd260c245b4a36fa9e01cd55b252bca0..075d0b9b4dc488d6e212290d1d1fc7e3a6cd8906 100644 (file)
        <table border=1 class="program-instr" oncontextmenu="return false;" style="display: none;" cellspacing="0" cellpadding="0"></table>
        <table border=1 class="program-drag" oncontextmenu="return false;" style="pointer-events: none; touch-action: none; visibility: hidden;"></table>
        <script>
+               var shotsmax = 10000;
                var ismobile = typeof(document.body.ontouchstart) == "object";
                $ = x => { return document.querySelectorAll(x); };
                if (window.location.href.includes("apikey="))
                                                        }
                                                }
                                        }
+                                       ret = "shots " + localStorage.getItem("shots") + ";\n" + ret;
                                }
                                return ret;
                        },
                window.onload = function()
                {
                        if (localStorage.getItem("noise") == null)
-                               localStorage.setItem("noise", 0);                               
+                               localStorage.setItem("noise", 0);
                        if (localStorage.getItem("hvar") == null)
-                               localStorage.setItem("hvar", "");                               
+                               localStorage.setItem("hvar", "");
+                       if (localStorage.getItem("shots") == null)
+                               localStorage.setItem("shots", shotsmax);
                        ProgramTrack.draw();
                        //$(".program-track")[0].parentNode.style.height = Math.round($(".program-instr")[0].getBoundingClientRect().height) + "px";
                        $(".program-track")[0].parentNode.style.width = "100%";
                                        $("#modal-main-content")[0].innerHTML = `
                                                <p>API key: </p><input type='text' id='apikey' />
                                                <br />
+                                               <p>Shots: <p/><input type='number' id='shots' step='1' />
+                                               <br />
                                                <p>Noise: <p/><input type='number' id='noise' step='0.01' />
                                                <br />
                                                <p>Hidden variable: <p/><input type='number' id='hvar' step='0.01' />
                                        $("#apikey")[0].style.width = "100%";
                                        $("#noise")[0].style.width = "100%";
                                        $("#hvar")[0].style.width = "100%";
+                                       $("#shots")[0].style.width = "100%";
                                        $("#apikey")[0].addEventListener("click", () =>
                                        {
                                                $("#apikey")[0].selectionStart = 0;
                                        $("#apikey")[0].addEventListener("keydown", e => { if (e.key == "Enter") { $("#modal-main-confirm")[0].click(); } })
                                        $("#noise")[0].addEventListener("keydown", e => { if (e.key == "Enter") { $("#modal-main-confirm")[0].click(); } });
                                        $("#hvar")[0].addEventListener("keydown", e => { if (e.key == "Enter") { $("#modal-main-confirm")[0].click(); } });
+                                       $("#shots")[0].addEventListener("keydown", e => { if (e.key == "Enter") { $("#modal-main-confirm")[0].click(); } });
                                        var noiseChange = () =>
                                        {
                                                var v = parseFloat($("#noise")[0].value);
                                                if (isNaN(v)) v = "";
                                                $("#hvar")[0].value = v;
                                        }
+                                       var shotsChange = () =>
+                                       {
+                                               var v = parseInt($("#shots")[0].value);
+                                               if (isNaN(v)) v = shotsmax;
+                                               if (v <= 0) v = 1;
+                                               if (v > shotsmax) v = shotsmax;
+                                               $("#shots")[0].value = v;
+                                       };
                                        var changeEvents = ["change", "keydown", "keyup", "mouseenter", "mouseleave"];
                                        for (var i = 0; i < changeEvents.length; i++)
                                        {
                                                $("#noise")[0].addEventListener(changeEvents[i], e => { noiseChange(); });
                                                $("#hvar")[0].addEventListener(changeEvents[i], e => { hvarChange(); });
+                                               $("#shots")[0].addEventListener(changeEvents[i], e => { shotsChange(); });
                                        }
                                        $("#apikey")[0].value = apikey;
                                        $("#noise")[0].value = localStorage.getItem("noise");
                                        $("#hvar")[0].value = localStorage.getItem("hvar");
+                                       $("#shots")[0].value = localStorage.getItem("shots");
                                        $("#modal-main-confirm")[0].style.display = "";
                                        $("#modal-main-cancel")[0].style.display = "";
                                        $("#modal-main-confirm")[0].setAttribute("onclick",
                                                localStorage.setItem('apikey', $('#apikey')[0].value);
                                                localStorage.setItem('noise', $('#noise')[0].value);
                                                localStorage.setItem('hvar', $('#hvar')[0].value);
+                                               localStorage.setItem('shots', $('#shots')[0].value);
                                                closeModal();
                                                ProgramTrack.draw();
                                        `);
index 4ff2adb563529b21aa389bf38e43632b68381188..d57fc33d418e89c87e10473c0b21c79fdbc7a302 100644 (file)
@@ -203,7 +203,7 @@ int qansel_parse_float(char* str, float* returnFloat)
        return 1;
 }
 
-int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatches, int* qubitCount, int* bitCount, unsigned char** binary, int* binarySize, QAnselContext* ctx, int samplingshots)
+int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatches, int* qubitCount, int* bitCount, unsigned char** binary, int* binarySize, QAnselContext* ctx, int* samplingshots)
 {
        unsigned short s0;
        float d0, d1, d2;
@@ -500,7 +500,7 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        if (a0 == QANSEL_ALL_CLASSIC) a0 = QANSEL_ALL;
                        else a0 = a0 - QANSEL_CBOUND_LOWER; 
                        ctx->sampling_bit = a0;
-                       ctx->sampling_shots = samplingshots;
+                       ctx->sampling_shots = *samplingshots;
                }
                else
                {
@@ -668,6 +668,7 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        tmp[strlen] = 0;
                        if (strcmp(tmp, "hvar") == 0) instr = 1;
                        if (strcmp(tmp, "noise") == 0) instr = 2;
+                       if (strcmp(tmp, "shots") == 0) instr = 3;
                }
                {
                        int strbeg = regmatches[rmp].rm_so;
@@ -686,6 +687,14 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                {
                        case 1: ctx->hidden_variable_set = 1; ctx->hidden_variable = d0; break;
                        case 2: ctx->noise = d0; break;
+                       case 3:
+                               if (d0 <= 0 || d0 > *samplingshots)
+                               {
+                                       fprintf(stderr, "QAnsel on line %i: Invalid number of shots.\n", line);
+                                       return 0;
+                               }
+                               *samplingshots = d0;
+                               break;
                }
        }
        else if (index == 12) //lone instructions
@@ -712,7 +721,7 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
        return 1;
 }
 
-int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsigned char** retBinary, int* retSize, QAnselContext* ctx, int samplingshots)
+int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsigned char** retBinary, int* retSize, QAnselContext* ctx, int* samplingshots)
 {
        unsigned char* binary = malloc(0);
        int binarySize = 0;
@@ -734,7 +743,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|noise)[ ][ ]*([-/0-9PI.][-/0-9PI.]*)[ ]*$",
+               "^[ ]*(hvar|noise|shots)[ ][ ]*([-/0-9PI.][-/0-9PI.]*)[ ]*$",
                "^[ ]*(rand|exit)[ ]*$",
        };
        int ret, status = 0, found;
@@ -816,7 +825,7 @@ int qanselBuildFromSource(char* osource, unsigned char** binary, int* binarySize
                fprintf(stderr, "QAnsel: Read script failure.\n");
                return 0;
        }
-       status = qansel_process_chunks(chunks, chunksAssociatedLines, chunksCount, binary, binarySize, ctx, samplingshots);
+       status = qansel_process_chunks(chunks, chunksAssociatedLines, chunksCount, binary, binarySize, ctx, &samplingshots);
        for (int i = 0; i < chunksCount; i++) free(chunks[i]);
        free(chunks);
        free(chunksAssociatedLines);
index 7bd666f3b5d9aa7a896fb81fcf909d22d2d699c2..beb77aba4cc94913b45f9c0881c48a96d26d0dd2 100644 (file)
@@ -11,8 +11,8 @@
 int qansel_read_script(char* script, char*** chunksReturn, int** chunkLinesReturn, int* countReturn);
 float qansel_parse_float_part(char* neg, char* str);
 int qansel_parse_float(char* str, float* returnFloat);
-int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatches, int* qubitCount, int* bitCount, unsigned char** binary, int* binarySize, QAnselContext* ctx, int samplingshots);
-int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsigned char** retBinary, int* retSize, QAnselContext* ctx, int samplingshots);
+int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatches, int* qubitCount, int* bitCount, unsigned char** binary, int* binarySize, QAnselContext* ctx, int* samplingshots);
+int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsigned char** retBinary, int* retSize, QAnselContext* ctx, int* samplingshots);
 int qanselBuildFromSource(char* source, unsigned char** binary, int* binarySize, QAnselContext* ctx);
 
 #endif