Distributed QUEST for GPU
Macros | Functions | Variables
QuEST.cpp File Reference

The core of the QuEST Library. More...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "QuEST_precision.h"
#include "QuEST.h"
#include "QuEST_internal.h"
#include "mt19937ar.h"
#include <sys/param.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>

Go to the source code of this file.

Macros

#define DEBUG   0
 

Functions

void reportState (MultiQubit multiQubit)
 Print the current state vector of probability amplitudes for a set of qubits to file. More...
 
void reportMultiQubitParams (MultiQubit multiQubit)
 Report metainformation about a set of qubits: number of qubits, number of probability amplitudes. More...
 
void rotateAroundAxis (MultiQubit multiQubit, const int rotQubit, REAL angle, Vector axis)
 Rotate a single qubit by a given angle around a given vector on the Bloch-sphere. More...
 
void rotateX (MultiQubit multiQubit, const int rotQubit, REAL angle)
 Rotate a single qubit by a given angle around the X-axis of the Bloch-sphere. More...
 
void rotateY (MultiQubit multiQubit, const int rotQubit, REAL angle)
 Rotate a single qubit by a given angle around the Y-axis of the Bloch-sphere. More...
 
void rotateZ (MultiQubit multiQubit, const int rotQubit, REAL angle)
 Rotate a single qubit by a given angle around the Z-axis of the Bloch-sphere (also known as a phase shift gate). More...
 
void controlledRotateAroundAxis (MultiQubit multiQubit, const int controlQubit, const int targetQubit, REAL angle, Vector axis)
 Applies a controlled rotation by a given angle around a given vector on the Bloch-sphere. More...
 
void controlledRotateX (MultiQubit multiQubit, const int controlQubit, const int targetQubit, REAL angle)
 Applies a controlled rotation by a given angle around the X-axis of the Bloch-sphere. More...
 
void controlledRotateY (MultiQubit multiQubit, const int controlQubit, const int targetQubit, REAL angle)
 Applies a controlled rotation by a given angle around the Y-axis of the Bloch-sphere. More...
 
void controlledRotateZ (MultiQubit multiQubit, const int controlQubit, const int targetQubit, REAL angle)
 Applies a controlled rotation by a given angle around the Z-axis of the Bloch-sphere. More...
 
void sigmaZ (MultiQubit multiQubit, const int targetQubit)
 Apply the single-qubit sigma-Z (also known as the Z, Pauli-Z or phase-flip) gate. More...
 
void sGate (MultiQubit multiQubit, const int targetQubit)
 Apply the single-qubit S gate. More...
 
void tGate (MultiQubit multiQubit, const int targetQubit)
 Apply the single-qubit T gate. More...
 
int validateMatrixIsUnitary (ComplexMatrix2 u)
 
int validateAlphaBeta (Complex alpha, Complex beta)
 
int validateUnitVector (REAL ux, REAL uy, REAL uz)
 
void QuESTSeedRandomDefault ()
 Seed the Mersenne Twister used for random number generation in the QuEST environment with an example defualt seed. More...
 
void QuESTSeedRandom (unsigned long int *seedArray, int numSeeds)
 numSeeds <= 64 More...
 
unsigned long int hashString (char *str)
 

Variables

const char * errorCodes []
 

Detailed Description

The core of the QuEST Library.

Definition in file QuEST.cpp.

Macro Definition Documentation

◆ DEBUG

#define DEBUG   0

Definition at line 22 of file QuEST.cpp.

Function Documentation

◆ controlledRotateAroundAxis()

void controlledRotateAroundAxis ( MultiQubit  multiQubit,
const int  controlQubit,
const int  targetQubit,
REAL  angle,
Vector  axis 
)

Applies a controlled rotation by a given angle around a given vector on the Bloch-sphere.

The vector must not be zero (else an error is thrown), but needn't be unit magnitude.

For angle $\theta$ and axis vector $\vec{n}$, applies $R_{\hat{n}} = \exp \left(- i \frac{\theta}{2} \hat{n} \cdot \vec{\sigma} \right) $ to states where the target qubit is 1 ( $\vec{\sigma}$ is the vector of Pauli matrices).

\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 2) {control}; \node[draw=none] at (-3.5, 0) {target}; \draw (-2, 2) -- (2, 2); \draw[fill=black] (0, 2) circle (.2); \draw (0, 2) -- (0, 1); \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {$R_{\hat{n}}(\theta)$}; \end{tikzpicture} } \]

Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]controlQubitqubit with value 1 in the rotated states
[in]targetQubitqubit to rotate
[in]angleangle by which to rotate in radians
[in]axisvector around which to rotate (can be non-unit; will be normalised)
Exceptions
exitWithErrorif either controlQubit or targetQubit are outside [0, multiQubit.numQubits) or are equal or if axis is the zero vector

Definition at line 122 of file QuEST.cpp.

References controlledCompactUnitary(), Complex::imag, Complex::real, Vector::x, Vector::y, and Vector::z.

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

122  {
123 
124  double mag = sqrt(pow(axis.x,2) + pow(axis.y,2) + pow(axis.z,2));
125  Vector unitAxis = {axis.x/mag, axis.y/mag, axis.z/mag};
126 
127  Complex alpha, beta;
128  alpha.real = cos(angle/2.0);
129  alpha.imag = -sin(angle/2.0)*unitAxis.z;
130  beta.real = sin(angle/2.0)*unitAxis.y;
131  beta.imag = -sin(angle/2.0)*unitAxis.x;
132  controlledCompactUnitary(multiQubit, controlQubit, targetQubit, alpha, beta);
133 }
REAL x
Definition: QuEST.h:42
void controlledCompactUnitary(MultiQubit multiQubit, const int controlQubit, const int targetQubit, Complex alpha, Complex beta)
Apply a controlled unitary (single control, single target) parameterised by two given complex scalars...
Definition: QuEST.h:40
REAL imag
Definition: QuEST.h:29
REAL real
Definition: QuEST.h:28
REAL z
Definition: QuEST.h:42
REAL y
Definition: QuEST.h:42
Represents one complex number.
Definition: QuEST.h:26

◆ controlledRotateX()

void controlledRotateX ( MultiQubit  multiQubit,
const int  controlQubit,
const int  targetQubit,
REAL  angle 
)

Applies a controlled rotation by a given angle around the X-axis of the Bloch-sphere.

The target qubit is rotated in states where the control qubit has value 1.

\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 2) {control}; \node[draw=none] at (-3.5, 0) {target}; \draw (-2, 2) -- (2, 2); \draw[fill=black] (0, 2) circle (.2); \draw (0, 2) -- (0, 1); \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {$R_x(\theta)$}; \end{tikzpicture} } \]


Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]controlQubitqubit which has value 1 in the rotated states
[in]tagretQubitqubit to rotate
[in]angleangle by which to rotate the target qubit in radians
Exceptions
exitWithErrorif either controlQubit or targetQubit are outside [0, multiQubit.numQubits) or are equal.

Definition at line 135 of file QuEST.cpp.

References controlledRotateAroundAxis().

135  {
136 
137  Vector unitAxis = {1, 0, 0};
138  controlledRotateAroundAxis(multiQubit, controlQubit, targetQubit, angle, unitAxis);
139 }
Definition: QuEST.h:40
void controlledRotateAroundAxis(MultiQubit multiQubit, const int controlQubit, const int targetQubit, REAL angle, Vector axis)
Applies a controlled rotation by a given angle around a given vector on the Bloch-sphere.
Definition: QuEST.cpp:122

◆ controlledRotateY()

void controlledRotateY ( MultiQubit  multiQubit,
const int  controlQubit,
const int  targetQubit,
REAL  angle 
)

Applies a controlled rotation by a given angle around the Y-axis of the Bloch-sphere.

The target qubit is rotated in states where the control qubit has value 1.

\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 2) {control}; \node[draw=none] at (-3.5, 0) {target}; \draw (-2, 2) -- (2, 2); \draw[fill=black] (0, 2) circle (.2); \draw (0, 2) -- (0, 1); \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {$R_y(\theta)$}; \end{tikzpicture} } \]


Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]controlQubitqubit which has value 1 in the rotated states
[in]tagretQubitqubit to rotate
[in]angleangle by which to rotate the target qubit in radians
Exceptions
exitWithErrorif either controlQubit or targetQubit are outside [0, multiQubit.numQubits) or are equal.

Definition at line 141 of file QuEST.cpp.

References controlledRotateAroundAxis().

141  {
142 
143  Vector unitAxis = {0, 1, 0};
144  controlledRotateAroundAxis(multiQubit, controlQubit, targetQubit, angle, unitAxis);
145 }
Definition: QuEST.h:40
void controlledRotateAroundAxis(MultiQubit multiQubit, const int controlQubit, const int targetQubit, REAL angle, Vector axis)
Applies a controlled rotation by a given angle around a given vector on the Bloch-sphere.
Definition: QuEST.cpp:122

◆ controlledRotateZ()

void controlledRotateZ ( MultiQubit  multiQubit,
const int  controlQubit,
const int  targetQubit,
REAL  angle 
)

Applies a controlled rotation by a given angle around the Z-axis of the Bloch-sphere.

The target qubit is rotated in states where the control qubit has value 1.

\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 2) {control}; \node[draw=none] at (-3.5, 0) {target}; \draw (-2, 2) -- (2, 2); \draw[fill=black] (0, 2) circle (.2); \draw (0, 2) -- (0, 1); \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {$R_z(\theta)$}; \end{tikzpicture} } \]


Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]controlQubitqubit which has value 1 in the rotated states
[in]tagretQubitqubit to rotate
[in]angleangle by which to rotate the target qubit in radians
Exceptions
exitWithErrorif either controlQubit or targetQubit are outside [0, multiQubit.numQubits) or are equal.

Definition at line 147 of file QuEST.cpp.

References controlledRotateAroundAxis().

147  {
148 
149  Vector unitAxis = {0, 0, 1};
150  controlledRotateAroundAxis(multiQubit, controlQubit, targetQubit, angle, unitAxis);
151 }
Definition: QuEST.h:40
void controlledRotateAroundAxis(MultiQubit multiQubit, const int controlQubit, const int targetQubit, REAL angle, Vector axis)
Applies a controlled rotation by a given angle around a given vector on the Bloch-sphere.
Definition: QuEST.cpp:122

◆ hashString()

unsigned long int hashString ( char *  str)

Definition at line 238 of file QuEST.cpp.

Referenced by QuESTSeedRandomDefault().

238  {
239  unsigned long int hash = 5381;
240  int c;
241 
242  while ((c = *str++))
243  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
244 
245  return hash;
246 }

◆ QuESTSeedRandom()

void QuESTSeedRandom ( unsigned long int *  seedArray,
int  numSeeds 
)

numSeeds <= 64

Seed the Mersenne Twister used for random number generation in the QuEST environment with a user defined seed.

Definition at line 231 of file QuEST.cpp.

References init_by_array().

231  {
232  // init MT random number generator with user defined list of seeds
233  // for the MPI version, it is ok that all procs will get the same seed as random numbers will only be
234  // used by the master process
235  init_by_array(seedArray, numSeeds);
236 }
void init_by_array(unsigned long init_key[], int key_length)
Definition: mt19937ar.cpp:76

◆ QuESTSeedRandomDefault()

void QuESTSeedRandomDefault ( void  )

Seed the Mersenne Twister used for random number generation in the QuEST environment with an example defualt seed.

This default seeding function uses the mt19937 init_by_array function with three keys – time, pid and hostname. Subsequent calls to mt19937 genrand functions will use this seeding. For a multi process code, the same seed is given to all process, therefore this seeding is only appropriate to use for functions such as measure where all processes require the same random value.

For more information about the MT, see http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html

Definition at line 206 of file QuEST.cpp.

References hashString(), and init_by_array().

206  {
207  // init MT random number generator with three keys -- time, pid and a hash of hostname
208  // for the MPI version, it is ok that all procs will get the same seed as random numbers will only be
209  // used by the master process
210 
211  struct timeval tv;
212  gettimeofday(&tv, NULL);
213 
214  double time_in_mill =
215  (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ; // convert tv_sec & tv_usec to millisecond
216 
217  unsigned long int pid = getpid();
218  unsigned long int msecs = (unsigned long int) time_in_mill;
219  char hostName[MAXHOSTNAMELEN+1];
220  gethostname(hostName, sizeof(hostName));
221  unsigned long int hostNameInt = hashString(hostName);
222 
223  unsigned long int key[3];
224  key[0] = msecs; key[1] = pid; key[2] = hostNameInt;
225  init_by_array(key, 3);
226 }
unsigned long int hashString(char *str)
Definition: QuEST.cpp:238
void init_by_array(unsigned long init_key[], int key_length)
Definition: mt19937ar.cpp:76

◆ reportMultiQubitParams()

void reportMultiQubitParams ( MultiQubit  multiQubit)

Report metainformation about a set of qubits: number of qubits, number of probability amplitudes.

Parameters
[in,out]multiQubitobject representing the set of qubits
[in]envobject representing the execution environment (local, multinode etc)

Definition at line 80 of file QuEST.cpp.

References MultiQubit::chunkId, MultiQubit::numChunks, and MultiQubit::numQubits.

80  {
81  long long int numAmps = 1L << multiQubit.numQubits;
82  long long int numAmpsPerRank = numAmps/multiQubit.numChunks;
83  if (multiQubit.chunkId==0){
84  printf("QUBITS:\n");
85  printf("Number of qubits is %d.\n", multiQubit.numQubits);
86  printf("Number of amps is %lld.\n", numAmps);
87  printf("Number of amps per rank is %lld.\n", numAmpsPerRank);
88  }
89 }
int numChunks
Number of chunks the state vector is broken up into – the number of MPI processes used...
Definition: QuEST.h:66
int numQubits
Number of qubits in the state.
Definition: QuEST.h:59
int chunkId
The position of the chunk of the state vector held by this process in the full state vector...
Definition: QuEST.h:64

◆ reportState()

void reportState ( MultiQubit  multiQubit)

Print the current state vector of probability amplitudes for a set of qubits to file.

File format:

real, imag
realComponent1, imagComponent1
realComponent2, imagComponent2
...
realComponentN, imagComponentN

File naming convention:

For each node that the program runs on, a file 'state_rank_[node_rank].csv' is generated. If there is more than one node, ranks after the first do not include the header

real, imag

so that files are easier to combine.

Parameters
[in,out]multiQubitobject representing the set of qubits

Definition at line 62 of file QuEST.cpp.

References MultiQubit::chunkId, ComplexArray::imag, MultiQubit::numAmps, ComplexArray::real, and MultiQubit::stateVec.

62  {
63  FILE *state;
64  char filename[100];
65  long long int index;
66  sprintf(filename, "state_rank_%d.csv", multiQubit.chunkId);
67  state = fopen(filename, "w");
68  if (multiQubit.chunkId==0) fprintf(state, "real, imag\n");
69 
70  for(index=0; index<multiQubit.numAmps; index++){
71  fprintf(state, "%.12f, %.12f\n", multiQubit.stateVec.real[index], multiQubit.stateVec.imag[index]);
72  }
73  fclose(state);
74 }
REAL * real
Definition: QuEST.h:20
long long int numAmps
Number of probability amplitudes held in stateVec by this process In the non-MPI version, this is the total number of amplitudes.
Definition: QuEST.h:62
int chunkId
The position of the chunk of the state vector held by this process in the full state vector...
Definition: QuEST.h:64
REAL * imag
Definition: QuEST.h:21
ComplexArray stateVec
Probablilty amplitudes for the multi qubit state.
Definition: QuEST.h:51

◆ rotateAroundAxis()

void rotateAroundAxis ( MultiQubit  multiQubit,
const int  rotQubit,
REAL  angle,
Vector  unitAxis 
)

Rotate a single qubit by a given angle around a given vector on the Bloch-sphere.

  • The vector must not be zero (else an error is thrown), but needn't be unit magnitude.
Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]rotQubitqubit to rotate
[in]angleangle by which to rotate in radians
[in]axisvector around which to rotate
Exceptions
exitWithErrorif rotQubit is outside [0, multiQubit.numQubits), or if axis is the zero vector

Definition at line 91 of file QuEST.cpp.

References compactUnitary(), Complex::imag, Complex::real, Vector::x, Vector::y, and Vector::z.

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

91  {
92 
93  double mag = sqrt(pow(axis.x,2) + pow(axis.y,2) + pow(axis.z,2));
94  Vector unitAxis = {axis.x/mag, axis.y/mag, axis.z/mag};
95 
96  Complex alpha, beta;
97  alpha.real = cos(angle/2.0);
98  alpha.imag = -sin(angle/2.0)*unitAxis.z;
99  beta.real = sin(angle/2.0)*unitAxis.y;
100  beta.imag = -sin(angle/2.0)*unitAxis.x;
101  compactUnitary(multiQubit, rotQubit, alpha, beta);
102 }
REAL x
Definition: QuEST.h:42
Definition: QuEST.h:40
REAL imag
Definition: QuEST.h:29
REAL real
Definition: QuEST.h:28
void compactUnitary(MultiQubit multiQubit, const int rotQubit, Complex alpha, Complex beta)
Apply a single-qubit unitary parameterised by two given complex scalars.
REAL z
Definition: QuEST.h:42
REAL y
Definition: QuEST.h:42
Represents one complex number.
Definition: QuEST.h:26

◆ rotateX()

void rotateX ( MultiQubit  multiQubit,
const int  rotQubit,
REAL  angle 
)

Rotate a single qubit by a given angle around the X-axis of the Bloch-sphere.

For angle $\theta$, applies

\[ \begin{pmatrix} \cos\theta/2 & -i \sin \theta/2\\ -i \sin \theta/2 & \cos \theta/2 \end{pmatrix} \]

\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 0) {rot}; \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {$R_x(\theta)$}; \end{tikzpicture} } \]

Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]rotQubitqubit to rotate
[in]angleangle by which to rotate in radians
Exceptions
exitWithErrorif rotQubit is outside [0, multiQubit.numQubits).

Definition at line 104 of file QuEST.cpp.

References rotateAroundAxis().

104  {
105 
106  Vector unitAxis = {1, 0, 0};
107  rotateAroundAxis(multiQubit, rotQubit, angle, unitAxis);
108 }
void rotateAroundAxis(MultiQubit multiQubit, const int rotQubit, REAL angle, Vector axis)
Rotate a single qubit by a given angle around a given vector on the Bloch-sphere. ...
Definition: QuEST.cpp:91
Definition: QuEST.h:40

◆ rotateY()

void rotateY ( MultiQubit  multiQubit,
const int  rotQubit,
REAL  angle 
)

Rotate a single qubit by a given angle around the Y-axis of the Bloch-sphere.

For angle $\theta$, applies

\[ \begin{pmatrix} \cos\theta/2 & \sin \theta/2\\ \sin \theta/2 & \cos \theta/2 \end{pmatrix} \]


\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 0) {rot}; \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {$R_y(\theta)$}; \end{tikzpicture} } \]

Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]rotQubitqubit to rotate
[in]angleangle by which to rotate in radians
Exceptions
exitWithErrorif rotQubit is outside [0, multiQubit.numQubits).

Definition at line 110 of file QuEST.cpp.

References rotateAroundAxis().

110  {
111 
112  Vector unitAxis = {0, 1, 0};
113  rotateAroundAxis(multiQubit, rotQubit, angle, unitAxis);
114 }
void rotateAroundAxis(MultiQubit multiQubit, const int rotQubit, REAL angle, Vector axis)
Rotate a single qubit by a given angle around a given vector on the Bloch-sphere. ...
Definition: QuEST.cpp:91
Definition: QuEST.h:40

◆ rotateZ()

void rotateZ ( MultiQubit  multiQubit,
const int  rotQubit,
REAL  angle 
)

Rotate a single qubit by a given angle around the Z-axis of the Bloch-sphere (also known as a phase shift gate).

For angle $\theta$, applies

\[ \begin{pmatrix} \exp(-i \theta/2) & 0 \\ 0 & \exp(i \theta/2) \end{pmatrix} \]

\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 0) {rot}; \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {$R_z(\theta)$}; \end{tikzpicture} } \]

Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]rotQubitqubit to rotate
[in]angleangle by which to rotate in radians
Exceptions
exitWithErrorif rotQubit is outside [0, multiQubit.numQubits).

Definition at line 116 of file QuEST.cpp.

References rotateAroundAxis().

116  {
117 
118  Vector unitAxis = {0, 0, 1};
119  rotateAroundAxis(multiQubit, rotQubit, angle, unitAxis);
120 }
void rotateAroundAxis(MultiQubit multiQubit, const int rotQubit, REAL angle, Vector axis)
Rotate a single qubit by a given angle around a given vector on the Bloch-sphere. ...
Definition: QuEST.cpp:91
Definition: QuEST.h:40

◆ sGate()

void sGate ( MultiQubit  multiQubit,
const int  targetQubit 
)

Apply the single-qubit S gate.

This is a rotation of $\pi/2$ around the Z-axis on the Bloch sphere, or the unitary:

\[ \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix} \]

\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 0) {target}; \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {S}; \end{tikzpicture} } \]

Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]targetQubitqubit to operate upon
Exceptions
exitWithErrorif targetQubit is outside [0, multiQubit.numQubits)

Definition at line 158 of file QuEST.cpp.

References phaseGate(), and S_GATE.

159 {
160  phaseGate(multiQubit, targetQubit, S_GATE);
161 }
void phaseGate(MultiQubit multiQubit, const int targetQubit, enum phaseGateType type)
Definition: QuEST.h:79

◆ sigmaZ()

void sigmaZ ( MultiQubit  multiQubit,
const int  targetQubit 
)

Apply the single-qubit sigma-Z (also known as the Z, Pauli-Z or phase-flip) gate.

This is a rotation of $\pi$ around the Z-axis (a phase shift) on the Bloch sphere. I.e.

\[ \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix} \]


\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 0) {target}; \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {$\sigma_z$}; \end{tikzpicture} } \]


Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]targetQubitqubit to operate on
Exceptions
exitWithErrorif targetQubit is outside [0, multiQubit.numQubits).

Definition at line 153 of file QuEST.cpp.

References phaseGate(), and SIGMA_Z.

154 {
155  phaseGate(multiQubit, targetQubit, SIGMA_Z);
156 }
void phaseGate(MultiQubit multiQubit, const int targetQubit, enum phaseGateType type)
Definition: QuEST.h:79

◆ tGate()

void tGate ( MultiQubit  multiQubit,
const int  targetQubit 
)

Apply the single-qubit T gate.

This is a rotation of $\pi/4$ around the Z-axis on the Bloch sphere, or the unitary:

\[ \begin{pmatrix} 1 & 0 \\ 0 & \exp\left(i \frac{\pi}{4}\right) \end{pmatrix} \]

\[ \setlength{\fboxrule}{0.01pt} \fbox{ \begin{tikzpicture}[scale=.5] \node[draw=none] at (-3.5, 0) {target}; \draw (-2,0) -- (-1, 0); \draw (1, 0) -- (2, 0); \draw (-1,-1)--(-1,1)--(1,1)--(1,-1)--cycle; \node[draw=none] at (0, 0) {T}; \end{tikzpicture} } \]

Parameters
[in,out]multiQubitobject representing the set of all qubits
[in]targetQubitqubit to operate upon
Exceptions
exitWithErrorif targetQubit is outside [0, multiQubit.numQubits)

Definition at line 163 of file QuEST.cpp.

References phaseGate(), and T_GATE.

164 {
165  phaseGate(multiQubit, targetQubit, T_GATE);
166 }
void phaseGate(MultiQubit multiQubit, const int targetQubit, enum phaseGateType type)
Definition: QuEST.h:79

◆ validateAlphaBeta()

int validateAlphaBeta ( Complex  alpha,
Complex  beta 
)

Definition at line 193 of file QuEST.cpp.

References Complex::imag, Complex::real, and REAL_EPS.

193  {
194  if ( fabs(alpha.real*alpha.real
195  + alpha.imag*alpha.imag
196  + beta.real*beta.real
197  + beta.imag*beta.imag - 1) > REAL_EPS ) return 0;
198  else return 1;
199 }
REAL imag
Definition: QuEST.h:29
REAL real
Definition: QuEST.h:28
#define REAL_EPS

◆ validateMatrixIsUnitary()

int validateMatrixIsUnitary ( ComplexMatrix2  u)

Definition at line 168 of file QuEST.cpp.

References Complex::imag, ComplexMatrix2::r0c0, ComplexMatrix2::r0c1, ComplexMatrix2::r1c0, ComplexMatrix2::r1c1, Complex::real, and REAL_EPS.

168  {
169 
170  if ( fabs(u.r0c0.real*u.r0c0.real
171  + u.r0c0.imag*u.r0c0.imag
172  + u.r1c0.real*u.r1c0.real
173  + u.r1c0.imag*u.r1c0.imag - 1) > REAL_EPS ) return 0;
174  // check
175  if ( fabs(u.r0c1.real*u.r0c1.real
176  + u.r0c1.imag*u.r0c1.imag
177  + u.r1c1.real*u.r1c1.real
178  + u.r1c1.imag*u.r1c1.imag - 1) > REAL_EPS ) return 0;
179 
180  if ( fabs(u.r0c0.real*u.r0c1.real
181  + u.r0c0.imag*u.r0c1.imag
182  + u.r1c0.real*u.r1c1.real
183  + u.r1c0.imag*u.r1c1.imag) > REAL_EPS ) return 0;
184 
185  if ( fabs(u.r0c1.real*u.r0c0.imag
186  - u.r0c0.real*u.r0c1.imag
187  + u.r1c1.real*u.r1c0.imag
188  - u.r1c0.real*u.r1c1.imag) > REAL_EPS ) return 0;
189 
190  return 1;
191 }
Complex r1c1
Definition: QuEST.h:37
REAL imag
Definition: QuEST.h:29
REAL real
Definition: QuEST.h:28
Complex r0c0
Definition: QuEST.h:36
Complex r1c0
Definition: QuEST.h:37
#define REAL_EPS
Complex r0c1
Definition: QuEST.h:36

◆ validateUnitVector()

int validateUnitVector ( REAL  ux,
REAL  uy,
REAL  uz 
)

Definition at line 201 of file QuEST.cpp.

References REAL_EPS.

201  {
202  if ( fabs(sqrt(ux*ux + uy*uy + uz*uz) - 1) > REAL_EPS ) return 0;
203  else return 1;
204 }
#define REAL_EPS

Variable Documentation

◆ errorCodes

const char* errorCodes[]
Initial value:
= {
"Success",
"Invalid target qubit. Note qubits are zero indexed.",
"Invalid control qubit. Note qubits are zero indexed.",
"Control qubit cannot equal target qubit.",
"Invalid number of control qubits",
"Invalid unitary matrix.",
"Invalid rotation arguments.",
"Invalid system size. Cannot print output for systems greater than 5 qubits.",
"Can't collapse to state with zero probability.",
"Invalid number of qubits.",
"Invalid measurement outcome -- must be either 0 or 1."
}

Definition at line 24 of file QuEST.cpp.