]> foleosoft.com Git - QAnsel.git/commitdiff
Thu Mar 14 11:32:16 AM EDT 2024
authormiha-q <>
Thu, 14 Mar 2024 15:32:16 +0000 (11:32 -0400)
committermiha-q <>
Thu, 14 Mar 2024 15:32:16 +0000 (11:32 -0400)
src/bytecode.c
src/complex.c
src/cores.c [deleted file]
src/hardware.c [new file with mode: 0644]
src/interface.html [new file with mode: 0644]
src/main.c

index b4a37b3558d45e8530bcae70b9bdf6c5b1e1e252..ac53737420b01bc265f8a60314e0007b5c2e4819 100644 (file)
@@ -151,7 +151,6 @@ void qansel_fredkin(cpx_mtx_t* stateVector, unsigned char qubitCount, unsigned c
        stateVector->cols = ret.cols;
 }
 
-
 void qansel_toffoli(cpx_mtx_t* stateVector, unsigned char qubitCount, unsigned char bitA, unsigned char bitB, unsigned char bitC)
 {
        if (bitA >= qubitCount || bitB >= qubitCount) return;
@@ -572,6 +571,114 @@ int qansel_get_instruction_bitmax(unsigned char* ptr, int offset, int* bitmax, i
        return 0;
 }
 
+int qansel_get_barrier(int** q, int qubitCount, int bitCount, unsigned char* binary, int pos)
+{
+
+       unsigned char instr = binary[pos];
+       switch (instr)
+       {
+               case QANSEL_INSTRUCTION_SAMPLE:
+               case QANSEL_INSTRUCTION_RAND:
+               case QANSEL_INSTRUCTION_HVAR:
+               case QANSEL_INSTRUCTION_EXIT:
+               case QANSEL_INSTRUCTION_IF_E:
+               case QANSEL_INSTRUCTION_IF_NE:
+               case QANSEL_INSTRUCTION_IF_G:
+               case QANSEL_INSTRUCTION_IF_GE:
+               case QANSEL_INSTRUCTION_IF_L:
+               case QANSEL_INSTRUCTION_IF_LE:
+                       *q = NULL;
+                       return 0;
+               case QANSEL_INSTRUCTION_X:
+               case QANSEL_INSTRUCTION_Y:
+               case QANSEL_INSTRUCTION_Z:
+               case QANSEL_INSTRUCTION_H:
+               case QANSEL_INSTRUCTION_S:
+               case QANSEL_INSTRUCTION_T:
+               case QANSEL_INSTRUCTION_RX:
+               case QANSEL_INSTRUCTION_RY:
+               case QANSEL_INSTRUCTION_RZ:
+               case QANSEL_INSTRUCTION_U1:
+               case QANSEL_INSTRUCTION_U2:
+               case QANSEL_INSTRUCTION_U3:
+                       //only barrier single-qubit instructions if
+                       //      they are nexted in an if statement
+                       if (pos >= (1 + sizeof(unsigned short)))
+                       {
+                               switch (binary[pos - (1 + sizeof(unsigned short))])
+                               {
+                                       case QANSEL_INSTRUCTION_IF_E:
+                                       case QANSEL_INSTRUCTION_IF_NE:
+                                       case QANSEL_INSTRUCTION_IF_G:
+                                       case QANSEL_INSTRUCTION_IF_GE:
+                                       case QANSEL_INSTRUCTION_IF_L:
+                                       case QANSEL_INSTRUCTION_IF_LE:
+                                               *q = malloc(1);
+                                               (*q)[0] = binary[pos + 1] + QANSEL_QBOUND_LOWER;
+                                               return 1;
+                               }
+                       }
+                       *q = NULL;
+                       return 0;
+               case QANSEL_INSTRUCTION_MEASURE:
+               case QANSEL_INSTRUCTION_DENSITY:
+                       *q = malloc(2);
+                       (*q)[0] = binary[pos + 1] + QANSEL_QBOUND_LOWER;
+                       (*q)[1] = binary[pos + 2] + QANSEL_CBOUND_LOWER;
+                       return 1;
+               case QANSEL_INSTRUCTION_BORN:
+               case QANSEL_INSTRUCTION_BARRIER:
+               case QANSEL_INSTRUCTION_PRINT:
+               case QANSEL_INSTRUCTION_RESET:
+                       switch (binary[pos + 1])
+                       {
+                               case QANSEL_ALL:
+                                       *q = malloc(sizeof(int) * (qubitCount + bitCount));
+                                       for (int i = 0; i < qubitCount; i++)
+                                       {
+                                               (*q)[i] = i + QANSEL_QBOUND_LOWER;
+                                       }
+                                       for (int i = 0; i < bitCount; i++)
+                                       {
+                                               (*q)[i] = i + QANSEL_CBOUND_LOWER;
+                                       }
+                                       return qubitCount + bitCount;
+                               case QANSEL_ALL_QUANTUM:
+                                       *q = malloc(sizeof(int) * qubitCount);
+                                       for (int i = 0; i < qubitCount; i++)
+                                       {
+                                               (*q)[i] = i + QANSEL_QBOUND_LOWER;
+                                       }
+                                       return qubitCount;
+                               case QANSEL_ALL_CLASSIC:
+                                       *q = malloc(sizeof(int) * bitCount);
+                                       for (int i = 0; i < bitCount; i++)
+                                       {
+                                               (*q)[i] = i + QANSEL_CBOUND_LOWER;
+                                       }
+                                       return bitCount;
+                               default:
+                                       *q = malloc(1);
+                                       (*q)[0] = binary[pos + 1];
+                                       return 1;
+                       }
+               case QANSEL_INSTRUCTION_CX:
+               case QANSEL_INSTRUCTION_SWAP:
+                       *q = malloc(2);
+                       (*q)[0] = binary[pos + 1] + QANSEL_QBOUND_LOWER;
+                       (*q)[1] = binary[pos + 2] + QANSEL_QBOUND_LOWER;
+                       return 2;
+               case QANSEL_INSTRUCTION_CCX:
+               case QANSEL_INSTRUCTION_CSWAP:
+                       *q = malloc(3);
+                       (*q)[0] = binary[pos + 1] + QANSEL_QBOUND_LOWER;
+                       (*q)[1] = binary[pos + 2] + QANSEL_QBOUND_LOWER;
+                       (*q)[2] = binary[pos + 3] + QANSEL_QBOUND_LOWER;
+                       return 3;
+       }
+       fprintf(stderr, "QAnsel (%04X): Unknown error in barrier analysis.\n", pos);
+}
+
 int qansel_get_instruction_size(unsigned char instr)
 {
        switch (instr)
@@ -938,6 +1045,16 @@ void qansel_run(unsigned char* program, int programSize, int qubitCount, int bit
                                        case QANSEL_INSTRUCTION_U3:
                                                queueFlushed = 0;
                                                break;
+                                       case QANSEL_INSTRUCTION_IF_E:
+                                       case QANSEL_INSTRUCTION_IF_NE:
+                                       case QANSEL_INSTRUCTION_IF_G:
+                                       case QANSEL_INSTRUCTION_IF_GE:
+                                       case QANSEL_INSTRUCTION_IF_L:
+                                       case QANSEL_INSTRUCTION_IF_LE:
+                                       case QANSEL_INSTRUCTION_SAMPLE:
+                                       case QANSEL_INSTRUCTION_HVAR:
+                                       case QANSEL_INSTRUCTION_RAND:
+                                               break;
                                        default:
                                                qansel_instruction(&stateVector, qubitCount, instr, 0, 0, 0, 0, &queueVector);
                                                for (int i = 0; i < qubitCount; i++)
index c1ab4e672cf44ab6d1fe989082326f5174e191bd..f9481e81013b66c531d556d6e1edaaf8af8f57f0 100644 (file)
@@ -5,7 +5,7 @@
 #include <math.h>
 #include <pthread.h>
 #include <string.h>
-#include "cores.c"
+#include "hardware.c"
 #include "kernel_cpu.cl"
 #include "kernel_gpu.cl"
 typedef struct
diff --git a/src/cores.c b/src/cores.c
deleted file mode 100644 (file)
index 09e7151..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-#include <stdio.h>
-#include <time.h>
-#if defined(_WIN32) || defined(_WIN64)
-#include <windows.h>
-#elif defined(__linux__)
-#include <unistd.h>
-#include <sys/time.h>
-#elif defined(__APPLE__)
-#include <sys/sysctl.h>
-#endif
-
-int ___get_core_count()
-{
-    #if defined(_WIN32) || defined(_WIN64)
-    SYSTEM_INFO sysinfo;
-    GetSystemInfo(&sysinfo);
-    return sysinfo.dwNumberOfProcessors;
-    #elif defined(__linux__)
-    return sysconf(_SC_NPROCESSORS_ONLN);
-    #elif defined(__APPLE__)
-    int nm[2];
-    size_t len = 4;
-    uint32_t count;
-
-    nm[0] = CTL_HW;
-    nm[1] = HW_AVAILCPU;
-    sysctl(nm, 2, &count, &len, NULL, 0);
-
-    if(count < 1) {
-        nm[1] = HW_NCPU;
-        sysctl(nm, 2, &count, &len, NULL, 0);
-        if(count < 1) {
-            count = 1;
-        }
-    }
-    return count;
-    #else
-    return -1; // Unknown platform
-    #endif
-}
-
-int get_core_count()
-{
-    static int coreCount = -1;
-    if (coreCount == -1)
-    {
-        coreCount = ___get_core_count();
-        if (coreCount == -1)
-        {
-            coreCount = 1;
-        }
-    }
-    return coreCount;
-}
-
-unsigned long int get_time()
-{
-    struct timeval tv;
-    gettimeofday(&tv,NULL);
-    unsigned long int timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
-    return timestamp;
-}
\ No newline at end of file
diff --git a/src/hardware.c b/src/hardware.c
new file mode 100644 (file)
index 0000000..3b6d3b2
--- /dev/null
@@ -0,0 +1,85 @@
+#include <stdio.h>
+#include <time.h>
+#if defined(_WIN32) || defined(_WIN64)
+#include <windows.h>
+#elif defined(__linux__)
+#include <unistd.h>
+#include <sys/time.h>
+#elif defined(__APPLE__)
+#include <sys/sysctl.h>
+#endif
+
+int ___get_core_count()
+{
+    #if defined(_WIN32) || defined(_WIN64)
+    SYSTEM_INFO sysinfo;
+    GetSystemInfo(&sysinfo);
+    return sysinfo.dwNumberOfProcessors;
+    #elif defined(__linux__)
+    return sysconf(_SC_NPROCESSORS_ONLN);
+    #elif defined(__APPLE__)
+    int nm[2];
+    size_t len = 4;
+    uint32_t count;
+
+    nm[0] = CTL_HW;
+    nm[1] = HW_AVAILCPU;
+    sysctl(nm, 2, &count, &len, NULL, 0);
+
+    if(count < 1) {
+        nm[1] = HW_NCPU;
+        sysctl(nm, 2, &count, &len, NULL, 0);
+        if(count < 1) {
+            count = 1;
+        }
+    }
+    return count;
+    #else
+    return -1; // Unknown platform
+    #endif
+}
+
+int get_core_count()
+{
+    static int coreCount = -1;
+    if (coreCount == -1)
+    {
+        coreCount = ___get_core_count();
+        if (coreCount == -1)
+        {
+            coreCount = 1;
+        }
+    }
+    return coreCount;
+}
+
+unsigned long int get_time()
+{
+    struct timeval tv;
+    gettimeofday(&tv,NULL);
+    unsigned long int timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
+    return timestamp;
+}
+
+int qansel_hardware_rand_supported()
+{
+    #if defined(__x86_64__) || defined(__i386__)
+    int result = 0;
+
+    // Execute CPUID instruction with feature request
+    __asm__ volatile
+        (
+            "mov $7, %%eax;mov $0, %%ecx;cpuid;mov %%ebx, %0;"
+            : "=r" (result)
+            : 
+            : "eax", "ebx", "ecx", "edx"
+        );
+    return (result >> 18) & 1;
+    #else
+    return 0;
+    #endif
+}
+int qansel_hardware_rand()
+{
+
+}
diff --git a/src/interface.html b/src/interface.html
new file mode 100644 (file)
index 0000000..de7e2e4
--- /dev/null
@@ -0,0 +1,40 @@
+<body>
+<table>
+       <tr>
+               <td>H</td>
+               <td>X</td>
+               <td>Y</td>
+               <td>Z</td>
+       </tr>
+</table>
+<table>
+       <tr>
+               <td>Q0</td>
+               <td>-</td>
+               <td>-</td>
+               <td>-</td>
+       </tr>
+       <tr>
+               <td>Q1</td>
+               <td>-</td>
+               <td>-</td>
+               <td>-</td>
+       </tr>
+       <tr>
+               <td>Q2</td>
+       </tr>
+       <tr>
+               <td>Q3</td>
+               <td>-</td>
+               <td>-</td>
+               <td>-</td>
+       </tr>
+       <tr>
+               <td>Q4</td>
+               <td>-</td>
+               <td>-</td>
+               <td>-</td>
+       </tr>
+</table>
+
+</body>
\ No newline at end of file
index 8ccb41720867e004998dc02f2176135e48c7c0b3..e7dd096d44f041bde617c414f8e8667289d2b321 100644 (file)
@@ -29,6 +29,9 @@ void display_help()
 
 void main(int argc, char** argv)
 {
+
+       printf("%i\n", qansel_hardware_rand_supported());
+       return;
        int opt;
        int maximumQubitSettings = QANSEL_QUBITS_MAX;
        int hardwarerngSetting = 0;