]> foleosoft.com Git - QAnsel.git/commitdiff
Fri Mar 8 11:54:53 PM EST 2024
authormiha-q <>
Sat, 9 Mar 2024 04:54:53 +0000 (23:54 -0500)
committermiha-q <>
Sat, 9 Mar 2024 04:54:53 +0000 (23:54 -0500)
examples/bombtester.txt
examples/decoherence.txt
examples/randombyte.txt
examples/slow.txt
src/QAnsel.c
src/core.c

index d40ffc5fb15a9751b336d32746702a4441acb376..e38178b896486211ea11a147a04c9828ce7f0e61 100644 (file)
@@ -56,4 +56,4 @@ measure q[1] -> c[1];
 //             where the bomb is or else it would have been measured,
 //             yet the observer can know for certainty the bomb is
 //             live without interacting with it (allegedly).
-sample;
+sample c;
index 2d59d957ebee51a879e09550d3af19f2d83320b4..c4ac0edd8a507d3f2713337cd20e7c9041c91a8e 100644 (file)
@@ -9,4 +9,4 @@ h q[0];
 cx q[0], q[1];
 h q[0];
 measure q[0] -> c[0];
-sample c;
+sample;
index e26b2cbae222746573f798435817df3bbec8d6aa..eb166129e26c752e3b90d3d97aaa1a41ceba213d 100644 (file)
@@ -9,4 +9,3 @@ h q[0]; measure q[0] -> c[5];
 h q[0]; measure q[0] -> c[6];
 h q[0]; measure q[0] -> c[7];
 print c;
-
index ecdb1f1badc822258e486e248920be443814e013..b01c9ad3c8ab8d968c1ae8feb19bf3320ecd54d0 100644 (file)
@@ -1,5 +1,5 @@
 //designed to be slow
-qreg q[11];
+qreg q[14];
 x q[0];
 x q[1];
 x q[2];
@@ -11,8 +11,8 @@ x q[7];
 x q[8];
 x q[9];
 x q[10];
-//x q[11];
-//x q[12];
-//x q[13];
+x q[11];
+x q[12];
+x q[13];
 
-//born;
\ No newline at end of file
+born q;
\ No newline at end of file
index 9a9ca9e44cbdbe0b87bc95b25ac181623360c09a..cb11e3a623d3b0015dab4f84fa01642b6bc67e2f 100644 (file)
@@ -199,7 +199,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)
+int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatches, int* qubitCount, int* bitCount, unsigned char** binary, int* binarySize)
 {
        unsigned short s0;
        float d0, d1, d2;
@@ -328,34 +328,34 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                switch (index)
                {
                        case 2:
-                               *binarySize += 2 + sizeof(float) * 0;
-                               binary = realloc(binary, (*binarySize));
-                               binary[(*binarySize) - 2] = instr;
-                               binary[(*binarySize) - 1] = a0;
+                               *binarySize += 2;
+                               *binary = realloc(*binary, (*binarySize));
+                               (*binary)[(*binarySize) - 2] = instr;
+                               (*binary)[(*binarySize) - 1] = a0;
                        break;
                        case 3:
                                *binarySize += 2 + sizeof(float) * 1;
-                               binary = realloc(binary, (*binarySize));
-                               binary[(*binarySize) - 2 - sizeof(float) * 1] = instr;
-                               binary[(*binarySize) - 1 - sizeof(float) * 1] = a0;
-                               memcpy(binary + ((*binarySize) - sizeof(float)), &d0, sizeof(float));
+                               *binary = realloc(*binary, (*binarySize));
+                               (*binary)[(*binarySize) - 2 - sizeof(float) * 1] = instr;
+                               (*binary)[(*binarySize) - 1 - sizeof(float) * 1] = a0;
+                               memcpy((*binary) + ((*binarySize) - sizeof(float)), &d0, sizeof(float));
                        break;
                        case 4:
                                *binarySize += 2 + sizeof(float) * 2;
-                               binary = realloc(binary, (*binarySize));
-                               binary[(*binarySize) - 2 - sizeof(float) * 2] = instr;
-                               binary[(*binarySize) - 1 - sizeof(float) * 2] = a0;
-                               memcpy(binary + ((*binarySize) - sizeof(float) * 2), &d0, sizeof(float));
-                               memcpy(binary + ((*binarySize) - sizeof(float) * 1), &d1, sizeof(float));
+                               *binary = realloc(*binary, (*binarySize));
+                               (*binary)[(*binarySize) - 2 - sizeof(float) * 2] = instr;
+                               (*binary)[(*binarySize) - 1 - sizeof(float) * 2] = a0;
+                               memcpy((*binary) + ((*binarySize) - sizeof(float) * 2), &d0, sizeof(float));
+                               memcpy((*binary) + ((*binarySize) - sizeof(float) * 1), &d1, sizeof(float));
                        break;
                        case 5:
                                *binarySize += 2 + sizeof(float) * 3;
-                               binary = realloc(binary, (*binarySize));
-                               binary[(*binarySize) - 2 - sizeof(float) * 3] = instr;
-                               binary[(*binarySize) - 1 - sizeof(float) * 3] = a0;
-                               memcpy(binary + ((*binarySize) - sizeof(float) * 3), &d0, sizeof(float));
-                               memcpy(binary + ((*binarySize) - sizeof(float) * 2), &d1, sizeof(float));
-                               memcpy(binary + ((*binarySize) - sizeof(float) * 1), &d2, sizeof(float));
+                               *binary = realloc(*binary, (*binarySize));
+                               (*binary)[(*binarySize) - 2 - sizeof(float) * 3] = instr;
+                               (*binary)[(*binarySize) - 1 - sizeof(float) * 3] = a0;
+                               memcpy((*binary) + ((*binarySize) - sizeof(float) * 3), &d0, sizeof(float));
+                               memcpy((*binary) + ((*binarySize) - sizeof(float) * 2), &d1, sizeof(float));
+                               memcpy((*binary) + ((*binarySize) - sizeof(float) * 1), &d2, sizeof(float));
                        break;
                }
        }
@@ -422,10 +422,10 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        }
                }
                *binarySize += 3;
-               binary = realloc(binary, (*binarySize));
-               binary[(*binarySize) - 3] = QANSEL_INSTRUCTION_MEASURE;
-               binary[(*binarySize) - 2] = a0;
-               binary[(*binarySize) - 1] = a1;
+               *binary = realloc(*binary, (*binarySize));
+               (*binary)[(*binarySize) - 3] = QANSEL_INSTRUCTION_MEASURE;
+               (*binary)[(*binarySize) - 2] = a0;
+               (*binary)[(*binarySize) - 1] = a1;
        }
        else if (index == 7) //classical bit instructions
        {
@@ -472,9 +472,9 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        }
                }
                *binarySize += 2;
-               binary = realloc(binary, (*binarySize));
-               binary[(*binarySize) - 2] = instr;
-               binary[(*binarySize) - 1] = a0;
+               *binary = realloc(*binary, (*binarySize));
+               (*binary)[(*binarySize) - 2] = instr;
+               (*binary)[(*binarySize) - 1] = a0;
        }
        else if (index == 8 || index == 9) //double qubit instructions
        {
@@ -492,7 +492,9 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        memcpy(tmp, chunk + strbeg, strlen);
                        tmp[strlen] = 0;
                        if (strcmp(tmp, "cx") == 0) instr = QANSEL_INSTRUCTION_CX;
-                       if (strcmp(tmp, "cnot") == 0) instr = QANSEL_INSTRUCTION_CCX;
+                       if (strcmp(tmp, "cnot") == 0) instr = QANSEL_INSTRUCTION_CX;
+                       if (strcmp(tmp, "ccnot") == 0) instr = QANSEL_INSTRUCTION_CCX;
+                       if (strcmp(tmp, "ccx") == 0) instr = QANSEL_INSTRUCTION_CCX;
                        else if (strcmp(tmp, "toffoli") == 0) instr = QANSEL_INSTRUCTION_CCX;
                        else if (strcmp(tmp, "cswap") == 0) instr = QANSEL_INSTRUCTION_CSWAP;
                        else if (strcmp(tmp, "fredkin") == 0) instr = QANSEL_INSTRUCTION_CSWAP;
@@ -521,19 +523,19 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                if (index == 8)
                {
                        *binarySize += 3;
-                       binary = realloc(binary, (*binarySize));
-                       binary[(*binarySize) - 3] = instr;
-                       binary[(*binarySize) - 2] = a0;
-                       binary[(*binarySize) - 1] = a1;
+                       *binary = realloc(*binary, (*binarySize));
+                       (*binary)[(*binarySize) - 3] = instr;
+                       (*binary)[(*binarySize) - 2] = a0;
+                       (*binary)[(*binarySize) - 1] = a1;
                }
                else
                {
                        *binarySize += 4;
-                       binary = realloc(binary, (*binarySize));
-                       binary[(*binarySize) - 4] = instr;
-                       binary[(*binarySize) - 3] = a0;
-                       binary[(*binarySize) - 2] = a1;
-                       binary[(*binarySize) - 1] = a2;
+                       *binary = realloc(*binary, (*binarySize));
+                       (*binary)[(*binarySize) - 4] = instr;
+                       (*binary)[(*binarySize) - 3] = a0;
+                       (*binary)[(*binarySize) - 2] = a1;
+                       (*binary)[(*binarySize) - 1] = a2;
                }
        }
        else if (index == 10) //if instructions
@@ -576,13 +578,32 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        char tmp[strlen + 1];
                        memcpy(tmp, chunk + strbeg, strlen);
                        tmp[strlen] = 0;
-                       if (strcmp(tmp, "==") == 0) instr = QANSEL_INSTRUCTION_IF_E;
-                       else if (strcmp(tmp, "!=") == 0) instr = QANSEL_INSTRUCTION_IF_NE;
-                       else if (strcmp(tmp, "<>") == 0) instr = QANSEL_INSTRUCTION_IF_NE;
-                       else if (strcmp(tmp, ">=") == 0) instr = QANSEL_INSTRUCTION_IF_GE;
-                       else if (strcmp(tmp, "<=") == 0) instr = QANSEL_INSTRUCTION_IF_LE;
-                       else if (strcmp(tmp, ">") == 0) instr = QANSEL_INSTRUCTION_IF_G;
-                       else if (strcmp(tmp, "<") == 0) instr = QANSEL_INSTRUCTION_IF_L;
+                       if (strlen == 0)
+                       {
+                               strbeg = regmatches[rmp].rm_so;
+                               strlen = regmatches[rmp].rm_eo - regmatches[rmp].rm_so;
+                               rmp++;
+                               char tmp[strlen + 1];
+                               memcpy(tmp, chunk + strbeg, strlen);
+                               tmp[strlen] = 0;
+                               if (strcmp(tmp, "==") == 0) instr = QANSEL_INSTRUCTION_IF_E;
+                               else if (strcmp(tmp, "!=") == 0) instr = QANSEL_INSTRUCTION_IF_NE;
+                               else if (strcmp(tmp, "<>") == 0) instr = QANSEL_INSTRUCTION_IF_NE;
+                               else if (strcmp(tmp, ">=") == 0) instr = QANSEL_INSTRUCTION_IF_GE;
+                               else if (strcmp(tmp, "<=") == 0) instr = QANSEL_INSTRUCTION_IF_LE;
+                               else if (strcmp(tmp, ">") == 0) instr = QANSEL_INSTRUCTION_IF_G;
+                               else if (strcmp(tmp, "<") == 0) instr = QANSEL_INSTRUCTION_IF_L;
+                       }
+                       else
+                       {
+                               if (strcmp(tmp, "==") == 0) instr = QANSEL_INSTRUCTION_IF_E;
+                               else if (strcmp(tmp, "!=") == 0) instr = QANSEL_INSTRUCTION_IF_NE;
+                               else if (strcmp(tmp, "<>") == 0) instr = QANSEL_INSTRUCTION_IF_NE;
+                               else if (strcmp(tmp, ">=") == 0) instr = QANSEL_INSTRUCTION_IF_GE;
+                               else if (strcmp(tmp, "<=") == 0) instr = QANSEL_INSTRUCTION_IF_LE;
+                               else if (strcmp(tmp, ">") == 0) instr = QANSEL_INSTRUCTION_IF_G;
+                               else if (strcmp(tmp, "<") == 0) instr = QANSEL_INSTRUCTION_IF_L;
+                       }
                }
                {
                        int strbeg = regmatches[rmp].rm_so;
@@ -593,11 +614,12 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        tmp[strlen] = 0;
                        s0 = (unsigned short)atoi(tmp);
                }
+               //printf("%s(%02x,%i)\n", qansel_instruction_to_string(instr), a0, s0);
                *binarySize += 2 + sizeof(unsigned short);
-               binary = realloc(binary, (*binarySize));
-               binary[(*binarySize) - 2 - sizeof(unsigned short)] = instr;
-               binary[(*binarySize) - 1 - sizeof(unsigned short)] = a0;
-               memcpy(binary + ((*binarySize) - sizeof(unsigned short)), &s0, sizeof(unsigned short));
+               *binary = realloc(*binary, (*binarySize));
+               (*binary)[(*binarySize) - 2 - sizeof(unsigned short)] = instr;
+               (*binary)[(*binarySize) - 1 - sizeof(unsigned short)] = a0;
+               memcpy((*binary) + ((*binarySize) - sizeof(unsigned short)), &s0, sizeof(unsigned short));
        }
        else if (index == 11) //floating point settings
        {
@@ -625,9 +647,9 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        }
                }
                *binarySize += 1 + sizeof(float);
-               binary = realloc(binary, (*binarySize));
-               binary[(*binarySize) - 1 - sizeof(float)] = instr;
-               memcpy(binary + ((*binarySize) - sizeof(float)), &d0, sizeof(float));
+               *binary = realloc(*binary, (*binarySize));
+               (*binary)[(*binarySize) - 1 - sizeof(float)] = instr;
+               memcpy((*binary) + ((*binarySize) - sizeof(float)), &d0, sizeof(float));
        }
        else if (index == 12) //lone instructions
        {
@@ -643,8 +665,8 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche
                        else if (strcmp(tmp, "exit") == 0) instr = QANSEL_INSTRUCTION_EXIT;
                }
                *binarySize += 1;
-               binary = realloc(binary, (*binarySize));
-               binary[(*binarySize) - 1] = instr;
+               *binary = realloc(*binary, (*binarySize));
+               (*binary)[(*binarySize) - 1] = instr;
        }
        return 1;
 }
@@ -666,15 +688,16 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count)
                "^\\s*(rx|ry|rz|u1)\\(\\s*([-/0-9PI.]*)\\s*\\)\\s*q\\s*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*|)$",
                "^\\s*(u2)\\(\\s*([-/0-9PI.]*)\\s*,\\s*([-/0-9PI.]*)\\s*\\)\\s*q\\s*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*|)$",
                "^\\s*(u|u3)\\(\\s*([-/0-9PI.]*)\\s*,\\s*([-/0-9PI.]*)\\s*,\\s*([-/0-9PI.]*)\\s*\\)\\s*q\\s*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*|)$",
-               "^\\s*measure\\s*q\\s*(\\[\\s*([0-9]*)\\s*\\]\\s*|)\\s*->\\s*c\\c*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*|)$",
+               "^\\s*measure\\s*q\\s*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*)\\s*->\\s*c\\c*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*)$",
+               //"^\\s*measure\\s*q\\s*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*|)\\s*->\\s*c\\c*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*|)$",
                "^\\s*(sample|reset|print)\\s*c\\s*(\\[\\s*([0-9][0-9]*)\\s*\\]\\s*|)$",
                "^\\s*(cx|cnot|swap)\\s*q\\s*\\[\\s*([0-9][0-9]*)\\s*\\]\\s*,\\s*q\\s*\\[\\s*([0-9]*)\\s*\\]\\s*$",
-               "^\\s*(ccx|toffoli|cswap|fredkin)\\s*q\\s*\\[\\s*([0-9]*)\\s*\\]\\s*,\\s*q\\s*\\[\\s*([0-9]*)\\s*\\]\\s*,\\s*q\\s*\\[\\s*([0-9][0-9]*)\\s*\\]\\s*$",
+               "^\\s*(ccx|toffoli|cswap|fredkin)\\s*q\\s*\\[\\s*([0-9][0-9]*)\\s*\\]\\s*,\\s*q\\s*\\[\\s*([0-9][0-9]*)\\s*\\]\\s*,\\s*q\\s*\\[\\s*([0-9][0-9]*)\\s*\\]\\s*$",
                "^\\s*if\\s*\\(\\s*c\\s*(\\[\\s*([0-9][0-9]*)\\s*\\]|)\\s*(==|!=|<>|>=|<=|>|<)\\s*([0-9][0-9]*)\\s*\\).*\\s*$",
                "^\\s*(hvar)\\s*([-/0-9PI.]*)\\s*$",
                "^\\s*(rand|exit)\\s*$",
        };
-       int ret, status, found;
+       int ret, status = 0, found;
        for (int i = 0; i < count; i++)
        {
                found = 0;
@@ -686,10 +709,11 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count)
                                exit(1);
                        }
                        ret = regexec(&regex, chunks[i], 10, regmatches, 0);
+                       regfree(&regex);
                        if (!ret)
                        {
                                found = 1;
-                               status = qansel_process_chunk(j, chunks[i], associatedLines[i], regmatches, &qubitCount, &bitCount, binary, &binarySize);
+                               status = qansel_process_chunk(j, chunks[i], associatedLines[i], regmatches, &qubitCount, &bitCount, &binary, &binarySize);
                                break;
                        }
                        else if (ret == REG_NOMATCH) {}
@@ -699,7 +723,6 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count)
                                fprintf(stderr, "QAnsel: %s.\n", errbuf);
                                exit(1);
                        }
-                       regfree(&regex);
                }
                if (!status) break;
                if (!found)
@@ -708,11 +731,21 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count)
                        return 0;
                }
        }
-       //qansel_execute(binary, binarySize);
+       qansel_execute(binary, binarySize);
+       free(binary);
 }
 
 void main(int argc, char** argv)
 {
+       MODE = MODE_METAL_THREADED;
+       unsigned char err = cpx_mtx_begin();
+       if (err == 0 && (MODE == MODE_METAL_THREADED || MODE == MODE_METAL))
+       {
+               MODE = MODE == MODE_METAL_THREADED ? MODE_THREADED : MODE_BARE;
+       }
+       RANDOM_FILE = fopen("/dev/TrueRNG0", "r");
+       if (!RANDOM_FILE) RANDOM_FILE = fopen("/dev/random", "r");
+
        char* script = malloc(0);
        size_t scriptSize = 0;
        int c;
@@ -733,17 +766,10 @@ void main(int argc, char** argv)
        free(chunks);
        free(chunksAssociatedLines);
        free(script);
-       return;
 
-       MODE = MODE_BARE;
-       unsigned char err = cpx_mtx_begin();
-       if (err == 0 && (MODE == MODE_METAL_THREADED || MODE == MODE_METAL))
-       {
-               MODE = MODE == MODE_METAL_THREADED ? MODE_THREADED : MODE_BARE;
-       }
-       RANDOM_FILE = fopen("/dev/TrueRNG0", "r");
-       if (!RANDOM_FILE) RANDOM_FILE = fopen("/dev/random", "r");
-       //qansel_execute
        fclose(RANDOM_FILE);
+
        if (MODE_METAL || MODE_METAL_THREADED) cpx_mtx_clean();
+       return;
+
 }
\ No newline at end of file
index 46c940f5a11483282aba98ca92f1bdba9a4b47fc..dc32ab237cb6b9fefd650e1ca7b6e707e64d4778 100644 (file)
@@ -535,6 +535,10 @@ int qansel_get_instruction_bitmax(unsigned char* ptr, int offset, int* bitmax, i
                        *qbitmax = a0 + 1;
                        *bitmax = (a1 - 0x10) + 1;
                        return 1;
+               case QANSEL_INSTRUCTION_RAND:
+               case QANSEL_INSTRUCTION_HVAR:
+               case QANSEL_INSTRUCTION_EXIT:
+                       return 1;
        }
        return 0;
 }
@@ -809,7 +813,6 @@ void qansel_crawl(unsigned char* program, int programSize, int* qubitCount, int*
        while (PC < programSize)
        {
                int next = qansel_get_instruction_size(program[PC]);
-               printf("|%s|\n", qansel_instruction_to_string(program[PC]));
                if (program[PC] == QANSEL_INSTRUCTION_SAMPLE)
                {
                        if ((program[PC + 1] < 0x10 || program[PC + 1] > 0x1D) && program[PC + 1] != 0x1F)
@@ -830,7 +833,7 @@ void qansel_crawl(unsigned char* program, int programSize, int* qubitCount, int*
                int success = qansel_get_instruction_bitmax(program, PC, &bitmax, &qbitmax);
                if (!success)
                {
-                       fprintf(stderr, "QAnsel (%04X): Invalid index (%s).\n", PC);
+                       fprintf(stderr, "QAnsel (%04X): Invalid index.\n", PC);
                        exit(1);
                }
                if (bitmax > *bitCount) *bitCount = bitmax;
@@ -1031,6 +1034,7 @@ void qansel_run(unsigned char* program, int programSize, int qubitCount, int bit
                        outputBitVector[i] = bitVector[i];
                }
        }
+       cpx_mtx_free(&stateVector);
 }
 
 void qansel_execute(unsigned char* buff, int sizeofbuff)