diff --git a/exe/main.cpp b/exe/main.cpp index f94bc041..c1384e7d 100644 --- a/exe/main.cpp +++ b/exe/main.cpp @@ -41,6 +41,23 @@ int main() { states = j; std::cout << states << std::endl; } + { + double coef = 2.0; + std::string pauli_string = "X 0 Z 1 Y 2"; + PauliOperator pauli(pauli_string, coef); + Json j = pauli; + std::cout << j.dump(2) << std::endl; + pauli = j; + } + { + std::uint64_t n_qubits = 3; + Operator op(n_qubits); + op.add_operator({0b001, 0b010, Complex(2)}); + op.add_operator({"X 2 Y 1", 1}); + Json j = op; + std::cout << j.dump(2) << std::endl; + op = j; + } Kokkos::finalize(); } diff --git a/include/scaluq/operator/operator.hpp b/include/scaluq/operator/operator.hpp index 85bccad6..30c6bec0 100644 --- a/include/scaluq/operator/operator.hpp +++ b/include/scaluq/operator/operator.hpp @@ -12,10 +12,11 @@ namespace scaluq { template class Operator { public: + Operator() = default; // for enable operator= from json explicit Operator(std::uint64_t n_qubits) : _n_qubits(n_qubits) {} - [[nodiscard]] inline bool is_hermitian() { return _is_hermitian; } - [[nodiscard]] inline std::uint64_t n_qubits() { return _n_qubits; } + [[nodiscard]] inline bool is_hermitian() const { return _is_hermitian; } + [[nodiscard]] inline std::uint64_t n_qubits() const { return _n_qubits; } [[nodiscard]] inline const std::vector>& terms() const { return _terms; } [[nodiscard]] std::string to_string() const; @@ -63,6 +64,22 @@ class Operator { Operator& operator*=(const PauliOperator& pauli); Operator operator*(const PauliOperator& pauli) const { return Operator(*this) *= pauli; } + friend void to_json(Json& j, const Operator& op) { + j = Json{{"n_qubits", op.n_qubits()}, {"terms", Json::array()}}; + for (const auto& pauli : op.terms()) { + Json tmp = pauli; + j["terms"].push_back(tmp); + } + } + friend void from_json(const Json& j, Operator& op) { + Operator res(j.at("n_qubits").get()); + for (const auto& pauli : j.at("terms")) { + PauliOperator tmp = pauli; + res.add_operator(tmp); + } + op = res; + } + private: std::vector> _terms; std::uint64_t _n_qubits; diff --git a/include/scaluq/operator/pauli_operator.hpp b/include/scaluq/operator/pauli_operator.hpp index 9b0491df..33dcc3df 100644 --- a/include/scaluq/operator/pauli_operator.hpp +++ b/include/scaluq/operator/pauli_operator.hpp @@ -55,6 +55,7 @@ class PauliOperator { public: enum PauliID : std::uint64_t { I, X, Y, Z }; + PauliOperator() = default; // for enable operator= from json explicit PauliOperator(Complex coef = 1.) : _ptr(std::make_shared(coef)) {} explicit PauliOperator(Data data) : _ptr(std::make_shared(data)) {} PauliOperator(std::string_view pauli_string, Complex coef = 1.) @@ -96,6 +97,14 @@ class PauliOperator { [[nodiscard]] inline PauliOperator operator*(Complex target) const { return PauliOperator(_ptr->_target_qubit_list, _ptr->_pauli_id_list, _ptr->_coef * target); } + + friend void to_json(Json& j, const PauliOperator& pauli) { + j = Json{{"pauli_string", pauli.get_pauli_string()}, {"coef", pauli.coef()}}; + } + friend void from_json(const Json& j, PauliOperator& pauli) { + pauli = PauliOperator(j.at("pauli_string").get(), + j.at("coef").get>()); + } }; #ifdef SCALUQ_USE_NANOBIND