From: miha-q <> Date: Tue, 19 Mar 2024 22:46:35 +0000 (-0400) Subject: Tue Mar 19 06:46:35 PM EDT 2024 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=3d03ce0544218fcf3d589f10d1b01d5d2384469d;p=QAnsel.git Tue Mar 19 06:46:35 PM EDT 2024 --- diff --git a/build.sh b/build.sh index c01195e..7b957d1 100644 --- a/build.sh +++ b/build.sh @@ -30,6 +30,9 @@ else bcmd="gcc src/main.c -g -o bin/QAnsel -lm$x86 -D__PTHREAD__ -D__SDL2__ -D__OPENCL__ -I/usr/include/SDL2 -D_REENTRANT -lSDL2 -lOpenCL -pthread" fi echo "$bcmd" -$($bcmd) -rm -f src/.kernel* src/kernel_* -echo "Built." \ No newline at end of file +if $($bcmd) then + echo "Built." +else + echo "Failed." +fi +rm -f src/.kernel* src/kernel_* \ No newline at end of file diff --git a/examples/bb84.txt b/examples/bb84.txt index d0cfe17..29acc29 100644 --- a/examples/bb84.txt +++ b/examples/bb84.txt @@ -1,26 +1,7 @@ qreg q[2]; creg c[1]; -//Alice sends two bits 00 h q[0]; h q[1]; -//Eve tries to measure the first -measure q[0] -> c[0]; - -//Bob applies the gate before measuring -h q[0]; -h q[1]; - -//The first should now be random noise -// while the second unmeasured one -// should be 0 -born q; - -//In a full BB84 Alice would randomly -// apply the gate or not apply the -// gate so Eve would have to guess -// and would inevitably end up -// turning the transferred bits -// into random noise whenever there -// is a wrong guess. \ No newline at end of file +density q[0]; \ No newline at end of file diff --git a/src/bytecode.c b/src/bytecode.c index dd434ec..0ca140f 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -9,35 +9,35 @@ const char* qansel_instruction_to_string(unsigned char instr) { switch (instr) { - case QANSEL_INSTRUCTION_X: return "QANSEL_INSTRUCTION_X"; - case QANSEL_INSTRUCTION_Y: return "QANSEL_INSTRUCTION_Y"; - case QANSEL_INSTRUCTION_Z: return "QANSEL_INSTRUCTION_Z"; - case QANSEL_INSTRUCTION_H: return "QANSEL_INSTRUCTION_H"; - case QANSEL_INSTRUCTION_S: return "QANSEL_INSTRUCTION_S"; - case QANSEL_INSTRUCTION_T: return "QANSEL_INSTRUCTION_T"; - case QANSEL_INSTRUCTION_RX: return "QANSEL_INSTRUCTION_RX"; - case QANSEL_INSTRUCTION_RY: return "QANSEL_INSTRUCTION_RY"; - case QANSEL_INSTRUCTION_RZ: return "QANSEL_INSTRUCTION_RZ"; - case QANSEL_INSTRUCTION_U1: return "QANSEL_INSTRUCTION_U1"; - case QANSEL_INSTRUCTION_U2: return "QANSEL_INSTRUCTION_U2"; - case QANSEL_INSTRUCTION_U3: return "QANSEL_INSTRUCTION_U3"; - case QANSEL_INSTRUCTION_CX: return "QANSEL_INSTRUCTION_CX"; - case QANSEL_INSTRUCTION_SWAP: return "QANSEL_INSTRUCTION_SWAP"; - case QANSEL_INSTRUCTION_CCX: return "QANSEL_INSTRUCTION_CCX"; - case QANSEL_INSTRUCTION_CSWAP: return "QANSEL_INSTRUCTION_CSWAP"; - case QANSEL_INSTRUCTION_MEASURE: return "QANSEL_INSTRUCTION_MEASURE"; - case QANSEL_INSTRUCTION_DENSITY: return "QANSEL_INSTRUCTION_DENSITY"; - case QANSEL_INSTRUCTION_BORN: return "QANSEL_INSTRUCTION_BORN"; - case QANSEL_INSTRUCTION_IF_E: return "QANSEL_INSTRUCTION_JUMP_E"; - case QANSEL_INSTRUCTION_IF_NE: return "QANSEL_INSTRUCTION_JUMP_NE"; - case QANSEL_INSTRUCTION_IF_G: return "QANSEL_INSTRUCTION_JUMP_G"; - case QANSEL_INSTRUCTION_IF_GE: return "QANSEL_INSTRUCTION_JUMP_GE"; - case QANSEL_INSTRUCTION_IF_L: return "QANSEL_INSTRUCTION_JUMP_L"; - case QANSEL_INSTRUCTION_IF_LE: return "QANSEL_INSTRUCTION_JUMP_LE"; - case QANSEL_INSTRUCTION_PRINT: return "QANSEL_INSTRUCTION_PRINT"; - case QANSEL_INSTRUCTION_RESET: return "QANSEL_INSTRUCTION_RESET"; - case QANSEL_INSTRUCTION_BARRIER: return "QANSEL_INSTRUCTION_BARRIER"; - case QANSEL_INSTRUCTION_EXIT: return "QANSEL_INSTRUCTION_EXIT"; + case QANSEL_INSTRUCTION_X: return "x"; + case QANSEL_INSTRUCTION_Y: return "y"; + case QANSEL_INSTRUCTION_Z: return "z"; + case QANSEL_INSTRUCTION_H: return "h"; + case QANSEL_INSTRUCTION_S: return "s"; + case QANSEL_INSTRUCTION_T: return "t"; + case QANSEL_INSTRUCTION_RX: return "rx"; + case QANSEL_INSTRUCTION_RY: return "ry"; + case QANSEL_INSTRUCTION_RZ: return "rz"; + case QANSEL_INSTRUCTION_U1: return "u1"; + case QANSEL_INSTRUCTION_U2: return "u2"; + case QANSEL_INSTRUCTION_U3: return "u3"; + case QANSEL_INSTRUCTION_CX: return "cx"; + case QANSEL_INSTRUCTION_SWAP: return "swap"; + case QANSEL_INSTRUCTION_CCX: return "ccx"; + case QANSEL_INSTRUCTION_CSWAP: return "cswap"; + case QANSEL_INSTRUCTION_MEASURE: return "measure"; + case QANSEL_INSTRUCTION_DENSITY: return "density"; + case QANSEL_INSTRUCTION_BORN: return "born"; + case QANSEL_INSTRUCTION_IF_E: return "if=="; + case QANSEL_INSTRUCTION_IF_NE: return "if!="; + case QANSEL_INSTRUCTION_IF_G: return "if>"; + case QANSEL_INSTRUCTION_IF_GE: return "if>="; + case QANSEL_INSTRUCTION_IF_L: return "if<"; + case QANSEL_INSTRUCTION_IF_LE: return "if<="; + case QANSEL_INSTRUCTION_PRINT: return "print"; + case QANSEL_INSTRUCTION_RESET: return "reset"; + case QANSEL_INSTRUCTION_BARRIER: return "barrier"; + case QANSEL_INSTRUCTION_EXIT: return "exit"; } return "Unknown"; } @@ -503,8 +503,12 @@ int qansel_get_instruction_bitmax(unsigned char* ptr, int offset, int* bitmax, i case QANSEL_INSTRUCTION_U1: case QANSEL_INSTRUCTION_U2: case QANSEL_INSTRUCTION_U3: - case QANSEL_INSTRUCTION_BARRIER: case QANSEL_INSTRUCTION_DENSITY: + a0 = ptr[offset + 1]; + if (a0 > QANSEL_QBOUND_UPPER) return 0; + *qbitmax = a0 + 1; + return 1; + case QANSEL_INSTRUCTION_BARRIER: case QANSEL_INSTRUCTION_BORN: a0 = ptr[offset + 1]; if (a0 > QANSEL_QBOUND_UPPER && a0 != QANSEL_ALL_QUANTUM) return 0; @@ -523,17 +527,15 @@ int qansel_get_instruction_bitmax(unsigned char* ptr, int offset, int* bitmax, i case QANSEL_INSTRUCTION_RESET: case QANSEL_INSTRUCTION_PRINT: a0 = ptr[offset + 1]; - if (a0 == QANSEL_ALL) return 1; - if (a0 < QANSEL_CBOUND_LOWER) + if (a0 == QANSEL_ALL || a0 == QANSEL_ALL_QUANTUM || a0 == QANSEL_ALL_CLASSIC) return 1; + if (a0 <= QANSEL_QBOUND_UPPER) { - if (a0 > QANSEL_QBOUND_UPPER && a0 != QANSEL_ALL_QUANTUM) return 0; - if (a0 != QANSEL_ALL_QUANTUM) *qbitmax = a0 + 1; + *qbitmax = a0 + 1; return 1; } - else + else if (a0 >= QANSEL_CBOUND_LOWER && a0 <= QANSEL_CBOUND_UPPER) { - if ((a0 > QANSEL_CBOUND_UPPER || a0 < QANSEL_CBOUND_LOWER) && a0 != QANSEL_ALL_CLASSIC) return 0; - if (a0 != QANSEL_ALL_CLASSIC) *bitmax = (a0 - QANSEL_CBOUND_LOWER) + 1; + *bitmax = (a0 - QANSEL_CBOUND_LOWER) + 1; return 1; } return 0; @@ -1203,7 +1205,7 @@ void qansel_reorder(QAnselContext* ctx, unsigned char* program, int programSize) copyloc += next; } } - + if (ctx->verbose) { if (best > efficiency) @@ -1215,6 +1217,8 @@ void qansel_reorder(QAnselContext* ctx, unsigned char* program, int programSize) printf("Could not increase efficiency.\n"); } } + + free(ramInstr); } void qansel_run(QAnselContext* ctx, unsigned char* program, int programSize, int qubitCount, int bitCount, unsigned char* outputBitVector) @@ -1298,6 +1302,10 @@ void qansel_run(QAnselContext* ctx, unsigned char* program, int programSize, int } } + if (ctx->verbose) + { + printf("%08X: %s\n", PC, qansel_instruction_to_string(instr)); + } switch (instr) { case QANSEL_INSTRUCTION_X: diff --git a/src/main.c b/src/main.c index 950d1f1..01c0521 100644 --- a/src/main.c +++ b/src/main.c @@ -20,10 +20,10 @@ void display_help() printf(" number generator.\n"); printf(" -v Enables verbose mode.\n"); printf(" -o0 No optimizations (default).\n"); - printf(" -o1 Enables multithreading.\n"); - printf(" -o2 Enables hardware acceleration (GPU).\n"); + printf(" -o1 Enables smart optimizations.\n"); + printf(" -o2 Enables multithreading.\n"); printf(" -o3 Combines -o1 and -o2.\n"); - printf(" -o4 Enables smart optimizations.\n"); + printf(" -o4 Enables hardware acceleration (GPU).\n"); printf(" -o5 Combines -o1 and -o4.\n"); printf(" -o6 Combines -o2 and -o4.\n"); printf(" -o7 Combines -o3 and -o4.\n"); diff --git a/src/openqasm.c b/src/openqasm.c index 1cad5e1..ee0367e 100644 --- a/src/openqasm.c +++ b/src/openqasm.c @@ -321,10 +321,23 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche else { a0 = QANSEL_ALL_QUANTUM; - if (instr == QANSEL_INSTRUCTION_DENSITY) + switch (instr) { - fprintf(stderr, "QAnsel on line %i: Density matrices can only be produced for individual qubits.\n", line, a0); - return 0; + case QANSEL_INSTRUCTION_DENSITY: + case QANSEL_INSTRUCTION_RX: + case QANSEL_INSTRUCTION_RY: + case QANSEL_INSTRUCTION_RZ: + case QANSEL_INSTRUCTION_U1: + case QANSEL_INSTRUCTION_U2: + case QANSEL_INSTRUCTION_U3: + case QANSEL_INSTRUCTION_X: + case QANSEL_INSTRUCTION_Y: + case QANSEL_INSTRUCTION_Z: + case QANSEL_INSTRUCTION_H: + case QANSEL_INSTRUCTION_S: + case QANSEL_INSTRUCTION_T: + fprintf(stderr, "QAnsel on line %i: Invalid syntax.\n", line, a0); + return 0; } } } @@ -757,6 +770,12 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsign int qanselBuildFromSource(char* source, unsigned char** binary, int* binarySize, QAnselContext* ctx) { + size_t source_len = strlen(source); + if (source_len > (INT_MAX/2)) + { + fprintf(stderr, "QAnsel: Source file truncated.\n"); + source[INT_MAX/2] = 0; + } if (!qanselContextValidate(ctx)) return 0; char** chunks; int* chunksAssociatedLines; diff --git a/src/qansel.h b/src/qansel.h index 61b8d44..a1a6782 100644 --- a/src/qansel.h +++ b/src/qansel.h @@ -12,6 +12,7 @@ #include #include #include +#include #if defined(_WIN32) || defined(_WIN64) #include #elif defined(__linux__) @@ -41,9 +42,9 @@ size_t cpx_mtx_cache_len = 0; #define QANSEL_SHOTS_DEFAULT 1000 #define QANSEL_QUBITS_MAX 16 #define QANSEL_MODE_BARE 0 -#define QANSEL_MODE_THREADED 1 -#define QANSEL_MODE_METAL 2 -#define QANSEL_MODE_SMART 4 +#define QANSEL_MODE_SMART 1 +#define QANSEL_MODE_THREADED 2 +#define QANSEL_MODE_METAL 4 //#define SPEED_TEST #define QANSEL_HARDWARE_NONE 0