| 33 | } |
| 34 | |
| 35 | void bindOperatorHandlers(nanobind::module_ &mod) { |
| 36 | using matrix_callback = std::function<complex_matrix( |
| 37 | const std::vector<int64_t> &, const parameter_map &)>; |
| 38 | |
| 39 | nanobind::class_<matrix_handler>(mod, "MatrixOperatorElement") |
| 40 | .def_prop_ro( |
| 41 | "id", |
| 42 | [](const matrix_handler &self) { return self.to_string(false); }, |
| 43 | "Returns the id used to define and instantiate the operator.") |
| 44 | .def_prop_ro("degrees", &matrix_handler::degrees, |
| 45 | "Returns a vector that lists all degrees of " |
| 46 | "freedom that the operator targets.") |
| 47 | .def_prop_ro("parameters", &matrix_handler::get_parameter_descriptions, |
| 48 | "Returns a dictionary that maps each parameter " |
| 49 | "name to its description.") |
| 50 | .def_prop_ro("expected_dimensions", |
| 51 | &matrix_handler::get_expected_dimensions, |
| 52 | "The number of levels, that is the dimension, for " |
| 53 | "each degree of freedom " |
| 54 | "in canonical order that the operator acts on. A " |
| 55 | "value of zero or less " |
| 56 | "indicates that the operator is defined for any " |
| 57 | "dimension of that degree.") |
| 58 | .def(nanobind::init<std::size_t>(), |
| 59 | "Creates an identity operator on the given target.") |
| 60 | .def( |
| 61 | "__init__", |
| 62 | [](matrix_handler *self, std::string operator_id, |
| 63 | std::vector<std::size_t> degrees) { |
| 64 | new (self) |
| 65 | matrix_handler(std::move(operator_id), std::move(degrees)); |
| 66 | }, |
| 67 | nanobind::arg("id"), nanobind::arg("degrees"), |
| 68 | "Creates the matrix operator with the given id acting on the given " |
| 69 | "degrees of " |
| 70 | "freedom. Throws a runtime exception if no operator with that id " |
| 71 | "has been defined.") |
| 72 | .def(nanobind::init<const matrix_handler &>(), "Copy constructor.") |
| 73 | .def("__eq__", &matrix_handler::operator==, nanobind::is_operator()) |
| 74 | .def("to_string", &matrix_handler::to_string, |
| 75 | nanobind::arg("include_degrees"), |
| 76 | "Returns the string representation of the operator.") |
| 77 | .def( |
| 78 | "to_matrix", |
| 79 | [](const matrix_handler &self, |
| 80 | std::optional<dimension_map> dimensions, |
| 81 | std::optional<parameter_map> params) { |
| 82 | dimension_map dims = dimensions.value_or(dimension_map()); |
| 83 | parameter_map pm = params.value_or(parameter_map()); |
| 84 | auto cmat = self.to_matrix(dims, pm); |
| 85 | return detail::cmat_to_numpy(cmat); |
| 86 | }, |
| 87 | nanobind::arg("dimensions") = nanobind::none(), |
| 88 | nanobind::arg("parameters") = nanobind::none(), |
| 89 | "Returns the matrix representation of the operator.") |
| 90 | .def( |
| 91 | "to_matrix", |
| 92 | [](const matrix_handler &self, |
no test coverage detected