XACC
ionq_program.hpp
1 #pragma once
2 
3 #include "json.hpp"
4 
5 #include <stdexcept>
6 #include <regex>
7 #include <vector>
8 
9 namespace xacc {
10 namespace ionq {
11  using nlohmann::json;
12 
13  template <typename T>
14  inline T get_optional(const json & j, const char * property) {
15  if (j.find(property) != j.end()) {
16  return j.at(property).get<T>();
17  }
18  return T();
19  }
20 
21  template <typename T>
22  inline std::shared_ptr<T> get_optional(const json & j, std::string property) {
23  return get_optional<T>(j, property.data());
24  }
25 
27  public:
28  CircuitInstruction() = default;
29  virtual ~CircuitInstruction() = default;
30 
31  private:
32  std::string gate;
33  std::int64_t target;
34  std::int64_t control = 0;
35  double rotation = 0.0;
36 
37  public:
38  const std::string & get_gate() const { return gate; }
39  std::string & get_mutable_gate() { return gate; }
40  void set_gate(const std::string & value) { this->gate = value; }
41 
42  const int64_t & get_target() const { return target; }
43  int64_t & get_mutable_target() { return target; }
44  void set_target(const std::int64_t & value) { this->target = value; }
45 
46  std::int64_t get_control() const { return control; }
47  void set_control(std::int64_t value) { this->control = value; }
48 
49  double get_rotation() const { return rotation; }
50  void set_rotation(double value) { this->rotation = value; }
51  };
52 
53  class Body {
54  public:
55  Body() = default;
56  virtual ~Body() = default;
57 
58  private:
59  std::int64_t qubits;
60  std::vector<CircuitInstruction> circuit;
61 
62  public:
63  const int64_t & get_qubits() const { return qubits; }
64  int64_t & get_mutable_qubits() { return qubits; }
65  void set_qubits(const int64_t & value) { this->qubits = value; }
66 
67  const std::vector<CircuitInstruction> & get_circuit() const { return circuit; }
68  std::vector<CircuitInstruction> & get_mutable_circuit() { return circuit; }
69  void set_circuit(const std::vector<CircuitInstruction> & value) { this->circuit = value; }
70  };
71 
72  class IonQProgram {
73  public:
74  IonQProgram() = default;
75  virtual ~IonQProgram() = default;
76 
77  private:
78  std::string lang = "json";
79  std::string target;
80  std::int64_t shots;
81  Body body;
82 
83  public:
84  const std::string & get_lang() const { return lang; }
85  std::string & get_mutable_lang() { return lang; }
86  void set_lang(const std::string & value) { this->lang = value; }
87 
88  const std::string & get_target() const { return target; }
89  std::string & get_mutable_target() { return target; }
90  void set_target(const std::string & value) { this->target = value; }
91 
92  const std::int64_t & get_shots() const { return shots; }
93  std::int64_t & get_mutable_shots() { return shots; }
94  void set_shots(const std::int64_t & value) { this->shots = value; }
95 
96  const Body & get_body() const { return body; }
97  Body & get_mutable_body() { return body; }
98  void set_body(const Body & value) { this->body = value; }
99  };
100 }
101 }
102 
103 namespace nlohmann {
104  void from_json(const json & j, xacc::ionq::CircuitInstruction & x);
105  void to_json(json & j, const xacc::ionq::CircuitInstruction & x);
106 
107  void from_json(const json & j, xacc::ionq::Body & x);
108  void to_json(json & j, const xacc::ionq::Body & x);
109 
110  void from_json(const json & j, xacc::ionq::IonQProgram & x);
111  void to_json(json & j, const xacc::ionq::IonQProgram & x);
112 
113  inline void from_json(const json & j, xacc::ionq::CircuitInstruction& x) {
114  x.set_gate(j.at("gate").get<std::string>());
115  x.set_target(j.at("target").get<std::int64_t>());
116  x.set_control(xacc::ionq::get_optional<std::int64_t>(j, "control"));
117  x.set_rotation(xacc::ionq::get_optional<double>(j, "rotation"));
118  }
119 
120  inline void to_json(json & j, const xacc::ionq::CircuitInstruction & x) {
121  j = json::object();
122  j["gate"] = x.get_gate();
123  j["target"] = x.get_target();
124  if (x.get_gate() == "cnot") {
125  j["control"] = x.get_control();
126  }
127  if (x.get_gate() == "rx" ||x.get_gate() == "ry" || x.get_gate() == "rz") {
128  j["rotation"] = x.get_rotation();
129  }
130  }
131 
132  inline void from_json(const json & j, xacc::ionq::Body& x) {
133  x.set_qubits(j.at("qubits").get<std::int64_t>());
134  x.set_circuit(j.at("circuit").get<std::vector<xacc::ionq::CircuitInstruction>>());
135  }
136 
137  inline void to_json(json & j, const xacc::ionq::Body & x) {
138  j = json::object();
139  j["qubits"] = x.get_qubits();
140  j["circuit"] = x.get_circuit();
141  }
142 
143  inline void from_json(const json & j, xacc::ionq::IonQProgram& x) {
144  x.set_lang(j.at("lang").get<std::string>());
145  x.set_target(j.at("target").get<std::string>());
146  x.set_shots(j.at("shots").get<std::int64_t>());
147  x.set_body(j.at("body").get<xacc::ionq::Body>());
148  }
149 
150  inline void to_json(json & j, const xacc::ionq::IonQProgram & x) {
151  j = json::object();
152  j["lang"] = x.get_lang();
153  j["target"] = x.get_target();
154  j["shots"] = x.get_shots();
155  j["body"] = x.get_body();
156  }
157 }
Definition: Accelerator.hpp:25
Definition: ionq_program.hpp:72
Definition: Backends.hpp:24
Definition: ionq_program.hpp:26
Definition: ionq_program.hpp:53