| 93 | } |
| 94 | |
| 95 | void bindFermionOperator(nanobind::module_ &mod) { |
| 96 | |
| 97 | auto fermion_op_class = nanobind::class_<fermion_op>(mod, "FermionOperator"); |
| 98 | auto fermion_op_term_class = |
| 99 | nanobind::class_<fermion_op_term>(mod, "FermionOperatorTerm"); |
| 100 | |
| 101 | fermion_op_class |
| 102 | .def( |
| 103 | "__iter__", |
| 104 | [](fermion_op &self) { |
| 105 | nanobind::list items; |
| 106 | for (auto it = self.begin(); it != self.end(); ++it) |
| 107 | items.append(nanobind::cast(*it)); |
| 108 | return items.attr("__iter__")(); |
| 109 | }, |
| 110 | "Loop through each term of the operator.") |
| 111 | |
| 112 | // properties |
| 113 | |
| 114 | .def_prop_ro("parameters", &fermion_op::get_parameter_descriptions, |
| 115 | "Returns a dictionary that maps each parameter " |
| 116 | "name to its description.") |
| 117 | .def_prop_ro("degrees", &fermion_op::degrees, |
| 118 | "Returns a vector that lists all degrees of " |
| 119 | "freedom that the operator targets. " |
| 120 | "The order of degrees is from smallest to largest " |
| 121 | "and reflects the ordering of " |
| 122 | "the matrix returned by `to_matrix`. " |
| 123 | "Specifically, the indices of a statevector " |
| 124 | "with two qubits are {00, 01, 10, 11}. An " |
| 125 | "ordering of degrees {0, 1} then indicates " |
| 126 | "that a state where the qubit with index 0 equals " |
| 127 | "1 with probability 1 is given by " |
| 128 | "the vector {0., 1., 0., 0.}.") |
| 129 | .def_prop_ro("min_degree", &fermion_op::min_degree, |
| 130 | "Returns the smallest index of the degrees of " |
| 131 | "freedom that the operator targets.") |
| 132 | .def_prop_ro("max_degree", &fermion_op::max_degree, |
| 133 | "Returns the smallest index of the degrees of " |
| 134 | "freedom that the operator targets.") |
| 135 | .def_prop_ro("term_count", &fermion_op::num_terms, |
| 136 | "Returns the number of terms in the operator.") |
| 137 | |
| 138 | // constructors |
| 139 | |
| 140 | .def(nanobind::init<>(), |
| 141 | "Creates a default instantiated sum. A default instantiated " |
| 142 | "sum has no value; it will take a value the first time an " |
| 143 | "arithmetic operation " |
| 144 | "is applied to it. In that sense, it acts as both the additive and " |
| 145 | "multiplicative " |
| 146 | "identity. To construct a `0` value in the mathematical sense " |
| 147 | "(neutral element " |
| 148 | "for addition), use `empty()` instead.") |
| 149 | .def(nanobind::init<std::size_t>(), |
| 150 | "Creates a sum operator with no terms, reserving " |
| 151 | "space for the given number of terms.") |
| 152 | .def(nanobind::init<const fermion_op_term &>(), |
no test coverage detected