| 21 | namespace cudaq { |
| 22 | |
| 23 | void bindMeasureCounts(nanobind::module_ &mod) { |
| 24 | using namespace cudaq; |
| 25 | |
| 26 | // TODO Bind the variants of this functions that take the register name |
| 27 | // as input. |
| 28 | nanobind::class_<sample_result>( |
| 29 | mod, "SampleResult", |
| 30 | R"#(A data-type containing the results of a call to :func:`sample`. |
| 31 | This includes all measurement counts data from both mid-circuit and |
| 32 | terminal measurements. |
| 33 | |
| 34 | Note: |
| 35 | Conditional logic on mid-circuit measurements is no longer supported with |
| 36 | `sample`. Use `run` instead.)#") |
| 37 | .def_prop_ro("register_names", &sample_result::register_names) |
| 38 | .def(nanobind::init<>()) |
| 39 | .def( |
| 40 | "dump", [](sample_result &self) { self.dump(); }, |
| 41 | "Print a string of the raw measurement counts data to the " |
| 42 | "terminal.\n") |
| 43 | .def("serialize", &sample_result::serialize, |
| 44 | "Serialize this SampleResult to a vector of integer encoding.") |
| 45 | .def("deserialize", &sample_result::deserialize, |
| 46 | "Deserialize this SampleResult from an existing vector of integers " |
| 47 | "adhering to the implicit encoding.") |
| 48 | .def("get_total_shots", &sample_result::get_total_shots, |
| 49 | "Get the total number of shots in the sample result") |
| 50 | .def( |
| 51 | "__str__", |
| 52 | [](sample_result &self) { |
| 53 | std::stringstream ss; |
| 54 | self.dump(ss); |
| 55 | return ss.str(); |
| 56 | }, |
| 57 | "Return a string of the raw measurement counts that are stored in " |
| 58 | "`self`.\n") |
| 59 | .def( |
| 60 | "__getitem__", |
| 61 | [](sample_result &self, const std::string &bitstring) { |
| 62 | auto map = self.to_map(); |
| 63 | auto iter = map.find(bitstring); |
| 64 | if (iter == map.end()) |
| 65 | throw nanobind::key_error( |
| 66 | ("bitstring '" + bitstring + "' does not exist").c_str()); |
| 67 | |
| 68 | return iter->second; |
| 69 | }, |
| 70 | nanobind::arg("bitstring"), |
| 71 | R"#(Return the measurement counts for the given `bitstring`. |
| 72 | |
| 73 | Args: |
| 74 | bitstring (str): The binary string to return the measurement data of. |
| 75 | |
| 76 | Returns: |
| 77 | float: The number of times the given `bitstring` was measured |
| 78 | during the `shots_count` number of executions on the QPU.)#") |
| 79 | .def( |
| 80 | "__len__", [](sample_result &self) { return self.to_map().size(); }, |
no test coverage detected