From: miha-q <> Date: Fri, 19 Jul 2024 23:55:03 +0000 (-0400) Subject: Fri Jul 19 07:55:03 PM EDT 2024 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=4dd593228544e0d6ee6229f650ca4022022ac240;p=QAnsel.git Fri Jul 19 07:55:03 PM EDT 2024 --- diff --git a/src/bytecode.c b/src/bytecode.c index 2b3b58b..b171016 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -10,6 +10,8 @@ const char* qansel_instruction_to_string(unsigned char instr) case QANSEL_INSTRUCTION_H: return "h"; case QANSEL_INSTRUCTION_S: return "s"; case QANSEL_INSTRUCTION_T: return "t"; + case QANSEL_INSTRUCTION_SDG: return "sdg"; + case QANSEL_INSTRUCTION_TDG: return "tdg"; case QANSEL_INSTRUCTION_RX: return "rx"; case QANSEL_INSTRUCTION_RY: return "ry"; case QANSEL_INSTRUCTION_RZ: return "rz"; @@ -224,6 +226,8 @@ void qansel_instruction_queue case QANSEL_INSTRUCTION_Z: gate_ptr = PauliZ; break; case QANSEL_INSTRUCTION_S: gate_ptr = PhaseS; break; case QANSEL_INSTRUCTION_T: gate_ptr = PhaseT; break; + case QANSEL_INSTRUCTION_SDG: gate_ptr = PhaseSdg; break; + case QANSEL_INSTRUCTION_TDG: gate_ptr = PhaseTdg; break; case QANSEL_INSTRUCTION_RX: case QANSEL_INSTRUCTION_RY: case QANSEL_INSTRUCTION_RZ: @@ -278,6 +282,8 @@ void qansel_instruction case QANSEL_INSTRUCTION_Z: gate_ptr = PauliZ; break; case QANSEL_INSTRUCTION_S: gate_ptr = PhaseS; break; case QANSEL_INSTRUCTION_T: gate_ptr = PhaseT; break; + case QANSEL_INSTRUCTION_SDG: gate_ptr = PhaseSdg; break; + case QANSEL_INSTRUCTION_TDG: gate_ptr = PhaseTdg; break; case QANSEL_INSTRUCTION_RX: case QANSEL_INSTRUCTION_RY: case QANSEL_INSTRUCTION_RZ: @@ -487,6 +493,8 @@ int qansel_get_instruction_bitmax(unsigned char* ptr, int offset, int* bitmax, i case QANSEL_INSTRUCTION_H: case QANSEL_INSTRUCTION_S: case QANSEL_INSTRUCTION_T: + case QANSEL_INSTRUCTION_SDG: + case QANSEL_INSTRUCTION_TDG: case QANSEL_INSTRUCTION_RX: case QANSEL_INSTRUCTION_RY: case QANSEL_INSTRUCTION_RZ: @@ -569,6 +577,8 @@ int qansel_get_instruction_size(unsigned char instr) case QANSEL_INSTRUCTION_H: return 1 + 1; case QANSEL_INSTRUCTION_S: return 1 + 1; case QANSEL_INSTRUCTION_T: return 1 + 1; + case QANSEL_INSTRUCTION_SDG: return 1 + 1; + case QANSEL_INSTRUCTION_TDG: return 1 + 1; case QANSEL_INSTRUCTION_RX: return 1 + 1 + sizeof(float); case QANSEL_INSTRUCTION_RY: return 1 + 1 + sizeof(float); case QANSEL_INSTRUCTION_RZ: return 1 + 1 + sizeof(float); @@ -615,6 +625,8 @@ void qansel_get_barrier(QBytecode** qbc, int idx) case QANSEL_INSTRUCTION_H: case QANSEL_INSTRUCTION_S: case QANSEL_INSTRUCTION_T: + case QANSEL_INSTRUCTION_SDG: + case QANSEL_INSTRUCTION_TDG: case QANSEL_INSTRUCTION_RX: case QANSEL_INSTRUCTION_RY: case QANSEL_INSTRUCTION_RZ: @@ -911,6 +923,8 @@ int qansel_efficiency(QBytecode* program, int programSize, int head) case QANSEL_INSTRUCTION_H: case QANSEL_INSTRUCTION_S: case QANSEL_INSTRUCTION_T: + case QANSEL_INSTRUCTION_SDG: + case QANSEL_INSTRUCTION_TDG: case QANSEL_INSTRUCTION_RX: case QANSEL_INSTRUCTION_RY: case QANSEL_INSTRUCTION_RZ: @@ -1037,6 +1051,8 @@ void qansel_reorder(QAnselContext* ctx, unsigned char* program, int programSize) case QANSEL_INSTRUCTION_H: case QANSEL_INSTRUCTION_S: case QANSEL_INSTRUCTION_T: + case QANSEL_INSTRUCTION_SDG: + case QANSEL_INSTRUCTION_TDG: case QANSEL_INSTRUCTION_RX: case QANSEL_INSTRUCTION_RY: case QANSEL_INSTRUCTION_RZ: @@ -1263,6 +1279,8 @@ void qansel_run(QAnselContext* ctx, unsigned char* program, int programSize, int case QANSEL_INSTRUCTION_H: case QANSEL_INSTRUCTION_S: case QANSEL_INSTRUCTION_T: + case QANSEL_INSTRUCTION_SDG: + case QANSEL_INSTRUCTION_TDG: case QANSEL_INSTRUCTION_RX: case QANSEL_INSTRUCTION_RY: case QANSEL_INSTRUCTION_RZ: @@ -1304,6 +1322,8 @@ void qansel_run(QAnselContext* ctx, unsigned char* program, int programSize, int case QANSEL_INSTRUCTION_H: case QANSEL_INSTRUCTION_S: case QANSEL_INSTRUCTION_T: + case QANSEL_INSTRUCTION_SDG: + case QANSEL_INSTRUCTION_TDG: case QANSEL_INSTRUCTION_RX: case QANSEL_INSTRUCTION_RY: case QANSEL_INSTRUCTION_RZ: diff --git a/src/bytecode.h b/src/bytecode.h index 0e472bb..25c106b 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -61,11 +61,24 @@ static float Hadamard[] = static float PhaseS[] = { 1, 0, 0, 0, - 0, 0, 0,-1, + 0, 0, 0, 1, }; // 1/sqrt(2) + 1/sqrt(2)i static float PhaseT[] = +{ + 1, 0, 0, 0, + 0, 0, R, R, +}; + +static float PhaseSdg[] = +{ + 1, 0, 0, 0, + 0, 0, 0, 1, +}; + +// 1/sqrt(2) - 1/sqrt(2)i +static float PhaseTdg[] = { 1, 0, 0, 0, 0, 0, R,-R, diff --git a/src/openqasm.c b/src/openqasm.c index c05bbf3..6853858 100644 --- a/src/openqasm.c +++ b/src/openqasm.c @@ -284,6 +284,8 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche else if (strcmp(tmp, "y") == 0) instr = QANSEL_INSTRUCTION_Y; else if (strcmp(tmp, "z") == 0) instr = QANSEL_INSTRUCTION_Z; else if (strcmp(tmp, "h") == 0) instr = QANSEL_INSTRUCTION_H; + else if (strcmp(tmp, "sdg") == 0) instr = QANSEL_INSTRUCTION_SDG; + else if (strcmp(tmp, "tdg") == 0) instr = QANSEL_INSTRUCTION_TDG; else if (strcmp(tmp, "s") == 0) instr = QANSEL_INSTRUCTION_S; else if (strcmp(tmp, "t") == 0) instr = QANSEL_INSTRUCTION_T; } @@ -338,6 +340,8 @@ int qansel_process_chunk(int index, char* chunk, int line, regmatch_t* regmatche case QANSEL_INSTRUCTION_H: case QANSEL_INSTRUCTION_S: case QANSEL_INSTRUCTION_T: + case QANSEL_INSTRUCTION_SDG: + case QANSEL_INSTRUCTION_TDG: fprintf(stderr, "QAnsel on line %i: Invalid syntax.\n", line, a0); return 0; } @@ -716,7 +720,7 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsign { "^[ ]*qreg[ ]*q[ ]*\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*$", "^[ ]*creg[ ]*c[ ]*\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*$", - "^[ ]*(x|y|z|h|s|t|reset|barrier|born|density|print)[ ]*q[ ]*(\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*|)$", + "^[ ]*(x|y|z|h|sdg|tdg|s|t|reset|barrier|born|density|print)[ ]*q[ ]*(\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*|)$", "^[ ]*(rx|ry|rz|u1)\\([ ]*([-/0-9PI.]*)[ ]*\\)[ ]*q[ ]*(\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*|)$", "^[ ]*(u2)\\([ ]*([-/0-9PI.]*)[ ]*,[ ]*([-/0-9PI.]*)[ ]*\\)[ ]*q[ ]*(\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*|)$", "^[ ]*(u|u3)\\([ ]*([-/0-9PI.]*)[ ]*,[ ]*([-/0-9PI.]*)[ ]*,[ ]*([-/0-9PI.]*)[ ]*\\)[ ]*q[ ]*(\\[[ ]*([0-9][0-9]*)[ ]*\\][ ]*|)$", @@ -763,7 +767,7 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsign return 0; } if (!found) - { + { fprintf(stderr, "QAnsel on line %i: Invalid syntax.\n", associatedLines[i]); free(binary); return 0; @@ -776,12 +780,13 @@ int qansel_process_chunks(char** chunks, int* associatedLines, int count, unsign int qanselBuildFromSource(char* osource, unsigned char** binary, int* binarySize, QAnselContext* ctx) { - size_t source_len = strlen(osource); - char* source = malloc(source_len); + size_t source_len = strlen(osource) + 1; + char* source = malloc(source_len + 1); for (size_t i = 0; i < source_len; i++) { source[i] = osource[i] == '\t' ? ' ' : osource[i]; } + source[source_len - 1] = 0; if (source_len > (INT_MAX/2)) { fprintf(stderr, "QAnsel: Source file truncated.\n"); diff --git a/src/qansel.h b/src/qansel.h index 703a0b5..9a10a2e 100644 --- a/src/qansel.h +++ b/src/qansel.h @@ -21,6 +21,8 @@ #define QANSEL_INSTRUCTION_H 0x13 #define QANSEL_INSTRUCTION_S 0x14 #define QANSEL_INSTRUCTION_T 0x15 +#define QANSEL_INSTRUCTION_SDG 0x16 +#define QANSEL_INSTRUCTION_TDG 0x17 #define QANSEL_INSTRUCTION_RX 0x20 #define QANSEL_INSTRUCTION_RY 0x21 #define QANSEL_INSTRUCTION_RZ 0x22