libqasm
library for handling cQASM files
cqasm-v1-primitives.cpp
Go to the documentation of this file.
1 
6 
7 namespace cqasm {
8 namespace v1 {
9 namespace primitives {
10 
11 template <>
12 Str initialize<Str>() { return ""; }
13 
14 template <>
15 void serialize(const Str &obj, ::tree::cbor::MapWriter &map) {
16  map.append_binary("x", obj);
17 }
18 
19 template <>
20 Str deserialize(const ::tree::cbor::MapReader &map) {
21  return map.at("x").as_binary();
22 }
23 
24 template <>
25 Bool initialize<Bool>() { return false; }
26 
27 template <>
28 void serialize(const Bool &obj, ::tree::cbor::MapWriter &map) {
29  map.append_bool("x", obj);
30 }
31 
32 template <>
33 Bool deserialize(const ::tree::cbor::MapReader &map) {
34  return map.at("x").as_bool();
35 }
36 
37 template <>
39 
40 template <>
41 void serialize(const Axis &obj, ::tree::cbor::MapWriter &map) {
42  switch (obj) {
43  case Axis::X: map.append_int("x", 0); break;
44  case Axis::Y: map.append_int("x", 1); break;
45  case Axis::Z: map.append_int("x", 2); break;
46  }
47 }
48 
49 template <>
50 Axis deserialize(const ::tree::cbor::MapReader &map) {
51  switch (map.at("x").as_int()) {
52  case 0: return Axis::X;
53  case 1: return Axis::Y;
54  case 2: return Axis::Z;
55  }
56  throw std::runtime_error("invalid value for axis enum during deserialization");
57 }
58 
59 template <>
60 Int initialize<Int>() { return 0; }
61 
62 template <>
63 void serialize(const Int &obj, ::tree::cbor::MapWriter &map) {
64  map.append_int("x", obj);
65 }
66 
67 template <>
68 Int deserialize(const ::tree::cbor::MapReader &map) {
69  return map.at("x").as_int();
70 }
71 
72 template <>
73 Real initialize<Real>() { return 0.0; }
74 
75 template <>
76 void serialize(const Real &obj, ::tree::cbor::MapWriter &map) {
77  map.append_float("x", obj);
78 }
79 
80 template <>
81 Real deserialize(const ::tree::cbor::MapReader &map) {
82  return map.at("x").as_float();
83 }
84 
85 template <>
86 void serialize(const Complex &obj, ::tree::cbor::MapWriter &map) {
87  map.append_float("r", obj.real());
88  map.append_float("i", obj.imag());
89 }
90 
91 template <>
92 Complex deserialize(const ::tree::cbor::MapReader &map) {
93  return {map.at("r").as_float(), map.at("i").as_float()};
94 }
95 
96 template <>
97 void serialize(const RMatrix &obj, ::tree::cbor::MapWriter &map) {
98  map.append_int("c", obj.size_cols());
99  auto aw = map.append_array("d");
100  for (const auto &value : obj.get_data()) {
101  aw.append_float(value);
102  }
103  aw.close();
104 }
105 
106 template <>
107 RMatrix deserialize(const ::tree::cbor::MapReader &map) {
108  size_t num_cols = map.at("c").as_int();
109  auto ar = map.at("d").as_array();
110  std::vector<Real> data;
111  data.reserve(ar.size());
112  for (size_t i = 0; i < ar.size(); i++) {
113  data[i] = ar.at(i).as_float();
114  }
115  return {data, num_cols};
116 }
117 
118 template <>
119 void serialize(const CMatrix &obj, ::tree::cbor::MapWriter &map) {
120  map.append_int("c", obj.size_cols());
121  auto aw = map.append_array("d");
122  for (const auto &value : obj.get_data()) {
123  aw.append_float(value.real());
124  aw.append_float(value.imag());
125  }
126  aw.close();
127 }
128 
129 template <>
130 CMatrix deserialize(const ::tree::cbor::MapReader &map) {
131  size_t num_cols = map.at("c").as_int();
132  auto ar = map.at("d").as_array();
133  std::vector<Complex> data;
134  data.reserve(ar.size() / 2);
135  for (size_t i = 0; i < ar.size() / 2; i++) {
136  data[i] = {ar.at(i*2).as_float(), ar.at(i*2+1).as_float()};
137  }
138  return {data, num_cols};
139 }
140 
141 template <>
142 void serialize(const Version &obj, ::tree::cbor::MapWriter &map) {
143  auto aw = map.append_array("x");
144  for (auto x : obj) {
145  aw.append_int(x);
146  }
147  aw.close();
148 }
149 
150 template <>
151 Version deserialize(const ::tree::cbor::MapReader &map) {
152  auto ar = map.at("x").as_array();
153  auto v = Version("");
154  v.reserve(ar.size());
155  for (size_t i = 0; i < ar.size(); i++) {
156  v.push_back(ar.at(i).as_int());
157  }
158  return v;
159 }
160 
164 std::ostream &operator<<(std::ostream &os, const Axis &axis) {
165  switch (axis) {
166  case Axis::X:
167  os << "X";
168  break;
169  case Axis::Y:
170  os << "Y";
171  break;
172  case Axis::Z:
173  os << "Z";
174  break;
175  }
176  return os;
177 }
178 
179 } // namespace primitives
180 } // namespace v1
181 } // namespace cqasm
Two-dimensional matrix of some kind of type.
std::complex< double > Complex
Complex number primitive used within the semantic trees.
error_model::ErrorModelRef deserialize(const ::tree::cbor::MapReader &map)
Deserializes the given primitive object from CBOR.
Toplevel namespace with entry points for the new API.
double Real
Real number primitive used within the AST and semantic trees.
std::int64_t Int
Integer primitive used within the AST and semantic trees.
Version number primitive used within the AST and semantic trees.
void serialize(const error_model::ErrorModelRef &obj, ::tree::cbor::MapWriter &map)
bool Bool
Boolean primitive used within the semantic trees.
Defines primitive types for use in trees generated by tree-gen.
Namespace for the "new" cQASM 1.x API.
std::string Str
String primitive used within the AST and semantic trees.
version::Version Version
Version number primitive used within the AST and semantic trees.
std::ostream & operator<<(std::ostream &os, const Axis &axis)
Stream << overload for axis nodes.
size_t size_cols() const
Returns the number of columns.
const std::vector< T > & get_data() const
Returns access to the raw data vector.
Axis
Axis primitive used within the semantic trees.