libqasm
library for handling cQASM files
cqasm-v1-primitives.hpp
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include <string>
8 #include <cstdint>
9 #include <complex>
10 #include <vector>
11 
12 #include "cqasm-version.hpp"
13 #include "tree-cbor.hpp"
14 
15 namespace cqasm {
16 namespace v1 {
17 
21 namespace primitives {
22 
30 template <class T>
31 T initialize() { return T(); };
32 
36 template <typename T>
37 void serialize(const T &obj, ::tree::cbor::MapWriter &map);
38 
42 template <typename T>
43 T deserialize(const ::tree::cbor::MapReader &map);
44 
48 using Str = std::string;
49 template <>
51 template <>
52 void serialize(const Str &obj, ::tree::cbor::MapWriter &map);
53 template <>
54 Str deserialize(const ::tree::cbor::MapReader &map);
55 
59 using Bool = bool;
60 template <>
62 template <>
63 void serialize(const Bool &obj, ::tree::cbor::MapWriter &map);
64 template <>
65 Bool deserialize(const ::tree::cbor::MapReader &map);
66 
70 enum class Axis { X, Y, Z };
71 template <>
73 template <>
74 void serialize(const Axis &obj, ::tree::cbor::MapWriter &map);
75 template <>
76 Axis deserialize(const ::tree::cbor::MapReader &map);
77 
81 using Int = std::int64_t;
82 template <>
84 template <>
85 void serialize(const Int &obj, ::tree::cbor::MapWriter &map);
86 template <>
87 Int deserialize(const ::tree::cbor::MapReader &map);
88 
92 using Real = double;
93 template <>
95 template <>
96 void serialize(const Real &obj, ::tree::cbor::MapWriter &map);
97 template <>
98 Real deserialize(const ::tree::cbor::MapReader &map);
99 
103 using Complex = std::complex<double>;
104 template <>
105 void serialize(const Complex &obj, ::tree::cbor::MapWriter &map);
106 template <>
107 Complex deserialize(const ::tree::cbor::MapReader &map);
108 
112 template <typename T>
113 class Matrix {
114 private:
115  std::vector<T> data;
116  size_t nrows;
117  size_t ncols;
118 public:
123  : data(ncols), nrows(1), ncols(0)
124  {}
125 
129  Matrix(size_t ncols)
130  : data(ncols), nrows(1), ncols(ncols)
131  {}
132 
136  Matrix(size_t nrows, size_t ncols)
137  : data(nrows*ncols), nrows(nrows), ncols(ncols)
138  {}
139 
143  Matrix(const std::vector<T> &data)
144  : data(data), nrows(data.size()), ncols(1)
145  {}
146 
152  Matrix(const std::vector<T> &data, size_t ncols)
153  : data(data), nrows(data.size() / ncols), ncols(ncols)
154  {
155  if (data.size() % ncols != 0) {
156  throw std::range_error("invalid matrix shape");
157  }
158  }
159 
163  size_t size_rows() const {
164  return nrows;
165  }
166 
170  size_t size_cols() const {
171  return ncols;
172  }
173 
177  const std::vector<T> &get_data() const {
178  return data;
179  }
180 
185  T at(size_t row, size_t col) const {
186  if (row < 1 || row > nrows || col < 1 || col > ncols) {
187  throw std::range_error("matrix index out of range");
188  }
189  return data[(row - 1) * ncols + col - 1];
190  }
191 
197  T &at(size_t row, size_t col) {
198  if (row < 1 || row > nrows || col < 1 || col > ncols) {
199  throw std::range_error("matrix index out of range");
200  }
201  return data[(row - 1) * ncols + col - 1];
202  }
203 
207  bool operator==(const Matrix<T> &rhs) const {
208  return data == rhs.data && nrows == rhs.nrows && ncols == rhs.ncols;
209  }
210 
214  bool operator!=(const Matrix<T> &rhs) const {
215  return !(*this == rhs);
216  }
217 };
218 
223 template <>
224 void serialize(const RMatrix &obj, ::tree::cbor::MapWriter &map);
225 template <>
226 RMatrix deserialize(const ::tree::cbor::MapReader &map);
227 
232 template <>
233 void serialize(const CMatrix &obj, ::tree::cbor::MapWriter &map);
234 template <>
235 CMatrix deserialize(const ::tree::cbor::MapReader &map);
236 
241 template <>
242 void serialize(const Version &obj, ::tree::cbor::MapWriter &map);
243 template <>
244 Version deserialize(const ::tree::cbor::MapReader &map);
245 
249 std::ostream &operator<<(std::ostream &os, const Axis &axis);
250 
254 template <typename T>
255 std::ostream &operator<<(std::ostream &os, const Matrix<T> &mat) {
256  os << "[";
257  for (size_t row = 1; row <= mat.size_rows(); row++) {
258  if (row > 1) {
259  os << "; ";
260  }
261  for (size_t col = 1; col <= mat.size_cols(); col++) {
262  if (col > 1) {
263  os << ", ";
264  }
265  os << mat.at(row, col);
266  }
267  }
268  os << "]";
269  return os;
270 }
271 
272 } // namespace primitives
273 } // namespace v1
274 } // namespace cqasm
Two-dimensional matrix of some kind of type.
std::complex< double > Complex
Complex number primitive used within the semantic trees.
Defines utilities for detecting and dealing with cQASM language versions.
size_t size_rows() const
Returns the number of rows.
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.
Matrix(const std::vector< T > &data, size_t ncols)
Creates a matrix with the given data.
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 operator!=(const Matrix< T > &rhs) const
Inequality operator for matrices.
T initialize()
Generates a default value for the given primitive type.
bool Bool
Boolean primitive used within the semantic trees.
Matrix(size_t nrows, size_t ncols)
Creates a zero-initialized matrix of the given size.
Namespace for the "new" cQASM 1.x API.
bool operator==(const Matrix< T > &rhs) const
Equality operator for matrices.
Matrix(const std::vector< T > &data)
Creates a column vector with the given data.
Matrix(size_t ncols)
Creates a vector.
T & at(size_t row, size_t col)
Returns a mutable reference to the value at the given position.
std::string Str
String 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.
Matrix()
Creates an empty matrix.
T at(size_t row, size_t col) const
Returns the value at the given position.