]> foleosoft.com Git - QAnsel.git/commitdiff
Tue Mar 19 06:46:35 PM EDT 2024
authormiha-q <>
Tue, 19 Mar 2024 22:46:35 +0000 (18:46 -0400)
committermiha-q <>
Tue, 19 Mar 2024 22:46:35 +0000 (18:46 -0400)
build.sh
examples/bb84.txt
src/bytecode.c
src/main.c
src/openqasm.c
src/qansel.h

index c01195e7731e47ff09602938419e3b0556739049..7b957d14e0c4afdcb2a3beb2ec83e87bc16ccd62 100644 (file)
--- 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
index d0cfe170a287a78bb596a031b5f242b18a864408..29acc29b462e4cf2a74c583edac61f9e68003dc4 100644 (file)
@@ -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
index dd434ecd99fbb357ecdab3403c4f9f77087050ee..0ca140fee74c221c5f0d50a50c50a02e081cab2c 100644 (file)
@@ -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:
index 950d1f14943ec3c984669107137cb7cb8515df56..01c0521f30250a6ddc440a39f526f808067a059c 100644 (file)
@@ -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");
index 1cad5e1f32d250f76c336c644900fef60f554343..ee0367ee4dd4a72878790cd5fb8ce5685dacfb96 100644 (file)
@@ -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;
index 61b8d44710a18f35d6a1dac3f3e59bf15ecdd07d..a1a6782ab664a4a4501a357d4166d2a3123bd34a 100644 (file)
@@ -12,6 +12,7 @@
 #include <string.h>
 #include <time.h>
 #include <math.h>
+#include <limits.h>
 #if defined(_WIN32) || defined(_WIN64)
 #include <windows.h>
 #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