-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultishot.c
186 lines (154 loc) · 5.04 KB
/
multishot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include "libquil.h"
void die(char *msg) {
printf("%s\n", msg);
exit(1);
}
// We specify a base qubit index of 100 in order to ensure
// that QVM compresses the number of qubits used in the program.
// If QVM did not do so, it would fail when trying to allocate
// memory for 100 qubits. In the examples below, QVM only
// really needs to allocate enough memory for 3 qubits.
const int q0 = 100;
void multishot_with_explicit_ro_indices() {
quil_program program;
char source[200];
sprintf(source,
"DECLARE ro BIT[3]; X %d; I %d; X %d; MEASURE %d ro[0]; MEASURE %d "
"ro[1]; MEASURE %d ro[2]",
q0, q0 + 1, q0 + 2, q0, q0 + 1, q0 + 2);
if (quilc_parse_quil(source, &program) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to parse quil");
exit(1);
}
qvm_multishot_addresses addresses;
if (qvm_multishot_addresses_new(&addresses) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to create addresses");
exit(1);
}
int indices[3] = {0, 1, 2};
if (qvm_multishot_addresses_set(addresses, "ro", indices, 3) !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to set address indices");
exit(1);
}
qvm_multishot_result qvm_res;
int num_trials = 10;
if (qvm_multishot(program, addresses, num_trials, NULL, NULL, NULL,
&qvm_res) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_multishot");
exit(1);
}
for (int i = 0; i < num_trials; i++) {
char vals[3];
if (qvm_multishot_result_get(qvm_res, "ro", i, &vals) !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_multishot_result_get");
exit(1);
}
printf("Trial %d\n\tro[0]=%d\n\tro[1]=%d\n\tro[2]=%d\n", i, vals[0],
vals[1], vals[2]);
}
lisp_release_handle(qvm_res);
lisp_release_handle(program);
}
void multishot_with_implicit_ro_indices() {
quil_program program;
char source[200];
sprintf(source,
"DECLARE ro BIT[3]; X %d; I %d; X %d; MEASURE %d ro[0]; MEASURE %d "
"ro[1]; MEASURE %d ro[2]",
q0, q0 + 1, q0 + 2, q0, q0 + 1, q0 + 2);
if (quilc_parse_quil(source, &program) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to parse quil");
exit(1);
}
qvm_multishot_addresses addresses;
if (qvm_multishot_addresses_new(&addresses) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to create addresses");
exit(1);
}
if (qvm_multishot_addresses_set_all(addresses, "ro") !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to set address indices");
exit(1);
}
qvm_multishot_result qvm_res;
int num_trials = 10;
double gate_noise[] = {0.0, 0.0, 0.0};
if (qvm_multishot(program, addresses, num_trials, NULL, NULL, NULL,
&qvm_res) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_multishot");
exit(1);
}
for (int i = 0; i < num_trials; i++) {
int len;
char *vals;
if (qvm_multishot_result_get_all(qvm_res, "ro", i, &vals, &len) !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_multishot_result_get_all");
exit(1);
}
printf("Trial %d:\n", i);
for (int j = 0; j < len; j++) {
printf("\tro[%d]=%d\n", j, vals[j]);
}
}
lisp_release_handle(qvm_res);
lisp_release_handle(program);
}
void multishot_with_noise() {
quil_program program;
char source[200];
sprintf(source,
"DECLARE ro BIT[3]; X %d; I %d; X %d; MEASURE %d ro[0]; MEASURE %d "
"ro[1]; MEASURE %d ro[2]",
q0, q0 + 1, q0 + 2, q0, q0 + 1, q0 + 2);
if (quilc_parse_quil(source, &program) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to parse quil");
exit(1);
}
qvm_multishot_addresses addresses;
if (qvm_multishot_addresses_new(&addresses) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to create addresses");
exit(1);
}
int indices[3] = {0, 1, 2};
if (qvm_multishot_addresses_set(addresses, "ro", indices, 3) !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to set address indices");
exit(1);
}
qvm_multishot_result qvm_res;
int num_trials = 10;
double gate_noise[] = {0.1, 0.1, 0.1};
double measurement_noise[] = {0.1, 0.0, 0.0};
if (qvm_multishot(program, addresses, num_trials, gate_noise,
measurement_noise, NULL,
&qvm_res) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_multishot");
exit(1);
}
for (int i = 0; i < num_trials; i++) {
char vals[3];
if (qvm_multishot_result_get(qvm_res, "ro", i, &vals) !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_multishot_result_get");
exit(1);
}
printf("Trial %d\n\tro[0]=%d\n\tro[1]=%d\n\tro[2]=%d\n", i, vals[0],
vals[1], vals[2]);
}
lisp_release_handle(qvm_res);
lisp_release_handle(program);
}
int main(int argc, char **argv) {
init("../../libquil.core");
multishot_with_explicit_ro_indices();
multishot_with_implicit_ro_indices();
multishot_with_noise();
return 0;
}