pairinteraction
A Rydberg Interaction Calculator
Operator.py.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 Pairinteraction Developers
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "./Operator.py.hpp"
5
11
12#include <nanobind/eigen/sparse.h>
13#include <nanobind/nanobind.h>
14#include <nanobind/operators.h>
15#include <nanobind/stl/complex.h>
16#include <nanobind/stl/shared_ptr.h>
17#include <nanobind/stl/vector.h>
18
19namespace nb = nanobind;
20using namespace pairinteraction;
21
22template <typename T>
23static void declare_operator(nb::module_ &m, std::string const &type_name) {
24 std::string pyclass_name = "Operator" + type_name;
25 using basis_t = typename Operator<T>::basis_t;
26 using scalar_t = typename Operator<T>::scalar_t;
27 nb::class_<Operator<T>, TransformationBuilderInterface<scalar_t>> pyclass(m,
28 pyclass_name.c_str());
29 pyclass.def(nb::init<std::shared_ptr<const basis_t>>())
30 .def("get_basis", nb::overload_cast<>(&Operator<T>::get_basis, nb::const_))
31 .def("get_matrix", nb::overload_cast<>(&Operator<T>::get_matrix, nb::const_))
32 .def("get_transformation", &Operator<T>::get_transformation)
33 .def("get_rotator", &Operator<T>::get_rotator)
34 .def("get_sorter", &Operator<T>::get_sorter)
35 .def("get_indices_of_blocks", &Operator<T>::get_indices_of_blocks)
36 .def("transformed",
37 nb::overload_cast<const Transformation<scalar_t> &>(&Operator<T>::transformed,
38 nb::const_))
39 .def("transformed",
40 nb::overload_cast<const Sorting &>(&Operator<T>::transformed, nb::const_))
41 .def(scalar_t() * nb::self)
42 .def(nb::self * scalar_t())
43 .def(nb::self / scalar_t())
44 .def(nb::self + nb::self)
45 .def(nb::self - nb::self); // NOLINT
46}
47
48template <typename T>
49static void declare_operator_atom(nb::module_ &m, std::string const &type_name) {
50 std::string pyclass_name = "OperatorAtom" + type_name;
51 using basis_t = typename OperatorAtom<T>::basis_t;
52 nb::class_<OperatorAtom<T>, Operator<OperatorAtom<T>>> pyclass(m, pyclass_name.c_str());
53 pyclass.def(nb::init<std::shared_ptr<const basis_t>>())
54 .def(nb::init<std::shared_ptr<const basis_t>, OperatorType, int>());
55}
56
57template <typename T>
58static void declare_operator_pair(nb::module_ &m, std::string const &type_name) {
59 std::string pyclass_name = "OperatorPair" + type_name;
60 using basis_t = typename OperatorPair<T>::basis_t;
61 nb::class_<OperatorPair<T>, Operator<OperatorPair<T>>> pyclass(m, pyclass_name.c_str());
62 pyclass.def(nb::init<std::shared_ptr<const basis_t>>())
63 .def(nb::init<std::shared_ptr<const basis_t>, OperatorType>());
64}
65
66void bind_operator(nb::module_ &m) {
67 declare_operator<OperatorAtom<double>>(m, "OperatorAtomReal");
68 declare_operator<OperatorAtom<std::complex<double>>>(m, "OperatorAtomComplex");
69 declare_operator_atom<double>(m, "Real");
70 declare_operator_atom<std::complex<double>>(m, "Complex");
71
72 declare_operator<OperatorPair<double>>(m, "OperatorPairReal");
73 declare_operator<OperatorPair<std::complex<double>>>(m, "OperatorPairComplex");
74 declare_operator_pair<double>(m, "Real");
75 declare_operator_pair<std::complex<double>>(m, "Complex");
76}
void bind_operator(nb::module_ &m)
Definition: Operator.py.cpp:66
typename traits::CrtpTraits< Type >::basis_t basis_t
typename traits::CrtpTraits< Type >::basis_t basis_t
typename traits::CrtpTraits< Derived >::scalar_t scalar_t
Definition: Operator.hpp:44
typename traits::CrtpTraits< Derived >::basis_t basis_t
Definition: Operator.hpp:47