Skip to content

Commit

Permalink
operator
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalfr-KY committed Dec 10, 2024
1 parent aaea514 commit 97a4943
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
17 changes: 17 additions & 0 deletions exe/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> pauli(pauli_string, coef);
Json j = pauli;
std::cout << j.dump(2) << std::endl;
pauli = j;
}
{
std::uint64_t n_qubits = 3;
Operator<double> op(n_qubits);
op.add_operator({0b001, 0b010, Complex<double>(2)});
op.add_operator({"X 2 Y 1", 1});
Json j = op;
std::cout << j.dump(2) << std::endl;
op = j;
}

Kokkos::finalize();
}
21 changes: 19 additions & 2 deletions include/scaluq/operator/operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ namespace scaluq {
template <std::floating_point Fp>
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<PauliOperator<Fp>>& terms() const { return _terms; }
[[nodiscard]] std::string to_string() const;

Expand Down Expand Up @@ -63,6 +64,22 @@ class Operator {
Operator& operator*=(const PauliOperator<Fp>& pauli);
Operator operator*(const PauliOperator<Fp>& 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<Fp> res(j.at("n_qubits").get<std::uint32_t>());
for (const auto& pauli : j.at("terms")) {
PauliOperator<Fp> tmp = pauli;
res.add_operator(tmp);
}
op = res;
}

private:
std::vector<PauliOperator<Fp>> _terms;
std::uint64_t _n_qubits;
Expand Down
9 changes: 9 additions & 0 deletions include/scaluq/operator/pauli_operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Fp> coef = 1.) : _ptr(std::make_shared<const Data>(coef)) {}
explicit PauliOperator(Data data) : _ptr(std::make_shared<const Data>(data)) {}
PauliOperator(std::string_view pauli_string, Complex<Fp> coef = 1.)
Expand Down Expand Up @@ -96,6 +97,14 @@ class PauliOperator {
[[nodiscard]] inline PauliOperator operator*(Complex<Fp> 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<std::string>(),
j.at("coef").get<Kokkos::complex<Fp>>());
}
};

#ifdef SCALUQ_USE_NANOBIND
Expand Down

0 comments on commit 97a4943

Please sign in to comment.