From c2057975358de0c71418d50df94b3bbf32b20934 Mon Sep 17 00:00:00 2001 From: miha-q <> Date: Thu, 14 Mar 2024 11:32:16 -0400 Subject: [PATCH] Thu Mar 14 11:32:16 AM EDT 2024 --- src/bytecode.c | 119 +++++++++++++++++++++++++++++++++++- src/complex.c | 2 +- src/{cores.c => hardware.c} | 25 +++++++- src/interface.html | 40 ++++++++++++ src/main.c | 3 + 5 files changed, 186 insertions(+), 3 deletions(-) rename src/{cores.c => hardware.c} (73%) create mode 100644 src/interface.html 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/hardware.c similarity index 73% rename from src/cores.c rename to src/hardware.c index 09e7151..3b6d3b2 100644 --- a/src/cores.c +++ b/src/hardware.c @@ -59,4 +59,27 @@ unsigned long int get_time() gettimeofday(&tv,NULL); unsigned long int timestamp = 1000000 * tv.tv_sec + tv.tv_usec; return timestamp; -} \ No newline at end of file +} + +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; -- 2.39.5