From 7940720f33a217b2590b31b107fdcc585cc8c6ca Mon Sep 17 00:00:00 2001 From: Mark Skilbeck Date: Wed, 15 Nov 2023 16:33:20 +0000 Subject: [PATCH] fix: include gate and measurement noise --- examples/qvm/multishot.c | 56 +++++++++++++++++++++++++++++++++++++--- src/qvm/api.lisp | 12 ++++++--- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/examples/qvm/multishot.c b/examples/qvm/multishot.c index da06009..0196179 100644 --- a/examples/qvm/multishot.c +++ b/examples/qvm/multishot.c @@ -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); @@ -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); @@ -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; } diff --git a/src/qvm/api.lisp b/src/qvm/api.lisp index b64f1d2..eff18be 100644 --- a/src/qvm/api.lisp +++ b/src/qvm/api.lisp @@ -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)))) @@ -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)