| 126 | } |
| 127 | |
| 128 | void bindSpinOperator(nanobind::module_ &mod) { |
| 129 | |
| 130 | auto spin_op_class = nanobind::class_<spin_op>(mod, "SpinOperator"); |
| 131 | auto spin_op_term_class = |
| 132 | nanobind::class_<spin_op_term>(mod, "SpinOperatorTerm"); |
| 133 | |
| 134 | spin_op_class |
| 135 | .def( |
| 136 | "__iter__", |
| 137 | [](spin_op &self) { |
| 138 | nanobind::list items; |
| 139 | for (auto it = self.begin(); it != self.end(); ++it) |
| 140 | items.append(nanobind::cast(*it)); |
| 141 | return items.attr("__iter__")(); |
| 142 | }, |
| 143 | "Loop through each term of the operator.") |
| 144 | |
| 145 | // properties |
| 146 | |
| 147 | .def_prop_ro("parameters", &spin_op::get_parameter_descriptions, |
| 148 | "Returns a dictionary that maps each parameter " |
| 149 | "name to its description.") |
| 150 | .def_prop_ro("degrees", &spin_op::degrees, |
| 151 | "Returns a vector that lists all degrees of " |
| 152 | "freedom that the operator targets. " |
| 153 | "The order of degrees is from smallest to largest " |
| 154 | "and reflects the ordering of " |
| 155 | "the matrix returned by `to_matrix`. " |
| 156 | "Specifically, the indices of a statevector " |
| 157 | "with two qubits are {00, 01, 10, 11}. An " |
| 158 | "ordering of degrees {0, 1} then indicates " |
| 159 | "that a state where the qubit with index 0 equals " |
| 160 | "1 with probability 1 is given by " |
| 161 | "the vector {0., 1., 0., 0.}.") |
| 162 | .def_prop_ro("min_degree", &spin_op::min_degree, |
| 163 | "Returns the smallest index of the degrees of " |
| 164 | "freedom that the operator targets.") |
| 165 | .def_prop_ro("max_degree", &spin_op::max_degree, |
| 166 | "Returns the smallest index of the degrees of " |
| 167 | "freedom that the operator targets.") |
| 168 | .def_prop_ro("term_count", &spin_op::num_terms, |
| 169 | "Returns the number of terms in the operator.") |
| 170 | // only exists for spin operators |
| 171 | .def_prop_ro("qubit_count", &spin_op::num_qubits<spin_handler>, |
| 172 | "Return the number of qubits this operator acts on.") |
| 173 | |
| 174 | // constructors |
| 175 | |
| 176 | .def(nanobind::init<>(), |
| 177 | "Creates a default instantiated sum. A default instantiated " |
| 178 | "sum has no value; it will take a value the first time an " |
| 179 | "arithmetic operation " |
| 180 | "is applied to it. In that sense, it acts as both the additive and " |
| 181 | "multiplicative " |
| 182 | "identity. To construct a `0` value in the mathematical sense " |
| 183 | "(neutral element " |
| 184 | "for addition), use `empty()` instead.") |
| 185 | .def(nanobind::init<std::size_t>(), nanobind::arg("size"), |
no test coverage detected