From: miha-q <> Date: Thu, 14 Mar 2024 15:32:16 +0000 (-0400) Subject: Thu Mar 14 11:32:16 AM EDT 2024 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=c2057975358de0c71418d50df94b3bbf32b20934;p=QAnsel.git Thu Mar 14 11:32:16 AM EDT 2024 --- diff --git a/src/bytecode.c b/src/bytecode.c index b4a37b3..ac53737 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -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++) diff --git a/src/complex.c b/src/complex.c index c1ab4e6..f9481e8 100644 --- a/src/complex.c +++ b/src/complex.c @@ -5,7 +5,7 @@ #include #include #include -#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 index 09e7151..0000000 --- a/src/cores.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#if defined(_WIN32) || defined(_WIN64) -#include -#elif defined(__linux__) -#include -#include -#elif defined(__APPLE__) -#include -#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 index 0000000..3b6d3b2 --- /dev/null +++ b/src/hardware.c @@ -0,0 +1,85 @@ +#include +#include +#if defined(_WIN32) || defined(_WIN64) +#include +#elif defined(__linux__) +#include +#include +#elif defined(__APPLE__) +#include +#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 index 0000000..de7e2e4 --- /dev/null +++ b/src/interface.html @@ -0,0 +1,40 @@ + + + + + + + + +
HXYZ
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Q0---
Q1---
Q2
Q3---
Q4---
+ + \ No newline at end of file diff --git a/src/main.c b/src/main.c index 8ccb417..e7dd096 100644 --- a/src/main.c +++ b/src/main.c @@ -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;