QuEST_qasm.c
Go to the documentation of this file.
1 // Distributed under MIT licence. See https://github.com/QuEST-Kit/QuEST/blob/master/LICENCE.txt for details
2 
16 # include "QuEST.h"
17 # include "QuEST_precision.h"
18 # include "QuEST_internal.h"
19 # include "QuEST_qasm.h"
20 
21 # include <math.h>
22 # include <stdio.h>
23 # include <stdlib.h>
24 # include <stdarg.h>
25 # include <string.h>
26 
27 # define QUREG_LABEL "q" // QASM var-name for the quantum register
28 # define MESREG_LABEL "c" // QASM var-name for the classical measurement register
29 # define CTRL_LABEL_PREF "c" // QASM syntax which prefixes gates when controlled
30 # define MEASURE_CMD "measure" // QASM cmd for measurement operation
31 # define INIT_ZERO_CMD "reset" // QASM cmd for setting state 0
32 # define COMMENT_PREF "//" // QASM syntax for a comment ;)
33 
34 # define MAX_LINE_LEN 1024 // maximum length (#chars) of a single QASM instruction
35 # define BUF_INIT_SIZE 1024 // initial size of the QASM buffer (#chars)
36 # define BUF_GROW_FAC 2 // growth factor when buffer dynamically resizes
37 
38 static const char* qasmGateLabels[] = {
39  [GATE_SIGMA_X] = "x",
40  [GATE_SIGMA_Y] = "y",
41  [GATE_SIGMA_Z] = "z",
42  [GATE_T] = "t",
43  [GATE_S] = "s",
44  [GATE_HADAMARD] = "h",
45  [GATE_ROTATE_X] = "Rx",
46  [GATE_ROTATE_Y] = "Ry",
47  [GATE_ROTATE_Z] = "Rz",
48  [GATE_UNITARY] = "U", // needs phase fix when controlled
49  [GATE_PHASE_SHIFT] = "Rz",// needs phase fix when controlled
50  [GATE_SWAP] = "swap", // needs decomp into cNOTs?
51  [GATE_SQRT_SWAP] = "sqrtswap" // needs decomp into cNOTs and Rx(pi/2)?
52 };
53 
54 // @TODO make a proper internal error thing
55 void bufferOverflow(void) {
56  printf("!!!\nINTERNAL ERROR: QASM line buffer filled!\n!!!");
57  exit(1);
58 }
59 
60 void qasm_setup(Qureg* qureg) {
61 
62  // populate and attach QASM logger
63  QASMLogger *qasmLog = malloc(sizeof *qasmLog);
64  qureg->qasmLog = qasmLog;
65  if (qasmLog == NULL)
67 
68  qasmLog->isLogging = 0;
69  qasmLog->bufferSize = BUF_INIT_SIZE;
70  qasmLog->buffer = malloc(qasmLog->bufferSize * sizeof *(qasmLog->buffer));
71  if (qasmLog->buffer == NULL)
73 
74  // add headers and quantum / classical register creation
75  qasmLog->bufferFill = snprintf(
76  qasmLog->buffer, qasmLog->bufferSize,
77  "OPENQASM 2.0;\nqreg %s[%d];\ncreg %s[%d];\n",
80  if (qasmLog->bufferFill >= qasmLog->bufferSize)
82 }
83 
85  qureg.qasmLog->isLogging = 1;
86 }
87 
89  qureg.qasmLog->isLogging = 0;
90 }
91 
92 void addStringToQASM(Qureg qureg, char line[], int lineLen) {
93 
94  char* buf = qureg.qasmLog->buffer;
95  int bufSize = qureg.qasmLog->bufferSize;
96  int bufFill = qureg.qasmLog->bufferFill;
97 
98  // grow QASM buffer if necessary
99  if (lineLen + bufFill > bufSize) {
100 
101  int newBufSize = BUF_GROW_FAC * bufSize;
102  if (lineLen + bufFill > newBufSize)
103  bufferOverflow();
104 
105  char* newBuffer = malloc(newBufSize * sizeof *newBuffer);
106  sprintf(newBuffer, "%s", buf);
107  free(buf);
108 
109  qureg.qasmLog->bufferSize = newBufSize;
110  qureg.qasmLog->buffer = newBuffer;
111  bufSize = newBufSize;
112  buf = newBuffer;
113  }
114 
115  // add new str
116  int addedChars = snprintf(buf+bufFill, bufSize-bufFill, "%s", line);
117  qureg.qasmLog->bufferFill += addedChars;
118 }
119 
120 void qasm_recordComment(Qureg qureg, char* comment, ...) {
121 
122  if (!qureg.qasmLog->isLogging)
123  return;
124 
125  // write formatted comment to buff
126  va_list argp;
127  va_start(argp, comment);
128  char buff[MAX_LINE_LEN - 4];
129  vsnprintf(buff, MAX_LINE_LEN-5, comment, argp);
130  va_end(argp);
131 
132  // add chars to buff, write to QASM logger
133  char line[MAX_LINE_LEN + 1]; // for trailing \0
134  int len = snprintf(line, MAX_LINE_LEN, "%s %s\n", COMMENT_PREF, buff);
135  addStringToQASM(qureg, line, len);
136 }
137 
138 void addGateToQASM(Qureg qureg, TargetGate gate, int* controlQubits, int numControlQubits, int targetQubit, qreal* params, int numParams) {
139 
140  int len = 0;
141  char line[MAX_LINE_LEN + 1]; // for trailing \0
142 
143  // add control labels
144  for (int i=0; i < numControlQubits; i++)
145  len += snprintf(line+len, MAX_LINE_LEN-len, "%s", CTRL_LABEL_PREF);
146 
147  // add target gate
148  len += snprintf(line+len, MAX_LINE_LEN-len, "%s", qasmGateLabels[gate]);
149 
150  // add parameters
151  if (numParams > 0) {
152  len += snprintf(line+len, MAX_LINE_LEN-len, "(");
153  for (int i=0; i < numParams; i++) {
154  len += snprintf(line+len, MAX_LINE_LEN-len, REAL_QASM_FORMAT, params[i]);
155  if (i != numParams - 1)
156  len += snprintf(line+len, MAX_LINE_LEN-len, ",");
157  }
158  len += snprintf(line+len, MAX_LINE_LEN-len, ")");
159  }
160 
161  // add space
162  len += snprintf(line+len, MAX_LINE_LEN-len, " ");
163 
164  // add control qubits
165  for (int i=0; i < numControlQubits; i++)
166  len += snprintf(line+len, MAX_LINE_LEN-len, "%s[%d],", QUREG_LABEL, controlQubits[i]);
167 
168  // add target qubit, colon and newline
169  len += snprintf(line+len, MAX_LINE_LEN-len, "%s[%d];\n", QUREG_LABEL, targetQubit);
170 
171  // check whether we overflowed buffer
172  if (len >= MAX_LINE_LEN)
173  bufferOverflow();
174 
175  addStringToQASM(qureg, line, len);
176 }
177 
178 void qasm_recordGate(Qureg qureg, TargetGate gate, int targetQubit) {
179 
180  if (!qureg.qasmLog->isLogging)
181  return;
182 
183  addGateToQASM(qureg, gate, NULL, 0, targetQubit, NULL, 0);
184 }
185 
186 void qasm_recordParamGate(Qureg qureg, TargetGate gate, int targetQubit, qreal param) {
187 
188  if (!qureg.qasmLog->isLogging)
189  return;
190 
191  qreal params[1] = {param};
192  addGateToQASM(qureg, gate, NULL, 0, targetQubit, params, 1);
193 }
194 
195 void qasm_recordCompactUnitary(Qureg qureg, Complex alpha, Complex beta, int targetQubit) {
196 
197  if (!qureg.qasmLog->isLogging)
198  return;
199 
200  qreal rz2, ry, rz1;
201  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
202 
203  qreal params[3] = {rz2, ry, rz1};
204  addGateToQASM(qureg, GATE_UNITARY, NULL, 0, targetQubit, params, 3);
205 }
206 
207 void qasm_recordUnitary(Qureg qureg, ComplexMatrix2 u, int targetQubit) {
208 
209  if (!qureg.qasmLog->isLogging)
210  return;
211 
212  Complex alpha, beta;
213  qreal discardedGlobalPhase;
214  getComplexPairAndPhaseFromUnitary(u, &alpha, &beta, &discardedGlobalPhase);
215 
216  qreal rz2, ry, rz1;
217  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
218 
219  qreal params[3] = {rz2, ry, rz1};
220  addGateToQASM(qureg, GATE_UNITARY, NULL, 0, targetQubit, params, 3);
221 }
222 
223 void qasm_recordAxisRotation(Qureg qureg, qreal angle, Vector axis, int targetQubit) {
224 
225  if (!qureg.qasmLog->isLogging)
226  return;
227 
228  Complex alpha, beta;
229  getComplexPairFromRotation(angle, axis, &alpha, &beta);
230 
231  qreal rz2, ry, rz1;
232  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
233 
234  qreal params[3] = {rz2, ry, rz1};
235  addGateToQASM(qureg, GATE_UNITARY, NULL, 0, targetQubit, params, 3);
236 }
237 
238 void qasm_recordControlledGate(Qureg qureg, TargetGate gate, int controlQubit, int targetQubit) {
239 
240  if (!qureg.qasmLog->isLogging)
241  return;
242 
243  int controls[1] = {controlQubit};
244  addGateToQASM(qureg, gate, controls, 1, targetQubit, 0, 0);
245 }
246 
247 void qasm_recordControlledParamGate(Qureg qureg, TargetGate gate, int controlQubit, int targetQubit, qreal param) {
248 
249  if (!qureg.qasmLog->isLogging)
250  return;
251 
252  int controls[1] = {controlQubit};
253  qreal params[1] = {param};
254  addGateToQASM(qureg, gate, controls, 1, targetQubit, params, 1);
255 
256  // correct the global phase of controlled phase shifts
257  if (gate == GATE_PHASE_SHIFT) {
258  qasm_recordComment(qureg, "Restoring the discarded global phase of the previous controlled phase gate");
259  qreal phaseFix[1] = {param/2.0};
260  addGateToQASM(qureg, GATE_ROTATE_Z, NULL, 0, targetQubit, phaseFix, 1);
261  }
262 }
263 
264 void qasm_recordControlledCompactUnitary(Qureg qureg, Complex alpha, Complex beta, int controlQubit, int targetQubit) {
265 
266  if (!qureg.qasmLog->isLogging)
267  return;
268 
269  qreal rz2, ry, rz1;
270  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
271 
272  int controls[1] = {controlQubit};
273  qreal params[3] = {rz2, ry, rz1};
274  addGateToQASM(qureg, GATE_UNITARY, controls, 1, targetQubit, params, 3);
275 }
276 
278 void qasm_recordControlledUnitary(Qureg qureg, ComplexMatrix2 u, int controlQubit, int targetQubit) {
279 
280  if (!qureg.qasmLog->isLogging)
281  return;
282 
283  Complex alpha, beta;
284  qreal globalPhase;
285  getComplexPairAndPhaseFromUnitary(u, &alpha, &beta, &globalPhase);
286 
287  qreal rz2, ry, rz1;
288  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
289 
290  int controls[1] = {controlQubit};
291  qreal params[3] = {rz2, ry, rz1};
292  addGateToQASM(qureg, GATE_UNITARY, controls, 1, targetQubit, params, 3);
293 
294  // add Rz
295  qasm_recordComment(qureg, "Restoring the discarded global phase of the previous controlled unitary");
296  qreal phaseFix[1] = {globalPhase};
297  addGateToQASM(qureg, GATE_ROTATE_Z, NULL, 0, targetQubit, phaseFix, 1);
298 }
299 
300 void qasm_recordControlledAxisRotation(Qureg qureg, qreal angle, Vector axis, int controlQubit, int targetQubit) {
301 
302  if (!qureg.qasmLog->isLogging)
303  return;
304 
305  Complex alpha, beta;
306  getComplexPairFromRotation(angle, axis, &alpha, &beta);
307 
308  qreal rz2, ry, rz1;
309  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
310 
311  int controls[1] = {controlQubit};
312  qreal params[3] = {rz2, ry, rz1};
313  addGateToQASM(qureg, GATE_UNITARY, controls, 1, targetQubit, params, 3);
314 }
315 
316 void qasm_recordMultiControlledGate(Qureg qureg, TargetGate gate, int* controlQubits, int numControlQubits, int targetQubit) {
317 
318  if (!qureg.qasmLog->isLogging)
319  return;
320 
321  addGateToQASM(qureg, gate, controlQubits, numControlQubits, targetQubit, NULL, 0);
322 }
323 
324 void qasm_recordMultiControlledParamGate(Qureg qureg, TargetGate gate, int* controlQubits, int numControlQubits, int targetQubit, qreal param) {
325 
326  if (!qureg.qasmLog->isLogging)
327  return;
328 
329  qreal params[1] = {param};
330  addGateToQASM(qureg, gate, controlQubits, numControlQubits, targetQubit, params, 1);
331 
332  // correct the global phase of controlled phase shifts
333  if (gate == GATE_PHASE_SHIFT) {
334  qasm_recordComment(qureg, "Restoring the discarded global phase of the previous multicontrolled phase gate");
335  qreal phaseFix[1] = {param/2.0};
336  addGateToQASM(qureg, GATE_ROTATE_Z, NULL, 0, targetQubit, phaseFix, 1);
337  }
338 }
339 
341 void qasm_recordMultiControlledUnitary(Qureg qureg, ComplexMatrix2 u, int* controlQubits, int numControlQubits, int targetQubit) {
342 
343  if (!qureg.qasmLog->isLogging)
344  return;
345 
346  Complex alpha, beta;
347  qreal globalPhase;
348  getComplexPairAndPhaseFromUnitary(u, &alpha, &beta, &globalPhase);
349 
350  qreal rz2, ry, rz1;
351  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
352 
353  qreal params[3] = {rz2, ry, rz1};
354  addGateToQASM(qureg, GATE_UNITARY, controlQubits, numControlQubits, targetQubit, params, 3);
355 
356  // add Rz
357  qasm_recordComment(qureg, "Restoring the discarded global phase of the previous multicontrolled unitary");
358  qreal phaseFix[1] = {globalPhase};
359  addGateToQASM(qureg, GATE_ROTATE_Z, NULL, 0, targetQubit, phaseFix, 1);
360 }
361 
363  Qureg qureg, ComplexMatrix2 u, int* controlQubits, int* controlState, int numControlQubits, int targetQubit
364 ) {
365  if (!qureg.qasmLog->isLogging)
366  return;
367 
368  qasm_recordComment(qureg, "NOTing some gates so that the subsequent unitary is controlled-on-0");
369  for (int i=0; i < numControlQubits; i++)
370  if (controlState[i] == 0)
371  addGateToQASM(qureg, GATE_SIGMA_X, NULL, 0, controlQubits[i], NULL, 0);
372 
373  qasm_recordMultiControlledUnitary(qureg, u, controlQubits, numControlQubits, targetQubit);
374 
375  qasm_recordComment(qureg, "Undoing the NOTing of the controlled-on-0 qubits of the previous unitary");
376  for (int i=0; i < numControlQubits; i++)
377  if (controlState[i] == 0)
378  addGateToQASM(qureg, GATE_SIGMA_X, NULL, 0, controlQubits[i], NULL, 0);
379 }
380 
381 /* not actually used, D'Oh!
382 void qasm_recordMultiControlledAxisRotation(Qureg qureg, qreal angle, Vector axis, int* controlQubits, int numControlQubits, int targetQubit) {
383 
384  if (!qureg.qasmLog->isLogging)
385  return;
386 
387  Complex alpha, beta;
388  getComplexPairFromRotation(angle, axis, &alpha, &beta);
389 
390  qreal rz2, ry, rz1;
391  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
392 
393  qreal params[3] = {rz2, ry, rz1};
394  addGateToQASM(qureg, GATE_UNITARY, controlQubits, numControlQubits, targetQubit, params, 3);
395 }
396 */
397 
398 void qasm_recordMeasurement(Qureg qureg, int measureQubit) {
399 
400  if (!qureg.qasmLog->isLogging)
401  return;
402 
403  char line[MAX_LINE_LEN + 1]; // for trailing \0
404  int len = snprintf(
405  line, MAX_LINE_LEN, "%s %s[%d] -> %s[%d];\n",
406  MEASURE_CMD, QUREG_LABEL, measureQubit, MESREG_LABEL, measureQubit);
407 
408  // check whether we overflowed buffer
409  if (len >= MAX_LINE_LEN)
410  bufferOverflow();
411 
412  addStringToQASM(qureg, line, len);
413 }
414 
416 
417  if (!qureg.qasmLog->isLogging)
418  return;
419 
420  char line[MAX_LINE_LEN + 1]; // for trailing \0
421  int len = snprintf(line, MAX_LINE_LEN, "%s %s;\n", INIT_ZERO_CMD, QUREG_LABEL);
422 
423  // check whether we overflowed buffer
424  if (len >= MAX_LINE_LEN)
425  bufferOverflow();
426 
427  addStringToQASM(qureg, line, len);
428 }
429 
431 
432  if (!qureg.qasmLog->isLogging)
433  return;
434 
435  // add an explanatory comment
436  char buf[MAX_LINE_LEN+1];
437  sprintf(buf, "Initialising state |+>");
438  qasm_recordComment(qureg, buf);
439 
440  // it's valid QASM to h the register (I think)
441  // |+> = H |0>
442  qasm_recordInitZero(qureg);
443  int charsWritten = snprintf(
444  buf, MAX_LINE_LEN, "%s %s;\n",
446  if (charsWritten >= MAX_LINE_LEN)
447  bufferOverflow();
448  addStringToQASM(qureg, buf, charsWritten);
449 
450  // old code (before above QASM shortcut)
451  /*
452  qasm_recordInitZero(qureg);
453  for (int q=0; q < qureg.numQubitsRepresented; q++)
454  qasm_recordGate(qureg, GATE_HADAMARD, q);
455  */
456 }
457 
458 void qasm_recordInitClassical(Qureg qureg, long long int stateInd) {
459 
460  if (!qureg.qasmLog->isLogging)
461  return;
462 
463  // add an explanatory comment
464  char cmt[MAX_LINE_LEN+1];
465  sprintf(cmt, "Initialising state |%lld>", stateInd);
466  qasm_recordComment(qureg, cmt);
467 
468  // start in |0>
469  qasm_recordInitZero(qureg);
470 
471  // NOT the 1 bits in stateInd
472  for (int q=0; q < qureg.numQubitsRepresented; q++)
473  if ((stateInd >> q) & 1)
474  qasm_recordGate(qureg, GATE_SIGMA_X, q);
475 }
476 
478 
479  // maintains current buffer size
480  (qureg.qasmLog->buffer)[0] = '\0';
481  qureg.qasmLog->bufferFill = 0;
482 }
483 
485  printf("%s", qureg.qasmLog->buffer);
486 }
487 
489 int qasm_writeRecordedToFile(Qureg qureg, char* filename) {
490 
491  FILE *file = fopen(filename, "w");
492  if (file == NULL)
493  return 0;
494 
495  fprintf(file, "%s", qureg.qasmLog->buffer);
496  fclose(file);
497  return 1;
498 }
499 
500 void qasm_free(Qureg qureg) {
501 
502  free(qureg.qasmLog->buffer);
503  free(qureg.qasmLog);
504 }
void qasm_printRecorded(Qureg qureg)
Definition: QuEST_qasm.c:484
Represents a 3-vector of real numbers.
Definition: QuEST.h:148
#define MEASURE_CMD
Definition: QuEST_qasm.c:30
#define MAX_LINE_LEN
Definition: QuEST_qasm.c:34
void qasm_recordParamGate(Qureg qureg, TargetGate gate, int targetQubit, qreal param)
Definition: QuEST_qasm.c:186
#define MESREG_LABEL
Definition: QuEST_qasm.c:28
void qasm_free(Qureg qureg)
Definition: QuEST_qasm.c:500
@ GATE_T
Definition: QuEST_qasm.h:24
@ GATE_PHASE_SHIFT
Definition: QuEST_qasm.h:32
void qasm_recordUnitary(Qureg qureg, ComplexMatrix2 u, int targetQubit)
Definition: QuEST_qasm.c:207
void qasm_recordInitZero(Qureg qureg)
Definition: QuEST_qasm.c:415
void qasm_recordMultiControlledGate(Qureg qureg, TargetGate gate, int *controlQubits, int numControlQubits, int targetQubit)
Definition: QuEST_qasm.c:316
void qasm_clearRecorded(Qureg qureg)
Definition: QuEST_qasm.c:477
void qasm_recordInitPlus(Qureg qureg)
Definition: QuEST_qasm.c:430
@ GATE_ROTATE_X
Definition: QuEST_qasm.h:27
void qasm_recordControlledCompactUnitary(Qureg qureg, Complex alpha, Complex beta, int controlQubit, int targetQubit)
Definition: QuEST_qasm.c:264
@ GATE_ROTATE_Z
Definition: QuEST_qasm.h:29
@ GATE_SIGMA_Z
Definition: QuEST_qasm.h:23
@ GATE_HADAMARD
Definition: QuEST_qasm.h:26
void qasm_startRecording(Qureg qureg)
Definition: QuEST_qasm.c:84
void getComplexPairFromRotation(qreal angle, Vector axis, Complex *alpha, Complex *beta)
Definition: QuEST_common.c:114
#define qreal
void qasm_recordMultiStateControlledUnitary(Qureg qureg, ComplexMatrix2 u, int *controlQubits, int *controlState, int numControlQubits, int targetQubit)
Definition: QuEST_qasm.c:362
void qasm_recordControlledGate(Qureg qureg, TargetGate gate, int controlQubit, int targetQubit)
Definition: QuEST_qasm.c:238
void qasm_recordAxisRotation(Qureg qureg, qreal angle, Vector axis, int targetQubit)
Definition: QuEST_qasm.c:223
@ GATE_SQRT_SWAP
Definition: QuEST_qasm.h:34
@ GATE_SIGMA_X
Definition: QuEST_qasm.h:21
void qasm_recordMeasurement(Qureg qureg, int measureQubit)
Definition: QuEST_qasm.c:398
void qasm_stopRecording(Qureg qureg)
Definition: QuEST_qasm.c:88
#define INIT_ZERO_CMD
Definition: QuEST_qasm.c:31
void qasm_recordInitClassical(Qureg qureg, long long int stateInd)
Definition: QuEST_qasm.c:458
int qasm_writeRecordedToFile(Qureg qureg, char *filename)
returns success of file write
Definition: QuEST_qasm.c:489
void qasm_recordControlledParamGate(Qureg qureg, TargetGate gate, int controlQubit, int targetQubit, qreal param)
Definition: QuEST_qasm.c:247
void qasm_recordControlledAxisRotation(Qureg qureg, qreal angle, Vector axis, int controlQubit, int targetQubit)
Definition: QuEST_qasm.c:300
@ GATE_UNITARY
Definition: QuEST_qasm.h:31
static const char * qasmGateLabels[]
Definition: QuEST_qasm.c:38
void qasm_recordComment(Qureg qureg, char *comment,...)
Definition: QuEST_qasm.c:120
QASMLogger * qasmLog
Storage for generated QASM output.
Definition: QuEST.h:232
void bufferOverflow(void)
Definition: QuEST_qasm.c:55
void addStringToQASM(Qureg qureg, char line[], int lineLen)
Definition: QuEST_qasm.c:92
#define QUREG_LABEL
TODO.
Definition: QuEST_qasm.c:27
#define COMMENT_PREF
Definition: QuEST_qasm.c:32
void qasm_recordMultiControlledUnitary(Qureg qureg, ComplexMatrix2 u, int *controlQubits, int numControlQubits, int targetQubit)
additionally performs Rz on target to restore the global phase lost from u in QASM U(a,...
Definition: QuEST_qasm.c:341
Represents a system of qubits.
Definition: QuEST.h:203
void qasm_recordMultiControlledParamGate(Qureg qureg, TargetGate gate, int *controlQubits, int numControlQubits, int targetQubit, qreal param)
Definition: QuEST_qasm.c:324
#define BUF_INIT_SIZE
Definition: QuEST_qasm.c:35
void getZYZRotAnglesFromComplexPair(Complex alpha, Complex beta, qreal *rz2, qreal *ry, qreal *rz1)
maps U(alpha, beta) to Rz(rz2) Ry(ry) Rz(rz1)
Definition: QuEST_common.c:124
#define CTRL_LABEL_PREF
Definition: QuEST_qasm.c:29
void addGateToQASM(Qureg qureg, TargetGate gate, int *controlQubits, int numControlQubits, int targetQubit, qreal *params, int numParams)
Definition: QuEST_qasm.c:138
#define BUF_GROW_FAC
Definition: QuEST_qasm.c:36
int numQubitsRepresented
The number of qubits represented in either the state-vector or density matrix.
Definition: QuEST.h:208
@ GATE_S
Definition: QuEST_qasm.h:25
@ GATE_SWAP
Definition: QuEST_qasm.h:33
TargetGate
! Identifiers of single-target gates
Definition: QuEST_qasm.h:20
@ GATE_SIGMA_Y
Definition: QuEST_qasm.h:22
void qasm_recordCompactUnitary(Qureg qureg, Complex alpha, Complex beta, int targetQubit)
Definition: QuEST_qasm.c:195
void qasm_recordControlledUnitary(Qureg qureg, ComplexMatrix2 u, int controlQubit, int targetQubit)
additionally performs Rz on target to restore the global phase lost from u in QASM U(a,...
Definition: QuEST_qasm.c:278
Represents one complex number.
Definition: QuEST.h:103
void qasm_setup(Qureg *qureg)
Definition: QuEST_qasm.c:60
void qasm_recordGate(Qureg qureg, TargetGate gate, int targetQubit)
Definition: QuEST_qasm.c:178
@ GATE_ROTATE_Y
Definition: QuEST_qasm.h:28
Represents a 2x2 matrix of complex numbers.
Definition: QuEST.h:114
void getComplexPairAndPhaseFromUnitary(ComplexMatrix2 u, Complex *alpha, Complex *beta, qreal *globalPhase)
maps U(r0c0, r0c1, r1c0, r1c1) to exp(i globalPhase) U(alpha, beta)
Definition: QuEST_common.c:136