MCPcopy Create free account
hub / github.com/NVIDIA/cuda-quantum / bindObserveResult

Function bindObserveResult

python/runtime/common/py_ObserveResult.cpp:51–189  ·  view source on GitHub ↗

@brief Bind the `cudaq::observe_result` and `cudaq::async_observe_result` data classes to python as `cudaq.ObserveResult` and `cudaq.AsyncObserveResult`.

Source from the content-addressed store, hash-verified

49/// data classes to python as `cudaq.ObserveResult` and
50/// `cudaq.AsyncObserveResult`.
51void bindObserveResult(nanobind::module_ &mod) {
52 nanobind::class_<observe_result>(
53 mod, "ObserveResult",
54 "A data-type containing the results of a call to :func:`observe`. "
55 "This includes any measurement counts data, as well as the global "
56 "expectation value of the user-defined `spin_operator`.\n")
57 .def(nanobind::init<double, spin_op, sample_result>())
58 .def("__init__",
59 [](observe_result *self, double exp_val, const spin_op &spin_op,
60 sample_result result) {
61 new (self) observe_result(exp_val, spin_op, result);
62 })
63 .def("__init__",
64 [](observe_result *self, double exp_val, nanobind::object spin_op,
65 sample_result result) {
66 new (self) observe_result(exp_val, to_spin_op(spin_op), result);
67 })
68 /// @brief Bind the member functions of `cudaq.ObserveResult`.
69 .def("dump", &observe_result::dump,
70 "Dump the raw data from the :class:`SampleResult` that are stored "
71 "in :class:`ObserveResult` to the terminal.")
72 .def("get_spin", &observe_result::get_spin,
73 "Return the `SpinOperator` corresponding to this `ObserveResult`.")
74 .def(
75 "counts", &observe_result::raw_data,
76 "Returns a :class:`SampleResult` dictionary with the measurement "
77 "results from the experiment. The result for each individual term of "
78 "the `spin_operator` is stored in its own measurement register. "
79 "Each register name corresponds to the string representation of the "
80 "spin term (without any coefficients).\n")
81 .def(
82 "counts",
83 [](observe_result &self, const spin_op_term &sub_term) {
84 return self.counts(sub_term);
85 },
86 nanobind::arg("sub_term"), "")
87 .def(
88 "counts",
89 [](observe_result &self, nanobind::object sub_term) {
90 return self.counts(to_spin_op_term(sub_term));
91 },
92 nanobind::arg("sub_term"),
93 R"#(Given a `sub_term` of the global `spin_operator` that was passed
94to :func:`observe`, return its measurement counts.
95
96Args:
97 sub_term (`SpinOperator`): An individual sub-term of the
98 `spin_operator`.
99
100Returns:
101 :class:`SampleResult`: The measurement counts data for the individual `sub_term`.)#")
102 .def(
103 "counts",
104 [](observe_result &self, const spin_op &sub_term) {
105 PyErr_WarnEx(
106 PyExc_DeprecationWarning,
107 "ensure to pass a SpinOperatorTerm instead of a SpinOperator",
108 1);

Callers 1

NB_MODULEFunction · 0.85

Calls 5

to_spin_opFunction · 0.85
to_spin_op_termFunction · 0.85
countsMethod · 0.80
expectationMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected