Skip to content

Commit

Permalink
fix: include gate and measurement noise
Browse files Browse the repository at this point in the history
  • Loading branch information
notmgsk committed Nov 15, 2023
1 parent deab064 commit 7940720
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
56 changes: 53 additions & 3 deletions examples/qvm/multishot.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void multishot_with_explicit_ro_indices() {

qvm_multishot_result qvm_res;
int num_trials = 10;
if (qvm_multishot(program, addresses, num_trials, &qvm_res) !=
if (qvm_multishot(program, addresses, num_trials, NULL, NULL, &qvm_res) !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_multishot");
exit(1);
Expand Down Expand Up @@ -82,7 +82,8 @@ void multishot_with_implicit_ro_indices() {

qvm_multishot_result qvm_res;
int num_trials = 10;
if (qvm_multishot(program, addresses, num_trials, &qvm_res) !=
double gate_noise[] = {0.0, 0.0, 0.0};
if (qvm_multishot(program, addresses, num_trials, &qvm_res, NULL, NULL) !=
LIBQUIL_ERROR_SUCCESS) {
LIBQUIL_ERROR("failed to call qvm_multishot");
exit(1);
Expand All @@ -108,11 +109,60 @@ void multishot_with_implicit_ro_indices() {
lisp_release_handle(program);
}

void multishot_with_noise() {
quil_program program;

char *source = "DECLARE ro BIT[3]; X 0; I 1; X 2; MEASURE 0 ro[0]; MEASURE 1 "
"ro[1]; MEASURE 2 ro[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, &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_implicit_ro_indices();

return 0;
}
12 changes: 9 additions & 3 deletions src/qvm/api.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
results-map
program-memory-descriptors)

(defun qvm-multishot (compiled-quil addresses trials)
(defun qvm-multishot (compiled-quil addresses trials gate-noise-ptr measurement-noise-ptr)
"Executes COMPILED-QUIL on a pure-state QVM TRIALS numbers of times. At the end of each execution, the measurements for ADDRESSES are collected. The return value is a list of those measurements."
(let* ((num-qubits (cl-quil.frontend::qubits-needed compiled-quil))
(results (%perform-multishot compiled-quil num-qubits addresses trials nil nil)))
(gate-noise (unless (null-pointer-p gate-noise-ptr)
(unpack-c-array-to-lisp-list gate-noise-ptr 3 :double)))
(measurement-noise (unless (null-pointer-p measurement-noise-ptr)
(unpack-c-array-to-lisp-list measurement-noise-ptr 3 :double)))
(results (%perform-multishot compiled-quil num-qubits addresses trials gate-noise measurement-noise)))
(make-qvm-multishot-result
:results-map results
:program-memory-descriptors (cl-quil:parsed-program-memory-definitions compiled-quil))))
Expand Down Expand Up @@ -164,7 +168,9 @@
qvm-multishot-result
((program quil-program)
(addresses qvm-multishot-addresses)
(trials :int)))
(trials :int)
(gate-noise :pointer)
(measurement-noise :pointer)))
(("multishot_result_get" qvm-multishot-result-get)
:void
((qvm-result qvm-multishot-result)
Expand Down

0 comments on commit 7940720

Please sign in to comment.