QuEST_qasm.c File Reference
#include "QuEST.h"
#include "QuEST_precision.h"
#include "QuEST_internal.h"
#include "QuEST_qasm.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

Go to the source code of this file.

Macros

#define BUF_GROW_FAC   2
 
#define BUF_INIT_SIZE   1024
 
#define COMMENT_PREF   "//"
 
#define CTRL_LABEL_PREF   "c"
 
#define INIT_ZERO_CMD   "reset"
 
#define MAX_LINE_LEN   1024
 
#define MAX_REG_SYMBS   24
 
#define MEASURE_CMD   "measure"
 
#define MESREG_LABEL   "c"
 
#define QUREG_LABEL   "q"
 TODO. More...
 

Functions

void addGateToQASM (Qureg qureg, TargetGate gate, int *controlQubits, int numControlQubits, int targetQubit, qreal *params, int numParams)
 
void addMultiVarOverridesToQASM (Qureg qureg, int numRegs, long long int *overrideInds, qreal *overridePhases, int numOverrides)
 
void addMultiVarRegsToQASM (Qureg qureg, int *qubits, int *numQubitsPerReg, int numRegs, enum bitEncoding encoding)
 
void addShiftValuesToQASM (Qureg qureg, enum phaseFunc funcName, int numRegs, qreal *params)
 
void addStringToQASM (Qureg qureg, char line[], int lineLen)
 
void bufferOverflow (void)
 
char getPhaseFuncSymbol (int numSymbs, int ind)
 
void qasm_clearRecorded (Qureg qureg)
 
void qasm_free (Qureg qureg)
 
void qasm_printRecorded (Qureg qureg)
 
void qasm_recordAxisRotation (Qureg qureg, qreal angle, Vector axis, int targetQubit)
 
void qasm_recordComment (Qureg qureg, char *comment,...)
 
void qasm_recordCompactUnitary (Qureg qureg, Complex alpha, Complex beta, int targetQubit)
 
void qasm_recordControlledAxisRotation (Qureg qureg, qreal angle, Vector axis, int controlQubit, int targetQubit)
 
void qasm_recordControlledCompactUnitary (Qureg qureg, Complex alpha, Complex beta, int controlQubit, int targetQubit)
 
void qasm_recordControlledGate (Qureg qureg, TargetGate gate, int controlQubit, int targetQubit)
 
void qasm_recordControlledParamGate (Qureg qureg, TargetGate gate, int controlQubit, int targetQubit, qreal param)
 
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,b,c) More...
 
void qasm_recordGate (Qureg qureg, TargetGate gate, int targetQubit)
 
void qasm_recordInitClassical (Qureg qureg, long long int stateInd)
 
void qasm_recordInitPlus (Qureg qureg)
 
void qasm_recordInitZero (Qureg qureg)
 
void qasm_recordMeasurement (Qureg qureg, int measureQubit)
 
void qasm_recordMultiControlledGate (Qureg qureg, TargetGate gate, int *controlQubits, int numControlQubits, int targetQubit)
 
void qasm_recordMultiControlledMultiQubitNot (Qureg qureg, int *ctrls, int numCtrls, int *targs, int numTargs)
 
void qasm_recordMultiControlledParamGate (Qureg qureg, TargetGate gate, int *controlQubits, int numControlQubits, int targetQubit, qreal param)
 
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,b,c) More...
 
void qasm_recordMultiStateControlledUnitary (Qureg qureg, ComplexMatrix2 u, int *controlQubits, int *controlState, int numControlQubits, int targetQubit)
 
void qasm_recordMultiVarPhaseFunc (Qureg qureg, int *qubits, int *numQubitsPerReg, int numRegs, enum bitEncoding encoding, qreal *coeffs, qreal *exponents, int *numTermsPerReg, long long int *overrideInds, qreal *overridePhases, int numOverrides)
 
void qasm_recordNamedPhaseFunc (Qureg qureg, int *qubits, int *numQubitsPerReg, int numRegs, enum bitEncoding encoding, enum phaseFunc funcName, qreal *params, int numParams, long long int *overrideInds, qreal *overridePhases, int numOverrides)
 
void qasm_recordParamGate (Qureg qureg, TargetGate gate, int targetQubit, qreal param)
 
void qasm_recordPhaseFunc (Qureg qureg, int *qubits, int numQubits, enum bitEncoding encoding, qreal *coeffs, qreal *exponents, int numTerms, long long int *overrideInds, qreal *overridePhases, int numOverrides)
 
void qasm_recordUnitary (Qureg qureg, ComplexMatrix2 u, int targetQubit)
 
void qasm_setup (Qureg *qureg)
 
void qasm_startRecording (Qureg qureg)
 
void qasm_stopRecording (Qureg qureg)
 
int qasm_writeRecordedToFile (Qureg qureg, char *filename)
 returns success of file write More...
 

Variables

static const char * qasmGateLabels []
 

Detailed Description

Functions for generating QASM output from QuEST circuits

Author
Tyson Jones

Definition in file QuEST_qasm.c.

Macro Definition Documentation

◆ BUF_GROW_FAC

#define BUF_GROW_FAC   2

Definition at line 36 of file QuEST_qasm.c.

◆ BUF_INIT_SIZE

#define BUF_INIT_SIZE   1024

Definition at line 35 of file QuEST_qasm.c.

◆ COMMENT_PREF

#define COMMENT_PREF   "//"

Definition at line 32 of file QuEST_qasm.c.

◆ CTRL_LABEL_PREF

#define CTRL_LABEL_PREF   "c"

Definition at line 29 of file QuEST_qasm.c.

◆ INIT_ZERO_CMD

#define INIT_ZERO_CMD   "reset"

Definition at line 31 of file QuEST_qasm.c.

◆ MAX_LINE_LEN

#define MAX_LINE_LEN   1024

Definition at line 34 of file QuEST_qasm.c.

◆ MAX_REG_SYMBS

#define MAX_REG_SYMBS   24

Definition at line 37 of file QuEST_qasm.c.

◆ MEASURE_CMD

#define MEASURE_CMD   "measure"

Definition at line 30 of file QuEST_qasm.c.

◆ MESREG_LABEL

#define MESREG_LABEL   "c"

Definition at line 28 of file QuEST_qasm.c.

◆ QUREG_LABEL

#define QUREG_LABEL   "q"

TODO.

  • allow user-set decimal precision (useful for when QASM is passed to a plotter)
  • sort out fixing global phase in controlledPhaseShift to controlledRotateZ plug
  • add functions to directly add comments to QASM by user
  • add abilitiy for user to directly add strings to QASM buffer??

Definition at line 27 of file QuEST_qasm.c.

Function Documentation

◆ addGateToQASM()

void addGateToQASM ( Qureg  qureg,
TargetGate  gate,
int *  controlQubits,
int  numControlQubits,
int  targetQubit,
qreal params,
int  numParams 
)

Definition at line 139 of file QuEST_qasm.c.

139  {
140 
141  int len = 0;
142  char line[MAX_LINE_LEN + 1]; // for trailing \0
143 
144  // add control labels
145  for (int i=0; i < numControlQubits; i++)
146  len += snprintf(line+len, MAX_LINE_LEN-len, "%s", CTRL_LABEL_PREF);
147 
148  // add target gate
149  len += snprintf(line+len, MAX_LINE_LEN-len, "%s", qasmGateLabels[gate]);
150 
151  // add parameters
152  if (numParams > 0) {
153  len += snprintf(line+len, MAX_LINE_LEN-len, "(");
154  for (int i=0; i < numParams; i++) {
155  len += snprintf(line+len, MAX_LINE_LEN-len, REAL_QASM_FORMAT, params[i]);
156  if (i != numParams - 1)
157  len += snprintf(line+len, MAX_LINE_LEN-len, ",");
158  }
159  len += snprintf(line+len, MAX_LINE_LEN-len, ")");
160  }
161 
162  // add space
163  len += snprintf(line+len, MAX_LINE_LEN-len, " ");
164 
165  // add control qubits
166  for (int i=0; i < numControlQubits; i++)
167  len += snprintf(line+len, MAX_LINE_LEN-len, "%s[%d],", QUREG_LABEL, controlQubits[i]);
168 
169  // add target qubit, colon and newline
170  len += snprintf(line+len, MAX_LINE_LEN-len, "%s[%d];\n", QUREG_LABEL, targetQubit);
171 
172  // check whether we overflowed buffer
173  if (len >= MAX_LINE_LEN)
174  bufferOverflow();
175 
176  addStringToQASM(qureg, line, len);
177 }

References addStringToQASM(), bufferOverflow(), CTRL_LABEL_PREF, MAX_LINE_LEN, qasmGateLabels, and QUREG_LABEL.

Referenced by qasm_recordAxisRotation(), qasm_recordCompactUnitary(), qasm_recordControlledAxisRotation(), qasm_recordControlledCompactUnitary(), qasm_recordControlledGate(), qasm_recordControlledParamGate(), qasm_recordControlledUnitary(), qasm_recordGate(), qasm_recordMultiControlledGate(), qasm_recordMultiControlledMultiQubitNot(), qasm_recordMultiControlledParamGate(), qasm_recordMultiControlledUnitary(), qasm_recordMultiStateControlledUnitary(), qasm_recordParamGate(), and qasm_recordUnitary().

◆ addMultiVarOverridesToQASM()

void addMultiVarOverridesToQASM ( Qureg  qureg,
int  numRegs,
long long int *  overrideInds,
qreal overridePhases,
int  numOverrides 
)

Definition at line 600 of file QuEST_qasm.c.

600  {
601 
602  qasm_recordComment(qureg, " though with overrides");
603 
604  char line[MAX_LINE_LEN+1];
605  int len = 0;
606 
607  // record like:
608  // |x=0, y=1, z=2> -> exp(i .45)
609  // |x=0, y=1, z=5> -> exp(i (-.5))
610  int vInd=0;
611  for (int v=0; v<numOverrides; v++) {
612  len = 0;
613  len += snprintf(line+len, MAX_LINE_LEN-len, "// |");
614  for (int r=0; r<numRegs; r++) {
615  if (numRegs <= MAX_REG_SYMBS)
616  len += snprintf(line+len, MAX_LINE_LEN-len,
617  (r<numRegs-1)?
618  "%c=%lld, " :
619  "%c=%lld>",
620  getPhaseFuncSymbol(numRegs,r),
621  overrideInds[vInd++]);
622  else
623  len += snprintf(line+len, MAX_LINE_LEN-len,
624  (r<numRegs-1)?
625  "x%d=%lld, " :
626  "x%d=%lld>",
627  r,
628  overrideInds[vInd++]);
629  }
630  len += snprintf(line+len, MAX_LINE_LEN-len,
631  (overridePhases[v] >= 0)?
632  " -> exp(i " REAL_QASM_FORMAT ")\n" :
633  " -> exp(i (" REAL_QASM_FORMAT "))\n",
634  overridePhases[v]);
635 
636  if (len >= MAX_LINE_LEN)
637  bufferOverflow();
638  addStringToQASM(qureg, line, len);
639  }
640 }

References addStringToQASM(), bufferOverflow(), getPhaseFuncSymbol(), MAX_LINE_LEN, MAX_REG_SYMBS, and qasm_recordComment().

Referenced by qasm_recordMultiVarPhaseFunc(), and qasm_recordNamedPhaseFunc().

◆ addMultiVarRegsToQASM()

void addMultiVarRegsToQASM ( Qureg  qureg,
int *  qubits,
int *  numQubitsPerReg,
int  numRegs,
enum bitEncoding  encoding 
)

Definition at line 571 of file QuEST_qasm.c.

571  {
572 
573  char encBuf[MAX_LINE_LEN];
574  if (encoding == UNSIGNED) sprintf(encBuf, "an unsigned");
575  if (encoding == TWOS_COMPLEMENT) sprintf(encBuf, "a two's complement");
576  qasm_recordComment(qureg, " upon substates informed by qubits (under %s binary encoding)", encBuf);
577 
578  char line[MAX_LINE_LEN+1];
579  int len = 0;
580 
581  // record like:
582  // |x> = {0, 3, 2}
583  // |y> = {1, 2}
584  int qInd = 0;
585  for (int r=0; r<numRegs; r++) {
586  len = 0;
587  if (numRegs <= MAX_REG_SYMBS)
588  len += snprintf(line+len, MAX_LINE_LEN-len, "// |%c> = {", getPhaseFuncSymbol(numRegs,r));
589  else
590  len += snprintf(line+len, MAX_LINE_LEN-len, "// |x%d> = {", r);
591  for (int q=0; q<numQubitsPerReg[r]; q++)
592  len += snprintf(line+len, MAX_LINE_LEN-len, (q < numQubitsPerReg[r]-1)? "%d, ":"%d}\n", qubits[qInd++]);
593 
594  if (len >= MAX_LINE_LEN)
595  bufferOverflow();
596  addStringToQASM(qureg, line, len);
597  }
598 }

References addStringToQASM(), bufferOverflow(), getPhaseFuncSymbol(), MAX_LINE_LEN, MAX_REG_SYMBS, qasm_recordComment(), TWOS_COMPLEMENT, and UNSIGNED.

Referenced by qasm_recordMultiVarPhaseFunc(), and qasm_recordNamedPhaseFunc().

◆ addShiftValuesToQASM()

void addShiftValuesToQASM ( Qureg  qureg,
enum phaseFunc  funcName,
int  numRegs,
qreal params 
)

Definition at line 642 of file QuEST_qasm.c.

642  {
643 
644  char line[MAX_LINE_LEN+1];
645  int len = 0;
646  int numDeltas;
647  if (funcName == SCALED_INVERSE_SHIFTED_NORM)
648  numDeltas = numRegs;
649  else if (funcName == SCALED_INVERSE_SHIFTED_DISTANCE)
650  numDeltas = numRegs / 2;
651  else
652  return;
653 
654  qasm_recordComment(qureg, " with the additional parameters");
655 
656  for (int k=0; k<numDeltas; k++) {
657  len = 0;
658  len += snprintf(line+len, MAX_LINE_LEN-len, "// delta%d = " REAL_QASM_FORMAT "\n", k, params[2+k]);
659  if (len >= MAX_LINE_LEN)
660  bufferOverflow();
661  addStringToQASM(qureg, line, len);
662  }
663 
664 }

References addStringToQASM(), bufferOverflow(), MAX_LINE_LEN, qasm_recordComment(), SCALED_INVERSE_SHIFTED_DISTANCE, and SCALED_INVERSE_SHIFTED_NORM.

Referenced by qasm_recordNamedPhaseFunc().

◆ addStringToQASM()

void addStringToQASM ( Qureg  qureg,
char  line[],
int  lineLen 
)

Definition at line 93 of file QuEST_qasm.c.

93  {
94 
95  char* buf = qureg.qasmLog->buffer;
96  int bufSize = qureg.qasmLog->bufferSize;
97  int bufFill = qureg.qasmLog->bufferFill;
98 
99  // grow QASM buffer if necessary
100  if (lineLen + bufFill > bufSize) {
101 
102  int newBufSize = BUF_GROW_FAC * bufSize;
103  if (lineLen + bufFill > newBufSize)
104  bufferOverflow();
105 
106  char* newBuffer = malloc(newBufSize * sizeof *newBuffer);
107  sprintf(newBuffer, "%s", buf);
108  free(buf);
109 
110  qureg.qasmLog->bufferSize = newBufSize;
111  qureg.qasmLog->buffer = newBuffer;
112  bufSize = newBufSize;
113  buf = newBuffer;
114  }
115 
116  // add new str
117  int addedChars = snprintf(buf+bufFill, bufSize-bufFill, "%s", line);
118  qureg.qasmLog->bufferFill += addedChars;
119 }

References BUF_GROW_FAC, bufferOverflow(), and Qureg::qasmLog.

Referenced by addGateToQASM(), addMultiVarOverridesToQASM(), addMultiVarRegsToQASM(), addShiftValuesToQASM(), qasm_recordComment(), qasm_recordInitPlus(), qasm_recordInitZero(), qasm_recordMeasurement(), qasm_recordMultiVarPhaseFunc(), qasm_recordNamedPhaseFunc(), and qasm_recordPhaseFunc().

◆ bufferOverflow()

void bufferOverflow ( void  )

◆ getPhaseFuncSymbol()

char getPhaseFuncSymbol ( int  numSymbs,
int  ind 
)

Definition at line 556 of file QuEST_qasm.c.

556  {
557 
558  static char xyz[7] = {'x', 'y', 'z', 't', 'r', 'v', 'u'};
559  if (numSymbs <= 7)
560  return xyz[ind];
561 
562  static char abc[MAX_REG_SYMBS] = {'a','b','c','d','e','f','g','h','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z'}; // no i or o
563  if (numSymbs <= MAX_REG_SYMBS)
564  return abc[ind];
565 
566  // we should never reach here, since caller should handle when numSymbs > 24
567  bufferOverflow();
568  return 'x';
569 }

References bufferOverflow(), and MAX_REG_SYMBS.

Referenced by addMultiVarOverridesToQASM(), addMultiVarRegsToQASM(), qasm_recordMultiVarPhaseFunc(), and qasm_recordNamedPhaseFunc().

◆ qasm_clearRecorded()

void qasm_clearRecorded ( Qureg  qureg)

Definition at line 864 of file QuEST_qasm.c.

864  {
865 
866  // maintains current buffer size
867  (qureg.qasmLog->buffer)[0] = '\0';
868  qureg.qasmLog->bufferFill = 0;
869 }

References Qureg::qasmLog.

Referenced by clearRecordedQASM().

◆ qasm_free()

void qasm_free ( Qureg  qureg)

Definition at line 887 of file QuEST_qasm.c.

887  {
888 
889  free(qureg.qasmLog->buffer);
890  free(qureg.qasmLog);
891 }

References Qureg::qasmLog.

Referenced by destroyQureg().

◆ qasm_printRecorded()

void qasm_printRecorded ( Qureg  qureg)

Definition at line 871 of file QuEST_qasm.c.

871  {
872  printf("%s", qureg.qasmLog->buffer);
873 }

References Qureg::qasmLog.

Referenced by printRecordedQASM().

◆ qasm_recordAxisRotation()

void qasm_recordAxisRotation ( Qureg  qureg,
qreal  angle,
Vector  axis,
int  targetQubit 
)

Definition at line 224 of file QuEST_qasm.c.

224  {
225 
226  if (!qureg.qasmLog->isLogging)
227  return;
228 
229  Complex alpha, beta;
230  getComplexPairFromRotation(angle, axis, &alpha, &beta);
231 
232  qreal rz2, ry, rz1;
233  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
234 
235  qreal params[3] = {rz2, ry, rz1};
236  addGateToQASM(qureg, GATE_UNITARY, NULL, 0, targetQubit, params, 3);
237 }

References addGateToQASM(), GATE_UNITARY, getComplexPairFromRotation(), getZYZRotAnglesFromComplexPair(), Qureg::qasmLog, and qreal.

Referenced by rotateAroundAxis().

◆ qasm_recordComment()

void qasm_recordComment ( Qureg  qureg,
char *  comment,
  ... 
)

Definition at line 121 of file QuEST_qasm.c.

121  {
122 
123  if (!qureg.qasmLog->isLogging)
124  return;
125 
126  // write formatted comment to buff
127  va_list argp;
128  va_start(argp, comment);
129  char buff[MAX_LINE_LEN - 4];
130  vsnprintf(buff, MAX_LINE_LEN-5, comment, argp);
131  va_end(argp);
132 
133  // add chars to buff, write to QASM logger
134  char line[MAX_LINE_LEN + 1]; // for trailing \0
135  int len = snprintf(line, MAX_LINE_LEN, "%s %s\n", COMMENT_PREF, buff);
136  addStringToQASM(qureg, line, len);
137 }

References addStringToQASM(), COMMENT_PREF, MAX_LINE_LEN, and Qureg::qasmLog.

Referenced by addMultiVarOverridesToQASM(), addMultiVarRegsToQASM(), addShiftValuesToQASM(), applyDiagonalOp(), applyExponentiatedPauliHamil(), applyFullQFT(), applyMatrix2(), applyMatrix4(), applyMatrixN(), applyMultiControlledMatrixN(), applyPauliHamil(), applyPauliSum(), applyProjector(), applyQFT(), applyTrotterCircuit(), controlledMultiQubitUnitary(), controlledTwoQubitUnitary(), initBlankState(), initPureState(), initStateFromAmps(), mixDephasing(), mixDepolarising(), mixKrausMap(), mixMultiQubitKrausMap(), mixPauli(), mixTwoQubitDephasing(), mixTwoQubitDepolarising(), mixTwoQubitKrausMap(), multiControlledMultiQubitUnitary(), multiControlledMultiRotatePauli(), multiControlledMultiRotateZ(), multiControlledTwoQubitUnitary(), multiQubitUnitary(), multiRotatePauli(), multiRotateZ(), qasm_recordControlledParamGate(), qasm_recordControlledUnitary(), qasm_recordInitClassical(), qasm_recordInitPlus(), qasm_recordMultiControlledMultiQubitNot(), qasm_recordMultiControlledParamGate(), qasm_recordMultiControlledUnitary(), qasm_recordMultiStateControlledUnitary(), qasm_recordMultiVarPhaseFunc(), qasm_recordNamedPhaseFunc(), qasm_recordPhaseFunc(), setAmps(), setDensityAmps(), setWeightedQureg(), and twoQubitUnitary().

◆ qasm_recordCompactUnitary()

void qasm_recordCompactUnitary ( Qureg  qureg,
Complex  alpha,
Complex  beta,
int  targetQubit 
)

Definition at line 196 of file QuEST_qasm.c.

196  {
197 
198  if (!qureg.qasmLog->isLogging)
199  return;
200 
201  qreal rz2, ry, rz1;
202  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
203 
204  qreal params[3] = {rz2, ry, rz1};
205  addGateToQASM(qureg, GATE_UNITARY, NULL, 0, targetQubit, params, 3);
206 }

References addGateToQASM(), GATE_UNITARY, getZYZRotAnglesFromComplexPair(), Qureg::qasmLog, and qreal.

Referenced by compactUnitary().

◆ qasm_recordControlledAxisRotation()

void qasm_recordControlledAxisRotation ( Qureg  qureg,
qreal  angle,
Vector  axis,
int  controlQubit,
int  targetQubit 
)

Definition at line 301 of file QuEST_qasm.c.

301  {
302 
303  if (!qureg.qasmLog->isLogging)
304  return;
305 
306  Complex alpha, beta;
307  getComplexPairFromRotation(angle, axis, &alpha, &beta);
308 
309  qreal rz2, ry, rz1;
310  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
311 
312  int controls[1] = {controlQubit};
313  qreal params[3] = {rz2, ry, rz1};
314  addGateToQASM(qureg, GATE_UNITARY, controls, 1, targetQubit, params, 3);
315 }

References addGateToQASM(), GATE_UNITARY, getComplexPairFromRotation(), getZYZRotAnglesFromComplexPair(), Qureg::qasmLog, and qreal.

Referenced by controlledRotateAroundAxis().

◆ qasm_recordControlledCompactUnitary()

void qasm_recordControlledCompactUnitary ( Qureg  qureg,
Complex  alpha,
Complex  beta,
int  controlQubit,
int  targetQubit 
)

Definition at line 265 of file QuEST_qasm.c.

265  {
266 
267  if (!qureg.qasmLog->isLogging)
268  return;
269 
270  qreal rz2, ry, rz1;
271  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
272 
273  int controls[1] = {controlQubit};
274  qreal params[3] = {rz2, ry, rz1};
275  addGateToQASM(qureg, GATE_UNITARY, controls, 1, targetQubit, params, 3);
276 }

References addGateToQASM(), GATE_UNITARY, getZYZRotAnglesFromComplexPair(), Qureg::qasmLog, and qreal.

Referenced by controlledCompactUnitary().

◆ qasm_recordControlledGate()

void qasm_recordControlledGate ( Qureg  qureg,
TargetGate  gate,
int  controlQubit,
int  targetQubit 
)

Definition at line 239 of file QuEST_qasm.c.

239  {
240 
241  if (!qureg.qasmLog->isLogging)
242  return;
243 
244  int controls[1] = {controlQubit};
245  addGateToQASM(qureg, gate, controls, 1, targetQubit, 0, 0);
246 }

References addGateToQASM(), and Qureg::qasmLog.

Referenced by agnostic_applyQFT(), controlledNot(), controlledPauliY(), controlledPhaseFlip(), sqrtSwapGate(), and swapGate().

◆ qasm_recordControlledParamGate()

void qasm_recordControlledParamGate ( Qureg  qureg,
TargetGate  gate,
int  controlQubit,
int  targetQubit,
qreal  param 
)

Definition at line 248 of file QuEST_qasm.c.

248  {
249 
250  if (!qureg.qasmLog->isLogging)
251  return;
252 
253  int controls[1] = {controlQubit};
254  qreal params[1] = {param};
255  addGateToQASM(qureg, gate, controls, 1, targetQubit, params, 1);
256 
257  // correct the global phase of controlled phase shifts
258  if (gate == GATE_PHASE_SHIFT) {
259  qasm_recordComment(qureg, "Restoring the discarded global phase of the previous controlled phase gate");
260  qreal phaseFix[1] = {param/2.0};
261  addGateToQASM(qureg, GATE_ROTATE_Z, NULL, 0, targetQubit, phaseFix, 1);
262  }
263 }

References addGateToQASM(), GATE_PHASE_SHIFT, GATE_ROTATE_Z, qasm_recordComment(), Qureg::qasmLog, and qreal.

Referenced by controlledPhaseShift(), controlledRotateX(), controlledRotateY(), and controlledRotateZ().

◆ qasm_recordControlledUnitary()

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,b,c)

Definition at line 279 of file QuEST_qasm.c.

279  {
280 
281  if (!qureg.qasmLog->isLogging)
282  return;
283 
284  Complex alpha, beta;
285  qreal globalPhase;
286  getComplexPairAndPhaseFromUnitary(u, &alpha, &beta, &globalPhase);
287 
288  qreal rz2, ry, rz1;
289  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
290 
291  int controls[1] = {controlQubit};
292  qreal params[3] = {rz2, ry, rz1};
293  addGateToQASM(qureg, GATE_UNITARY, controls, 1, targetQubit, params, 3);
294 
295  // add Rz
296  qasm_recordComment(qureg, "Restoring the discarded global phase of the previous controlled unitary");
297  qreal phaseFix[1] = {globalPhase};
298  addGateToQASM(qureg, GATE_ROTATE_Z, NULL, 0, targetQubit, phaseFix, 1);
299 }

References addGateToQASM(), GATE_ROTATE_Z, GATE_UNITARY, getComplexPairAndPhaseFromUnitary(), getZYZRotAnglesFromComplexPair(), qasm_recordComment(), Qureg::qasmLog, and qreal.

Referenced by controlledUnitary().

◆ qasm_recordGate()

void qasm_recordGate ( Qureg  qureg,
TargetGate  gate,
int  targetQubit 
)

Definition at line 179 of file QuEST_qasm.c.

179  {
180 
181  if (!qureg.qasmLog->isLogging)
182  return;
183 
184  addGateToQASM(qureg, gate, NULL, 0, targetQubit, NULL, 0);
185 }

References addGateToQASM(), and Qureg::qasmLog.

Referenced by agnostic_applyQFT(), hadamard(), pauliX(), pauliY(), pauliZ(), qasm_recordInitClassical(), sGate(), and tGate().

◆ qasm_recordInitClassical()

void qasm_recordInitClassical ( Qureg  qureg,
long long int  stateInd 
)

Definition at line 471 of file QuEST_qasm.c.

471  {
472 
473  if (!qureg.qasmLog->isLogging)
474  return;
475 
476  // add an explanatory comment
477  char cmt[MAX_LINE_LEN+1];
478  sprintf(cmt, "Initialising state |%lld>", stateInd);
479  qasm_recordComment(qureg, cmt);
480 
481  // start in |0>
482  qasm_recordInitZero(qureg);
483 
484  // NOT the 1 bits in stateInd
485  for (int q=0; q < qureg.numQubitsRepresented; q++)
486  if ((stateInd >> q) & 1)
487  qasm_recordGate(qureg, GATE_SIGMA_X, q);
488 }

References GATE_SIGMA_X, MAX_LINE_LEN, Qureg::numQubitsRepresented, qasm_recordComment(), qasm_recordGate(), qasm_recordInitZero(), and Qureg::qasmLog.

Referenced by initClassicalState().

◆ qasm_recordInitPlus()

void qasm_recordInitPlus ( Qureg  qureg)

Definition at line 443 of file QuEST_qasm.c.

443  {
444 
445  if (!qureg.qasmLog->isLogging)
446  return;
447 
448  // add an explanatory comment
449  char buf[MAX_LINE_LEN+1];
450  sprintf(buf, "Initialising state |+>");
451  qasm_recordComment(qureg, buf);
452 
453  // it's valid QASM to h the register (I think)
454  // |+> = H |0>
455  qasm_recordInitZero(qureg);
456  int charsWritten = snprintf(
457  buf, MAX_LINE_LEN, "%s %s;\n",
459  if (charsWritten >= MAX_LINE_LEN)
460  bufferOverflow();
461  addStringToQASM(qureg, buf, charsWritten);
462 
463  // old code (before above QASM shortcut)
464  /*
465  qasm_recordInitZero(qureg);
466  for (int q=0; q < qureg.numQubitsRepresented; q++)
467  qasm_recordGate(qureg, GATE_HADAMARD, q);
468  */
469 }

References addStringToQASM(), bufferOverflow(), GATE_HADAMARD, MAX_LINE_LEN, qasm_recordComment(), qasm_recordInitZero(), qasmGateLabels, Qureg::qasmLog, and QUREG_LABEL.

Referenced by initPlusState().

◆ qasm_recordInitZero()

void qasm_recordInitZero ( Qureg  qureg)

Definition at line 428 of file QuEST_qasm.c.

428  {
429 
430  if (!qureg.qasmLog->isLogging)
431  return;
432 
433  char line[MAX_LINE_LEN + 1]; // for trailing \0
434  int len = snprintf(line, MAX_LINE_LEN, "%s %s;\n", INIT_ZERO_CMD, QUREG_LABEL);
435 
436  // check whether we overflowed buffer
437  if (len >= MAX_LINE_LEN)
438  bufferOverflow();
439 
440  addStringToQASM(qureg, line, len);
441 }

References addStringToQASM(), bufferOverflow(), INIT_ZERO_CMD, MAX_LINE_LEN, Qureg::qasmLog, and QUREG_LABEL.

Referenced by initZeroState(), qasm_recordInitClassical(), and qasm_recordInitPlus().

◆ qasm_recordMeasurement()

void qasm_recordMeasurement ( Qureg  qureg,
int  measureQubit 
)

Definition at line 411 of file QuEST_qasm.c.

411  {
412 
413  if (!qureg.qasmLog->isLogging)
414  return;
415 
416  char line[MAX_LINE_LEN + 1]; // for trailing \0
417  int len = snprintf(
418  line, MAX_LINE_LEN, "%s %s[%d] -> %s[%d];\n",
419  MEASURE_CMD, QUREG_LABEL, measureQubit, MESREG_LABEL, measureQubit);
420 
421  // check whether we overflowed buffer
422  if (len >= MAX_LINE_LEN)
423  bufferOverflow();
424 
425  addStringToQASM(qureg, line, len);
426 }

References addStringToQASM(), bufferOverflow(), MAX_LINE_LEN, MEASURE_CMD, MESREG_LABEL, Qureg::qasmLog, and QUREG_LABEL.

Referenced by collapseToOutcome(), measure(), and measureWithStats().

◆ qasm_recordMultiControlledGate()

void qasm_recordMultiControlledGate ( Qureg  qureg,
TargetGate  gate,
int *  controlQubits,
int  numControlQubits,
int  targetQubit 
)

Definition at line 317 of file QuEST_qasm.c.

317  {
318 
319  if (!qureg.qasmLog->isLogging)
320  return;
321 
322  addGateToQASM(qureg, gate, controlQubits, numControlQubits, targetQubit, NULL, 0);
323 }

References addGateToQASM(), and Qureg::qasmLog.

Referenced by multiControlledPhaseFlip().

◆ qasm_recordMultiControlledMultiQubitNot()

void qasm_recordMultiControlledMultiQubitNot ( Qureg  qureg,
int *  ctrls,
int  numCtrls,
int *  targs,
int  numTargs 
)

Definition at line 382 of file QuEST_qasm.c.

382  {
383 
384  if (!qureg.qasmLog->isLogging)
385  return;
386 
387  qasm_recordComment(qureg, "The following %d gates resulted from a single %s() call", numTargs,
388  (numCtrls > 0)? "multiControlledMultiQubitNot" : "multiQubitNot");
389 
390  for (int t=0; t<numTargs; t++)
391  addGateToQASM(qureg, GATE_SIGMA_X, ctrls, numCtrls, targs[t], NULL, 0);
392 }

References addGateToQASM(), GATE_SIGMA_X, qasm_recordComment(), and Qureg::qasmLog.

Referenced by multiControlledMultiQubitNot(), and multiQubitNot().

◆ qasm_recordMultiControlledParamGate()

void qasm_recordMultiControlledParamGate ( Qureg  qureg,
TargetGate  gate,
int *  controlQubits,
int  numControlQubits,
int  targetQubit,
qreal  param 
)

Definition at line 325 of file QuEST_qasm.c.

325  {
326 
327  if (!qureg.qasmLog->isLogging)
328  return;
329 
330  qreal params[1] = {param};
331  addGateToQASM(qureg, gate, controlQubits, numControlQubits, targetQubit, params, 1);
332 
333  // correct the global phase of controlled phase shifts
334  if (gate == GATE_PHASE_SHIFT) {
335  qasm_recordComment(qureg, "Restoring the discarded global phase of the previous multicontrolled phase gate");
336  qreal phaseFix[1] = {param/2.0};
337  addGateToQASM(qureg, GATE_ROTATE_Z, NULL, 0, targetQubit, phaseFix, 1);
338  }
339 }

References addGateToQASM(), GATE_PHASE_SHIFT, GATE_ROTATE_Z, qasm_recordComment(), Qureg::qasmLog, and qreal.

Referenced by multiControlledPhaseShift().

◆ qasm_recordMultiControlledUnitary()

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,b,c)

Definition at line 342 of file QuEST_qasm.c.

342  {
343 
344  if (!qureg.qasmLog->isLogging)
345  return;
346 
347  Complex alpha, beta;
348  qreal globalPhase;
349  getComplexPairAndPhaseFromUnitary(u, &alpha, &beta, &globalPhase);
350 
351  qreal rz2, ry, rz1;
352  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
353 
354  qreal params[3] = {rz2, ry, rz1};
355  addGateToQASM(qureg, GATE_UNITARY, controlQubits, numControlQubits, targetQubit, params, 3);
356 
357  // add Rz
358  qasm_recordComment(qureg, "Restoring the discarded global phase of the previous multicontrolled unitary");
359  qreal phaseFix[1] = {globalPhase};
360  addGateToQASM(qureg, GATE_ROTATE_Z, NULL, 0, targetQubit, phaseFix, 1);
361 }

References addGateToQASM(), GATE_ROTATE_Z, GATE_UNITARY, getComplexPairAndPhaseFromUnitary(), getZYZRotAnglesFromComplexPair(), qasm_recordComment(), Qureg::qasmLog, and qreal.

Referenced by multiControlledUnitary(), and qasm_recordMultiStateControlledUnitary().

◆ qasm_recordMultiStateControlledUnitary()

void qasm_recordMultiStateControlledUnitary ( Qureg  qureg,
ComplexMatrix2  u,
int *  controlQubits,
int *  controlState,
int  numControlQubits,
int  targetQubit 
)

Definition at line 363 of file QuEST_qasm.c.

365  {
366  if (!qureg.qasmLog->isLogging)
367  return;
368 
369  qasm_recordComment(qureg, "NOTing some gates so that the subsequent unitary is controlled-on-0");
370  for (int i=0; i < numControlQubits; i++)
371  if (controlState[i] == 0)
372  addGateToQASM(qureg, GATE_SIGMA_X, NULL, 0, controlQubits[i], NULL, 0);
373 
374  qasm_recordMultiControlledUnitary(qureg, u, controlQubits, numControlQubits, targetQubit);
375 
376  qasm_recordComment(qureg, "Undoing the NOTing of the controlled-on-0 qubits of the previous unitary");
377  for (int i=0; i < numControlQubits; i++)
378  if (controlState[i] == 0)
379  addGateToQASM(qureg, GATE_SIGMA_X, NULL, 0, controlQubits[i], NULL, 0);
380 }

References addGateToQASM(), GATE_SIGMA_X, qasm_recordComment(), qasm_recordMultiControlledUnitary(), and Qureg::qasmLog.

Referenced by multiStateControlledUnitary().

◆ qasm_recordMultiVarPhaseFunc()

void qasm_recordMultiVarPhaseFunc ( Qureg  qureg,
int *  qubits,
int *  numQubitsPerReg,
int  numRegs,
enum bitEncoding  encoding,
qreal coeffs,
qreal exponents,
int *  numTermsPerReg,
long long int *  overrideInds,
qreal overridePhases,
int  numOverrides 
)

Definition at line 666 of file QuEST_qasm.c.

666  {
667 
668  if (!qureg.qasmLog->isLogging)
669  return;
670 
671  qasm_recordComment(qureg, "Here, applyMultiVarPhaseFunc() multiplied a complex scalar of the form");
672 
673  // Here, applyMultiVarPhaseFunction() multiplied a complex scalar of the form
674  // exp(i (
675  // .5 x^2 + .6 x + x
676  // - y^2 - 5 y - y
677  // + z^2 + z^3 ))
678 
679  qasm_recordComment(qureg, " exp(i (");
680  char line[MAX_LINE_LEN+1];
681  int len=0;
682 
683  int tFlatInd = 0;
684  for (int r=0; r<numRegs; r++) {
685  len = snprintf(line, MAX_LINE_LEN, "// ");
686 
687  // manually force sign of first term
688  len += snprintf(line+len, MAX_LINE_LEN-len, (coeffs[tFlatInd] > 0)? " + ":" - ");
689 
690  for (int t=0; t<numTermsPerReg[r]; t++) {
691  if (numRegs <= MAX_REG_SYMBS)
692  len += snprintf(line+len, MAX_LINE_LEN-len,
693  (exponents[tFlatInd] > 0)?
694  REAL_QASM_FORMAT " %c^" REAL_QASM_FORMAT :
695  REAL_QASM_FORMAT " %c^(" REAL_QASM_FORMAT ")",
696  absReal(coeffs[tFlatInd]),
697  getPhaseFuncSymbol(numRegs,r),
698  exponents[tFlatInd]);
699  else
700  len += snprintf(line+len, MAX_LINE_LEN-len,
701  (exponents[tFlatInd] > 0)?
702  REAL_QASM_FORMAT " x%d^" REAL_QASM_FORMAT :
703  REAL_QASM_FORMAT " x%d^(" REAL_QASM_FORMAT ")",
704  absReal(coeffs[tFlatInd]), r, exponents[tFlatInd]);
705  if (t < numTermsPerReg[r]-1)
706  len += snprintf(line+len, MAX_LINE_LEN-len, (coeffs[tFlatInd+1] > 0)? " + ":" - ");
707  tFlatInd++;
708  }
709 
710  if (r < numRegs-1)
711  len += snprintf(line+len, MAX_LINE_LEN-len, "\n");
712  else
713  len += snprintf(line+len, MAX_LINE_LEN-len, " ))\n");
714 
715  if (len >= MAX_LINE_LEN)
716  bufferOverflow();
717  addStringToQASM(qureg, line, len);
718  }
719 
720  addMultiVarRegsToQASM(qureg, qubits, numQubitsPerReg, numRegs, encoding);
721 
722  if (numOverrides > 0)
723  addMultiVarOverridesToQASM(qureg, numRegs, overrideInds, overridePhases, numOverrides);
724 }

References addMultiVarOverridesToQASM(), addMultiVarRegsToQASM(), addStringToQASM(), bufferOverflow(), getPhaseFuncSymbol(), MAX_LINE_LEN, MAX_REG_SYMBS, qasm_recordComment(), and Qureg::qasmLog.

Referenced by applyMultiVarPhaseFunc(), and applyMultiVarPhaseFuncOverrides().

◆ qasm_recordNamedPhaseFunc()

void qasm_recordNamedPhaseFunc ( Qureg  qureg,
int *  qubits,
int *  numQubitsPerReg,
int  numRegs,
enum bitEncoding  encoding,
enum phaseFunc  funcName,
qreal params,
int  numParams,
long long int *  overrideInds,
qreal overridePhases,
int  numOverrides 
)

Definition at line 726 of file QuEST_qasm.c.

726  {
727  // I apologise to my future self and my esteemed readers for such terrible code
728 
729  if (!qureg.qasmLog->isLogging)
730  return;
731 
732  qasm_recordComment(qureg, "Here, applyNamedPhaseFunc() multiplied a complex scalar of form");
733  char line[MAX_LINE_LEN+1];
734 
735  int len = snprintf(line, MAX_LINE_LEN, "// exp(i ");
736 
737  // record norm-based function, like: (-1.0) / sqrt(x^2 + y^2 + z^2 + ...)
738  if (funcName == NORM || funcName == SCALED_NORM ||
739  funcName == INVERSE_NORM || funcName == SCALED_INVERSE_NORM ||
740  funcName == SCALED_INVERSE_SHIFTED_NORM)
741  {
742  // coefficient
743  if (funcName == SCALED_NORM || funcName == SCALED_INVERSE_NORM || funcName == SCALED_INVERSE_SHIFTED_NORM)
744  len += snprintf(line+len, MAX_LINE_LEN-len,
745  (params[0]>0)?
746  REAL_QASM_FORMAT " " :
747  "(" REAL_QASM_FORMAT ") ",
748  params[0]);
749 
750  // sqrt(
751  if (funcName == NORM || funcName == SCALED_NORM)
752  len += snprintf(line+len, MAX_LINE_LEN-len, "sqrt(");
753  else if (funcName == INVERSE_NORM)
754  len += snprintf(line+len, MAX_LINE_LEN-len, "1 / sqrt(");
755  else if (funcName == SCALED_INVERSE_NORM || funcName == SCALED_INVERSE_SHIFTED_NORM)
756  len += snprintf(line+len, MAX_LINE_LEN-len, "/ sqrt(");
757 
758  // x^2 + y^2 + ...
759  if (numRegs <= MAX_REG_SYMBS)
760  for (int r=0; r<numRegs; r++) {
761  if (funcName == SCALED_INVERSE_SHIFTED_NORM)
762  len += snprintf(line+len, MAX_LINE_LEN-len,
763  (params[2+r] < 0)?
764  "(%c^2+" REAL_QASM_FORMAT ")" :
765  "(%c^2-" REAL_QASM_FORMAT ")",
766  getPhaseFuncSymbol(numRegs,r), fabs(params[2+r]));
767  else
768  len += snprintf(line+len, MAX_LINE_LEN-len, "%c^2", getPhaseFuncSymbol(numRegs,r));
769  len += snprintf(line+len, MAX_LINE_LEN-len, (r < numRegs - 1)? " + ":"))\n");
770  }
771  else {
772  if (funcName == SCALED_INVERSE_SHIFTED_NORM)
773  len += snprintf(line+len, MAX_LINE_LEN-len, "(x0-delta0)^2 + (x1-delta1)^2 + (x2-delta2)^2... ))\n");
774  else
775  len += snprintf(line+len, MAX_LINE_LEN-len, "x0^2 + x1^2 + x2^2... ))\n");
776  }
777  }
778  // record product-based, like (-1.0) 1/(x y z) ...
779  else if (funcName == PRODUCT || funcName == SCALED_PRODUCT ||
780  funcName == INVERSE_PRODUCT || funcName == SCALED_INVERSE_PRODUCT)
781  {
782  // coefficient
783  if (funcName == SCALED_PRODUCT || funcName == SCALED_INVERSE_PRODUCT)
784  len += snprintf(line+len, MAX_LINE_LEN-len,
785  (params[0]>0)?
786  REAL_QASM_FORMAT " ":
787  "(" REAL_QASM_FORMAT ") ",
788  params[0]);
789 
790  // reciprocal
791  if (funcName == INVERSE_PRODUCT)
792  len += snprintf(line+len, MAX_LINE_LEN-len, "1 / (");
793  else if (funcName == SCALED_INVERSE_PRODUCT)
794  len += snprintf(line+len, MAX_LINE_LEN-len, "/ (");
795 
796  // x y z ...
797  if (numRegs <= MAX_REG_SYMBS)
798  for (int r=0; r<numRegs; r++)
799  len += snprintf(line+len, MAX_LINE_LEN-len, (r < numRegs - 1)? "%c ":"%c)", getPhaseFuncSymbol(numRegs,r));
800  else
801  len += snprintf(line+len, MAX_LINE_LEN-len, "x0 x1 x2 ...)");
802 
803  // close reciprocal brackets
804  if (funcName == INVERSE_PRODUCT || funcName == SCALED_INVERSE_PRODUCT)
805  len += snprintf(line+len, MAX_LINE_LEN-len, ")");
806  len += snprintf(line+len, MAX_LINE_LEN-len, "\n");
807  }
808  // record distance-based, like (-1.0) 1/sqrt((x1-x2)^2 + (y1-y2)^2 + ...)
809  else if (funcName == DISTANCE || funcName == SCALED_DISTANCE ||
810  funcName == INVERSE_DISTANCE || funcName == SCALED_INVERSE_DISTANCE ||
812  {
813  // coefficient
814  if (funcName == SCALED_DISTANCE || funcName == SCALED_INVERSE_DISTANCE || funcName == SCALED_INVERSE_SHIFTED_DISTANCE)
815  len += snprintf(line+len, MAX_LINE_LEN-len,
816  (params[0]>0)?
817  REAL_QASM_FORMAT " " :
818  "(" REAL_QASM_FORMAT ") ",
819  params[0]);
820 
821  // sqrt(
822  if (funcName == DISTANCE || funcName == SCALED_DISTANCE)
823  len += snprintf(line+len, MAX_LINE_LEN-len, "sqrt(");
824  else if (funcName == INVERSE_DISTANCE)
825  len += snprintf(line+len, MAX_LINE_LEN-len, "1 / sqrt(");
826  else if (funcName == SCALED_INVERSE_DISTANCE || funcName == SCALED_INVERSE_SHIFTED_DISTANCE)
827  len += snprintf(line+len, MAX_LINE_LEN-len, "/ sqrt(");
828 
829  // (x-y)^2 + (z-t)^2 + ...
830  if (numRegs <= MAX_REG_SYMBS)
831  for (int r=0; r<numRegs; r+=2) {
832  if (funcName == SCALED_INVERSE_SHIFTED_DISTANCE)
833  len += snprintf(line+len, MAX_LINE_LEN-len,
834  (params[2+r/2] < 0)?
835  "(%c-%c+" REAL_QASM_FORMAT ")^2":
836  "(%c-%c-" REAL_QASM_FORMAT ")^2",
837  getPhaseFuncSymbol(numRegs,r), getPhaseFuncSymbol(numRegs,r+1), fabs(params[2+r/2]));
838  else
839  len += snprintf(line+len, MAX_LINE_LEN-len, "(%c-%c)^2",
840  getPhaseFuncSymbol(numRegs,r), getPhaseFuncSymbol(numRegs,r+1));
841  len += snprintf(line+len, MAX_LINE_LEN-len, (r+1 < numRegs-1)? " + ":"))\n");
842  }
843  else {
844  if (funcName == SCALED_INVERSE_SHIFTED_DISTANCE)
845  len += snprintf(line+len, MAX_LINE_LEN-len, "(x0-x1-delta0)^2 + (x2-x3-delta1)^2 + ...))\n");
846  else
847  len += snprintf(line+len, MAX_LINE_LEN-len, "(x0-x1)^2 + (x2-x3)^2 + ...))\n");
848  }
849  }
850 
851  if (len >= MAX_LINE_LEN)
852  bufferOverflow();
853  addStringToQASM(qureg, line, len);
854 
855  addMultiVarRegsToQASM(qureg, qubits, numQubitsPerReg, numRegs, encoding);
856  if (numRegs > MAX_REG_SYMBS && (funcName == SCALED_INVERSE_SHIFTED_NORM || funcName == SCALED_INVERSE_SHIFTED_DISTANCE))
857  addShiftValuesToQASM(qureg, funcName, numRegs, params);
858 
859  if (numOverrides > 0)
860  addMultiVarOverridesToQASM(qureg, numRegs, overrideInds,overridePhases, numOverrides);
861 }

References addMultiVarOverridesToQASM(), addMultiVarRegsToQASM(), addShiftValuesToQASM(), addStringToQASM(), bufferOverflow(), DISTANCE, getPhaseFuncSymbol(), INVERSE_DISTANCE, INVERSE_NORM, INVERSE_PRODUCT, MAX_LINE_LEN, MAX_REG_SYMBS, NORM, PRODUCT, qasm_recordComment(), Qureg::qasmLog, SCALED_DISTANCE, SCALED_INVERSE_DISTANCE, SCALED_INVERSE_NORM, SCALED_INVERSE_PRODUCT, SCALED_INVERSE_SHIFTED_DISTANCE, SCALED_INVERSE_SHIFTED_NORM, SCALED_NORM, and SCALED_PRODUCT.

Referenced by agnostic_applyQFT(), applyNamedPhaseFunc(), applyNamedPhaseFuncOverrides(), applyParamNamedPhaseFunc(), and applyParamNamedPhaseFuncOverrides().

◆ qasm_recordParamGate()

void qasm_recordParamGate ( Qureg  qureg,
TargetGate  gate,
int  targetQubit,
qreal  param 
)

Definition at line 187 of file QuEST_qasm.c.

187  {
188 
189  if (!qureg.qasmLog->isLogging)
190  return;
191 
192  qreal params[1] = {param};
193  addGateToQASM(qureg, gate, NULL, 0, targetQubit, params, 1);
194 }

References addGateToQASM(), Qureg::qasmLog, and qreal.

Referenced by phaseShift(), rotateX(), rotateY(), and rotateZ().

◆ qasm_recordPhaseFunc()

void qasm_recordPhaseFunc ( Qureg  qureg,
int *  qubits,
int  numQubits,
enum bitEncoding  encoding,
qreal coeffs,
qreal exponents,
int  numTerms,
long long int *  overrideInds,
qreal overridePhases,
int  numOverrides 
)

Definition at line 490 of file QuEST_qasm.c.

490  {
491 
492  if (!qureg.qasmLog->isLogging)
493  return;
494 
495  qasm_recordComment(qureg, "Here, applyPhaseFunc() multiplied a complex scalar of the form");
496 
497  // record like:
498  // exp(i (-.5 x^2 + .5 x^(-1.5) - 1.3 x^4 ))
499  char line[MAX_LINE_LEN+1];
500  int len = snprintf(line, MAX_LINE_LEN, "// exp(i (");
501  for (int t=0; t<numTerms; t++) {
502  len += snprintf(line+len, MAX_LINE_LEN-len,
503  (exponents[t] > 0)?
504  (REAL_QASM_FORMAT " x^" REAL_QASM_FORMAT) :
505  (REAL_QASM_FORMAT " x^(" REAL_QASM_FORMAT ")"),
506  (t>0)?
507  absReal(coeffs[t]) :
508  coeffs[t],
509  exponents[t]);
510  if (t < numTerms-1)
511  len += snprintf(line+len, MAX_LINE_LEN-len, (coeffs[t+1] > 0)? " + ":" - ");
512  }
513  len += snprintf(line+len, MAX_LINE_LEN-len, "))\n");
514 
515  if (len >= MAX_LINE_LEN)
516  bufferOverflow();
517  addStringToQASM(qureg, line, len);
518 
519  char encBuf[MAX_LINE_LEN];
520  if (encoding == UNSIGNED) sprintf(encBuf, "an unsigned");
521  if (encoding == TWOS_COMPLEMENT) sprintf(encBuf, "a two's complement");
522  qasm_recordComment(qureg, " upon every substate |x>, informed by qubits (under %s binary encoding)", encBuf);
523 
524  // record like:
525  // {0, 3, 2}
526  len=0;
527  len = snprintf(line, MAX_LINE_LEN, "// {");
528  for (int q=0; q<numQubits; q++)
529  len += snprintf(line+len, MAX_LINE_LEN-len, (q < numQubits-1)? "%d, ":"%d}\n", qubits[q]);
530 
531  if (len >= MAX_LINE_LEN)
532  bufferOverflow();
533  addStringToQASM(qureg, line, len);
534 
535  if (numOverrides > 0) {
536  // optionally record like:
537  // |0> -> exp(i .45)
538  // |1> -> exp(i (-.5))
539  qasm_recordComment(qureg, " though with overrides");
540  for (int v=0; v<numOverrides; v++)
541  qasm_recordComment(qureg, (overridePhases[v] >= 0)?
542  " |%lld> -> exp(i " REAL_QASM_FORMAT ")" :
543  " |%lld> -> exp(i (" REAL_QASM_FORMAT "))",
544  overrideInds[v], overridePhases[v]);
545  }
546 
547  // Here, applyPhaseFunction() multiplied a complex scalar of the form
548  // exp(i (.5 x^2 + .5 x^(-1.5) - 1.3 x^4 ))
549  // upon every sub-state |x>, informed by qubits
550  // {0, 1, 2}
551  // though with overrides
552  // |0> -> exp(i .45)
553  // |1> -> exp(i (-.5))
554 }

References addStringToQASM(), bufferOverflow(), MAX_LINE_LEN, qasm_recordComment(), Qureg::qasmLog, TWOS_COMPLEMENT, and UNSIGNED.

Referenced by applyPhaseFunc(), and applyPhaseFuncOverrides().

◆ qasm_recordUnitary()

void qasm_recordUnitary ( Qureg  qureg,
ComplexMatrix2  u,
int  targetQubit 
)

Definition at line 208 of file QuEST_qasm.c.

208  {
209 
210  if (!qureg.qasmLog->isLogging)
211  return;
212 
213  Complex alpha, beta;
214  qreal discardedGlobalPhase;
215  getComplexPairAndPhaseFromUnitary(u, &alpha, &beta, &discardedGlobalPhase);
216 
217  qreal rz2, ry, rz1;
218  getZYZRotAnglesFromComplexPair(alpha, beta, &rz2, &ry, &rz1);
219 
220  qreal params[3] = {rz2, ry, rz1};
221  addGateToQASM(qureg, GATE_UNITARY, NULL, 0, targetQubit, params, 3);
222 }

References addGateToQASM(), GATE_UNITARY, getComplexPairAndPhaseFromUnitary(), getZYZRotAnglesFromComplexPair(), Qureg::qasmLog, and qreal.

Referenced by unitary().

◆ qasm_setup()

void qasm_setup ( Qureg qureg)

Definition at line 61 of file QuEST_qasm.c.

61  {
62 
63  // populate and attach QASM logger
64  QASMLogger *qasmLog = malloc(sizeof *qasmLog);
65  qureg->qasmLog = qasmLog;
66  if (qasmLog == NULL)
68 
69  qasmLog->isLogging = 0;
70  qasmLog->bufferSize = BUF_INIT_SIZE;
71  qasmLog->buffer = malloc(qasmLog->bufferSize * sizeof *(qasmLog->buffer));
72  if (qasmLog->buffer == NULL)
74 
75  // add headers and quantum / classical register creation
76  qasmLog->bufferFill = snprintf(
77  qasmLog->buffer, qasmLog->bufferSize,
78  "OPENQASM 2.0;\nqreg %s[%d];\ncreg %s[%d];\n",
81  if (qasmLog->bufferFill >= qasmLog->bufferSize)
83 }

References BUF_INIT_SIZE, bufferOverflow(), MESREG_LABEL, Qureg::numQubitsRepresented, Qureg::qasmLog, and QUREG_LABEL.

Referenced by createCloneQureg(), createDensityQureg(), and createQureg().

◆ qasm_startRecording()

void qasm_startRecording ( Qureg  qureg)

Definition at line 85 of file QuEST_qasm.c.

85  {
86  qureg.qasmLog->isLogging = 1;
87 }

References Qureg::qasmLog.

Referenced by startRecordingQASM().

◆ qasm_stopRecording()

void qasm_stopRecording ( Qureg  qureg)

Definition at line 89 of file QuEST_qasm.c.

89  {
90  qureg.qasmLog->isLogging = 0;
91 }

References Qureg::qasmLog.

Referenced by stopRecordingQASM().

◆ qasm_writeRecordedToFile()

int qasm_writeRecordedToFile ( Qureg  qureg,
char *  filename 
)

returns success of file write

Definition at line 876 of file QuEST_qasm.c.

876  {
877 
878  FILE *file = fopen(filename, "w");
879  if (file == NULL)
880  return 0;
881 
882  fprintf(file, "%s", qureg.qasmLog->buffer);
883  fclose(file);
884  return 1;
885 }

References Qureg::qasmLog.

Referenced by writeRecordedQASMToFile().

Variable Documentation

◆ qasmGateLabels

const char* qasmGateLabels[]
static
Initial value:
= {
[GATE_SIGMA_X] = "x",
[GATE_SIGMA_Y] = "y",
[GATE_SIGMA_Z] = "z",
[GATE_T] = "t",
[GATE_S] = "s",
[GATE_HADAMARD] = "h",
[GATE_ROTATE_X] = "Rx",
[GATE_ROTATE_Y] = "Ry",
[GATE_ROTATE_Z] = "Rz",
[GATE_UNITARY] = "U",
[GATE_PHASE_SHIFT] = "Rz",
[GATE_SWAP] = "swap",
[GATE_SQRT_SWAP] = "sqrtswap"
}

Definition at line 39 of file QuEST_qasm.c.

Referenced by addGateToQASM(), and qasm_recordInitPlus().

@ INVERSE_PRODUCT
Definition: QuEST.h:233
#define MEASURE_CMD
Definition: QuEST_qasm.c:30
#define MAX_LINE_LEN
Definition: QuEST_qasm.c:34
@ DISTANCE
Definition: QuEST.h:234
#define MESREG_LABEL
Definition: QuEST_qasm.c:28
@ GATE_T
Definition: QuEST_qasm.h:24
@ GATE_PHASE_SHIFT
Definition: QuEST_qasm.h:32
char getPhaseFuncSymbol(int numSymbs, int ind)
Definition: QuEST_qasm.c:556
void qasm_recordInitZero(Qureg qureg)
Definition: QuEST_qasm.c:428
@ GATE_ROTATE_X
Definition: QuEST_qasm.h:27
@ TWOS_COMPLEMENT
Definition: QuEST.h:269
@ GATE_ROTATE_Z
Definition: QuEST_qasm.h:29
@ GATE_SIGMA_Z
Definition: QuEST_qasm.h:23
@ NORM
Definition: QuEST.h:232
@ GATE_HADAMARD
Definition: QuEST_qasm.h:26
@ SCALED_INVERSE_DISTANCE
Definition: QuEST.h:234
#define MAX_REG_SYMBS
Definition: QuEST_qasm.c:37
@ UNSIGNED
Definition: QuEST.h:269
void getComplexPairFromRotation(qreal angle, Vector axis, Complex *alpha, Complex *beta)
Definition: QuEST_common.c:120
@ INVERSE_DISTANCE
Definition: QuEST.h:234
#define qreal
void addShiftValuesToQASM(Qureg qureg, enum phaseFunc funcName, int numRegs, qreal *params)
Definition: QuEST_qasm.c:642
@ GATE_SQRT_SWAP
Definition: QuEST_qasm.h:34
@ GATE_SIGMA_X
Definition: QuEST_qasm.h:21
#define INIT_ZERO_CMD
Definition: QuEST_qasm.c:31
@ SCALED_PRODUCT
Definition: QuEST.h:233
@ GATE_UNITARY
Definition: QuEST_qasm.h:31
static const char * qasmGateLabels[]
Definition: QuEST_qasm.c:39
void qasm_recordComment(Qureg qureg, char *comment,...)
Definition: QuEST_qasm.c:121
@ SCALED_INVERSE_SHIFTED_NORM
Definition: QuEST.h:232
QASMLogger * qasmLog
Storage for generated QASM output.
Definition: QuEST.h:351
void bufferOverflow(void)
Definition: QuEST_qasm.c:56
void addStringToQASM(Qureg qureg, char line[], int lineLen)
Definition: QuEST_qasm.c:93
#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:342
@ INVERSE_NORM
Definition: QuEST.h:232
#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:130
void addMultiVarRegsToQASM(Qureg qureg, int *qubits, int *numQubitsPerReg, int numRegs, enum bitEncoding encoding)
Definition: QuEST_qasm.c:571
@ PRODUCT
Definition: QuEST.h:233
#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:139
#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:327
@ SCALED_DISTANCE
Definition: QuEST.h:234
@ GATE_S
Definition: QuEST_qasm.h:25
@ GATE_SWAP
Definition: QuEST_qasm.h:33
@ GATE_SIGMA_Y
Definition: QuEST_qasm.h:22
@ SCALED_INVERSE_SHIFTED_DISTANCE
Definition: QuEST.h:234
Represents one complex number.
Definition: QuEST.h:103
@ SCALED_NORM
Definition: QuEST.h:232
@ SCALED_INVERSE_PRODUCT
Definition: QuEST.h:233
void qasm_recordGate(Qureg qureg, TargetGate gate, int targetQubit)
Definition: QuEST_qasm.c:179
@ GATE_ROTATE_Y
Definition: QuEST_qasm.h:28
void addMultiVarOverridesToQASM(Qureg qureg, int numRegs, long long int *overrideInds, qreal *overridePhases, int numOverrides)
Definition: QuEST_qasm.c:600
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:142
@ SCALED_INVERSE_NORM
Definition: QuEST.h:232