-
Notifications
You must be signed in to change notification settings - Fork 1
/
expectation.c
56 lines (45 loc) · 1.28 KB
/
expectation.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
#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);
}
double do_expectation(quil_program state_prep, quil_program operator) {
quil_program operators[1] = {operator};
double expectations[1] = {0};
if (qvm_expectation(state_prep, operators, 1, NULL, &expectations) !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_expectation");
exit(1);
}
return expectations[0];
}
int main(int argc, char **argv) {
init("../../libquil.core");
quil_program i;
quil_program z;
quil_program x;
if (quilc_parse_quil("I 0", &i) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to parse quil");
exit(1);
}
if (quilc_parse_quil("Z 0", &z) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to parse quil");
exit(1);
}
if (quilc_parse_quil("X 0", &x) != LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to parse quil");
exit(1);
}
double ziz_expectation = do_expectation(i, z);
printf("<Z|I|Z> = %f (should be 1.0)\n", ziz_expectation);
double xix_expectation = do_expectation(x, z);
printf("<Z|X|Z> = %f (should be -1.0)\n", xix_expectation);
lisp_release_handle(i);
lisp_release_handle(z);
lisp_release_handle(x);
return 0;
}