tutorial_example.c
Go to the documentation of this file.
1 
8 #include <stdio.h>
9 #include "QuEST.h"
10 
11 int main (int narg, char *varg[]) {
12 
13 
14 
15  /*
16  * PREPARE QuEST environment
17  * (Required only once per program)
18  */
19 
20  QuESTEnv env = createQuESTEnv();
21 
22  printf("-------------------------------------------------------\n");
23  printf("Running QuEST tutorial:\n\t Basic circuit involving a system of 3 qubits.\n");
24  printf("-------------------------------------------------------\n");
25 
26 
27 
28  /*
29  * PREPARE QUBIT SYSTEM
30  */
31 
32  Qureg qubits = createQureg(3, env);
33  initZeroState(qubits);
34 
35 
36 
37  /*
38  * REPORT SYSTEM AND ENVIRONMENT
39  */
40  printf("\nThis is our environment:\n");
41  reportQuregParams(qubits);
42  reportQuESTEnv(env);
43 
44 
45 
46  /*
47  * APPLY CIRCUIT
48  */
49 
50  hadamard(qubits, 0);
51  controlledNot(qubits, 0, 1);
52  rotateY(qubits, 2, .1);
53 
54  int targs[] = {0,1,2};
55  multiControlledPhaseFlip(qubits, targs, 3);
56 
57  ComplexMatrix2 u = {
58  .real={{.5,.5},{.5,.5}},
59  .imag={{.5,-.5},{-.5,.5}}
60  };
61  unitary(qubits, 0, u);
62 
63  Complex a, b;
64  a.real = .5; a.imag = .5;
65  b.real = .5; b.imag = -.5;
66  compactUnitary(qubits, 1, a, b);
67 
68  Vector v;
69  v.x = 1; v.y = 0; v.z = 0;
70  rotateAroundAxis(qubits, 2, 3.14/2, v);
71 
72  controlledCompactUnitary(qubits, 0, 1, a, b);
73 
74  int ctrls[] = {0,1};
75  multiControlledUnitary(qubits, ctrls, 2, 2, u);
76 
78  toff.real[6][7] = 1;
79  toff.real[7][6] = 1;
80  for (int i=0; i<6; i++)
81  toff.real[i][i] = 1;
82  multiQubitUnitary(qubits, targs, 3, toff);
83 
84 
85 
86  /*
87  * STUDY QUANTUM STATE
88  */
89 
90  printf("\nCircuit output:\n");
91 
92  qreal prob;
93  prob = getProbAmp(qubits, 7);
94  printf("Probability amplitude of |111>: %g\n", prob);
95 
96  prob = calcProbOfOutcome(qubits, 2, 1);
97  printf("Probability of qubit 2 being in state 1: %g\n", prob);
98 
99  int outcome = measure(qubits, 0);
100  printf("Qubit 0 was measured in state %d\n", outcome);
101 
102  outcome = measureWithStats(qubits, 2, &prob);
103  printf("Qubit 2 collapsed to %d with probability %g\n", outcome, prob);
104 
105 
106 
107  /*
108  * FREE MEMORY
109  */
110 
111  destroyQureg(qubits, env);
112  destroyComplexMatrixN(toff);
113 
114 
115 
116  /*
117  * CLOSE QUEST ENVIRONMET
118  * (Required once at end of program)
119  */
120  destroyQuESTEnv(env);
121  return 0;
122 }
qreal getProbAmp(Qureg qureg, long long int index)
Get the probability of a state-vector at an index in the full state vector.
Definition: QuEST.c:690
Represents a 3-vector of real numbers.
Definition: QuEST.h:148
void destroyQuESTEnv(QuESTEnv env)
Destroy the QuEST environment.
void reportQuregParams(Qureg qureg)
Report metainformation about a set of qubits: number of qubits, number of probability amplitudes.
Definition: QuEST_common.c:233
void destroyComplexMatrixN(ComplexMatrixN m)
Destroy a ComplexMatrixN instance created with createComplexMatrixN()
Definition: QuEST.c:1064
ComplexMatrixN createComplexMatrixN(int numQubits)
Create (dynamically) a square complex matrix which can be passed to the multi-qubit general unitary f...
Definition: QuEST.c:1043
qreal z
Definition: QuEST.h:150
int measure(Qureg qureg, int measureQubit)
Measures a single qubit, collapsing it randomly to 0 or 1.
Definition: QuEST.c:756
Information about the environment the program is running in.
Definition: QuEST.h:199
Represents a general 2^N by 2^N matrix of complex numbers.
Definition: QuEST.h:136
#define qreal
void multiControlledUnitary(Qureg qureg, int *controlQubits, const int numControlQubits, const int targetQubit, ComplexMatrix2 u)
Apply a general multiple-control single-target unitary, which can include a global phase factor.
Definition: QuEST.c:372
void multiControlledPhaseFlip(Qureg qureg, int *controlQubits, int numControlQubits)
Apply the multiple-qubit controlled phase flip gate, also known as the multiple-qubit controlled paul...
Definition: QuEST.c:559
int measureWithStats(Qureg qureg, int measureQubit, qreal *outcomeProb)
Measures a single qubit, collapsing it randomly to 0 or 1, and additionally gives the probability of ...
Definition: QuEST.c:743
qreal y
Definition: QuEST.h:150
void multiQubitUnitary(Qureg qureg, int *targs, const int numTargs, ComplexMatrixN u)
Apply a general multi-qubit unitary (including a global phase factor) with any number of target qubit...
Definition: QuEST.c:295
qreal x
Definition: QuEST.h:150
void compactUnitary(Qureg qureg, const int targetQubit, Complex alpha, Complex beta)
Apply a single-qubit unitary parameterised by two given complex scalars.
Definition: QuEST.c:403
void hadamard(Qureg qureg, const int targetQubit)
Apply the single-qubit Hadamard gate.
Definition: QuEST.c:175
void rotateY(Qureg qureg, const int targetQubit, qreal angle)
Rotate a single qubit by a given angle around the Y-axis of the Bloch-sphere.
Definition: QuEST.c:197
void controlledNot(Qureg qureg, const int controlQubit, const int targetQubit)
Apply the controlled not (single control, single target) gate, also known as the c-X,...
Definition: QuEST.c:523
void destroyQureg(Qureg qureg, QuESTEnv env)
Deallocate a Qureg object representing a set of qubits.
Definition: QuEST.c:75
qreal ** real
Definition: QuEST.h:139
int main(int narg, char *varg[])
void unitary(Qureg qureg, const int targetQubit, ComplexMatrix2 u)
Apply a general single-qubit unitary (including a global phase factor).
Definition: QuEST.c:347
Represents a system of qubits.
Definition: QuEST.h:160
qreal real[2][2]
Definition: QuEST.h:116
void controlledCompactUnitary(Qureg qureg, 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.c:416
void reportQuESTEnv(QuESTEnv env)
Report information about the QuEST environment.
qreal real
Definition: QuEST.h:105
Qureg createQureg(int numQubits, QuESTEnv env)
Create a Qureg object representing a set of qubits which will remain in a pure state.
Definition: QuEST.c:34
qreal calcProbOfOutcome(Qureg qureg, const int measureQubit, int outcome)
Gives the probability of a specified qubit being measured in the given outcome (0 or 1).
Definition: QuEST.c:888
qreal imag
Definition: QuEST.h:106
Represents one complex number.
Definition: QuEST.h:103
QuESTEnv createQuESTEnv(void)
Create the QuEST execution environment.
void rotateAroundAxis(Qureg qureg, const int rotQubit, qreal angle, Vector axis)
Rotate a single qubit by a given angle around a given Vector on the Bloch-sphere.
Definition: QuEST.c:573
void initZeroState(Qureg qureg)
Initialise a set of qubits to the classical zero state .
Definition: QuEST.c:111
Represents a 2x2 matrix of complex numbers.
Definition: QuEST.h:114