13 #include "tree-cbor.hpp" 21 namespace primitives {
37 void serialize(
const T &obj, ::tree::cbor::MapWriter &map);
48 using Str = std::string;
52 void serialize(
const Str &obj, ::tree::cbor::MapWriter &map);
63 void serialize(
const Bool &obj, ::tree::cbor::MapWriter &map);
74 void serialize(
const Axis &obj, ::tree::cbor::MapWriter &map);
81 using Int = std::int64_t;
85 void serialize(
const Int &obj, ::tree::cbor::MapWriter &map);
96 void serialize(
const Real &obj, ::tree::cbor::MapWriter &map);
112 template <
typename T>
123 : data(ncols), nrows(1), ncols(0)
130 : data(ncols), nrows(1), ncols(ncols)
137 : data(nrows*ncols), nrows(nrows), ncols(ncols)
144 : data(data), nrows(data.size()), ncols(1)
152 Matrix(
const std::vector<T> &data,
size_t ncols)
153 : data(data), nrows(data.size() / ncols), ncols(ncols)
155 if (data.size() % ncols != 0) {
156 throw std::range_error(
"invalid matrix shape");
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");
189 return data[(row - 1) * ncols + col - 1];
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");
201 return data[(row - 1) * ncols + col - 1];
208 return data == rhs.data && nrows == rhs.nrows && ncols == rhs.ncols;
215 return !(*
this == rhs);
254 template <
typename T>
255 std::ostream &operator<<(std::ostream &os, const Matrix<T> &mat) {
257 for (
size_t row = 1; row <= mat.size_rows(); row++) {
261 for (
size_t col = 1; col <= mat.size_cols(); col++) {
265 os << mat.at(row, col);
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.
Bool initialize< Bool >()
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.
Real initialize< Real >()
Axis initialize< Axis >()
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.