libqasm
library for handling cQASM files
cqasm-v1-values-gen.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <iostream>
9 #include "cqasm-tree.hpp"
10 #include "cqasm-v1-primitives.hpp"
11 #include "cqasm-v1-types.hpp"
12 
13 namespace cqasm {
14 namespace v1 {
15 
82 namespace values {
83 
84 // Base classes used to construct the tree.
86 template <class T> using Maybe = cqasm::tree::Maybe<T>;
87 template <class T> using One = cqasm::tree::One<T>;
88 template <class T> using Any = cqasm::tree::Any<T>;
89 template <class T> using Many = cqasm::tree::Many<T>;
90 template <class T> using OptLink = cqasm::tree::OptLink<T>;
91 template <class T> using Link = cqasm::tree::Link<T>;
92 
93 // Forward declarations for all classes.
94 class Node;
95 class BitRefs;
96 class ConstAxis;
97 class ConstBool;
98 class ConstComplex;
99 class ConstComplexMatrix;
100 class ConstInt;
101 class ConstJson;
102 class ConstReal;
103 class ConstRealMatrix;
104 class ConstString;
105 class Constant;
106 class Function;
107 class QubitRefs;
108 class Reference;
109 class VariableRef;
110 class VisitorBase;
111 template <typename T = void>
112 class Visitor;
113 class RecursiveVisitor;
114 class Dumper;
115 
119 enum class NodeType {
120  BitRefs,
121  ConstAxis,
122  ConstBool,
123  ConstComplex,
125  ConstInt,
126  ConstJson,
127  ConstReal,
129  ConstString,
130  Function,
131  QubitRefs,
133 };
134 
138 class Node : public Base {
139 public:
140 
144  virtual NodeType type() const = 0;
145 
149  virtual One<Node> copy() const = 0;
150 
154  virtual One<Node> clone() const = 0;
155 
159  virtual bool equals(const Node& rhs) const = 0;
160 
164  virtual bool operator==(const Node& rhs) const = 0;
165 
169  inline bool operator!=(const Node& rhs) const {
170  return !(*this == rhs);
171  }
172 
173 protected:
174 
178  virtual void visit_internal(VisitorBase &visitor, void *retval=nullptr) = 0;
179 
180 public:
181 
185  template <typename T>
186  T visit(Visitor<T> &visitor);
187 
191  void dump(std::ostream &out=std::cout, int indent=0);
192 
197  void dump_seq(std::ostream &out=std::cout, int indent=0);
198 
203  virtual BitRefs *as_bit_refs();
204 
209  virtual const BitRefs *as_bit_refs() const;
210 
215  virtual ConstAxis *as_const_axis();
216 
221  virtual const ConstAxis *as_const_axis() const;
222 
227  virtual ConstBool *as_const_bool();
228 
233  virtual const ConstBool *as_const_bool() const;
234 
239  virtual ConstComplex *as_const_complex();
240 
245  virtual const ConstComplex *as_const_complex() const;
246 
251  virtual ConstComplexMatrix *as_const_complex_matrix();
252 
257  virtual const ConstComplexMatrix *as_const_complex_matrix() const;
258 
263  virtual ConstInt *as_const_int();
264 
269  virtual const ConstInt *as_const_int() const;
270 
275  virtual ConstJson *as_const_json();
276 
281  virtual const ConstJson *as_const_json() const;
282 
287  virtual ConstReal *as_const_real();
288 
293  virtual const ConstReal *as_const_real() const;
294 
299  virtual ConstRealMatrix *as_const_real_matrix();
300 
305  virtual const ConstRealMatrix *as_const_real_matrix() const;
306 
311  virtual ConstString *as_const_string();
312 
317  virtual const ConstString *as_const_string() const;
318 
323  virtual Constant *as_constant();
324 
329  virtual const Constant *as_constant() const;
330 
335  virtual Function *as_function();
336 
341  virtual const Function *as_function() const;
342 
347  virtual QubitRefs *as_qubit_refs();
348 
353  virtual const QubitRefs *as_qubit_refs() const;
354 
359  virtual Reference *as_reference();
360 
365  virtual const Reference *as_reference() const;
366 
371  virtual VariableRef *as_variable_ref();
372 
377  virtual const VariableRef *as_variable_ref() const;
378 
382  virtual void serialize(
383  ::tree::cbor::MapWriter &map,
384  const ::tree::base::PointerMap &ids
385  ) const = 0;
386 
390  static std::shared_ptr<Node> deserialize(
391  const ::tree::cbor::MapReader &map,
392  ::tree::base::IdentifierMap &ids
393  );
394 
395 };
396 
400 class Reference : public Node {
401 public:
402 
407  Reference *as_reference() override;
408 
413  const Reference *as_reference() const override;
414 
418  static std::shared_ptr<Reference> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
419 
420 };
421 
427 class BitRefs : public Reference {
428 public:
429 
434 
438  BitRefs(const Many<ConstInt> &index = Many<ConstInt>());
439 
443  void find_reachable(::tree::base::PointerMap &map) const override;
444 
448  void check_complete(const ::tree::base::PointerMap &map) const override;
449 
453  NodeType type() const override;
454 
455 protected:
456 
460  void visit_internal(VisitorBase &visitor, void *retval) override;
461 
462 public:
463 
468  BitRefs *as_bit_refs() override;
469 
474  const BitRefs *as_bit_refs() const override;
475 
479  One<Node> copy() const override;
480 
484  One<Node> clone() const override;
485 
489  bool equals(const Node &rhs) const override;
490 
494  bool operator==(const Node &rhs) const override;
495 
499  void serialize(
500  ::tree::cbor::MapWriter &map,
501  const ::tree::base::PointerMap &ids
502  ) const override;
503 
507  static std::shared_ptr<BitRefs> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
508 
509 };
510 
514 class Constant : public Node {
515 public:
516 
521  Constant *as_constant() override;
522 
527  const Constant *as_constant() const override;
528 
532  static std::shared_ptr<Constant> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
533 
534 };
535 
539 class ConstAxis : public Constant {
540 public:
541 
546 
550  ConstAxis(const cqasm::v1::primitives::Axis &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::Axis>());
551 
555  void find_reachable(::tree::base::PointerMap &map) const override;
556 
560  void check_complete(const ::tree::base::PointerMap &map) const override;
561 
565  NodeType type() const override;
566 
567 protected:
568 
572  void visit_internal(VisitorBase &visitor, void *retval) override;
573 
574 public:
575 
580  ConstAxis *as_const_axis() override;
581 
586  const ConstAxis *as_const_axis() const override;
587 
591  One<Node> copy() const override;
592 
596  One<Node> clone() const override;
597 
601  bool equals(const Node &rhs) const override;
602 
606  bool operator==(const Node &rhs) const override;
607 
611  void serialize(
612  ::tree::cbor::MapWriter &map,
613  const ::tree::base::PointerMap &ids
614  ) const override;
615 
619  static std::shared_ptr<ConstAxis> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
620 
621 };
622 
626 class ConstBool : public Constant {
627 public:
628 
633 
637  ConstBool(const cqasm::v1::primitives::Bool &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::Bool>());
638 
642  void find_reachable(::tree::base::PointerMap &map) const override;
643 
647  void check_complete(const ::tree::base::PointerMap &map) const override;
648 
652  NodeType type() const override;
653 
654 protected:
655 
659  void visit_internal(VisitorBase &visitor, void *retval) override;
660 
661 public:
662 
667  ConstBool *as_const_bool() override;
668 
673  const ConstBool *as_const_bool() const override;
674 
678  One<Node> copy() const override;
679 
683  One<Node> clone() const override;
684 
688  bool equals(const Node &rhs) const override;
689 
693  bool operator==(const Node &rhs) const override;
694 
698  void serialize(
699  ::tree::cbor::MapWriter &map,
700  const ::tree::base::PointerMap &ids
701  ) const override;
702 
706  static std::shared_ptr<ConstBool> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
707 
708 };
709 
713 class ConstComplex : public Constant {
714 public:
715 
720 
724  ConstComplex(const cqasm::v1::primitives::Complex &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::Complex>());
725 
729  void find_reachable(::tree::base::PointerMap &map) const override;
730 
734  void check_complete(const ::tree::base::PointerMap &map) const override;
735 
739  NodeType type() const override;
740 
741 protected:
742 
746  void visit_internal(VisitorBase &visitor, void *retval) override;
747 
748 public:
749 
754  ConstComplex *as_const_complex() override;
755 
760  const ConstComplex *as_const_complex() const override;
761 
765  One<Node> copy() const override;
766 
770  One<Node> clone() const override;
771 
775  bool equals(const Node &rhs) const override;
776 
780  bool operator==(const Node &rhs) const override;
781 
785  void serialize(
786  ::tree::cbor::MapWriter &map,
787  const ::tree::base::PointerMap &ids
788  ) const override;
789 
793  static std::shared_ptr<ConstComplex> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
794 
795 };
796 
800 class ConstComplexMatrix : public Constant {
801 public:
802 
807 
811  ConstComplexMatrix(const cqasm::v1::primitives::CMatrix &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::CMatrix>());
812 
816  void find_reachable(::tree::base::PointerMap &map) const override;
817 
821  void check_complete(const ::tree::base::PointerMap &map) const override;
822 
826  NodeType type() const override;
827 
828 protected:
829 
833  void visit_internal(VisitorBase &visitor, void *retval) override;
834 
835 public:
836 
841  ConstComplexMatrix *as_const_complex_matrix() override;
842 
847  const ConstComplexMatrix *as_const_complex_matrix() const override;
848 
852  One<Node> copy() const override;
853 
857  One<Node> clone() const override;
858 
862  bool equals(const Node &rhs) const override;
863 
867  bool operator==(const Node &rhs) const override;
868 
872  void serialize(
873  ::tree::cbor::MapWriter &map,
874  const ::tree::base::PointerMap &ids
875  ) const override;
876 
880  static std::shared_ptr<ConstComplexMatrix> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
881 
882 };
883 
887 class ConstInt : public Constant {
888 public:
889 
894 
898  ConstInt(const cqasm::v1::primitives::Int &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::Int>());
899 
903  void find_reachable(::tree::base::PointerMap &map) const override;
904 
908  void check_complete(const ::tree::base::PointerMap &map) const override;
909 
913  NodeType type() const override;
914 
915 protected:
916 
920  void visit_internal(VisitorBase &visitor, void *retval) override;
921 
922 public:
923 
928  ConstInt *as_const_int() override;
929 
934  const ConstInt *as_const_int() const override;
935 
939  One<Node> copy() const override;
940 
944  One<Node> clone() const override;
945 
949  bool equals(const Node &rhs) const override;
950 
954  bool operator==(const Node &rhs) const override;
955 
959  void serialize(
960  ::tree::cbor::MapWriter &map,
961  const ::tree::base::PointerMap &ids
962  ) const override;
963 
967  static std::shared_ptr<ConstInt> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
968 
969 };
970 
974 class ConstJson : public Constant {
975 public:
976 
981 
985  ConstJson(const cqasm::v1::primitives::Str &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::Str>());
986 
990  void find_reachable(::tree::base::PointerMap &map) const override;
991 
995  void check_complete(const ::tree::base::PointerMap &map) const override;
996 
1000  NodeType type() const override;
1001 
1002 protected:
1003 
1007  void visit_internal(VisitorBase &visitor, void *retval) override;
1008 
1009 public:
1010 
1015  ConstJson *as_const_json() override;
1016 
1021  const ConstJson *as_const_json() const override;
1022 
1026  One<Node> copy() const override;
1027 
1031  One<Node> clone() const override;
1032 
1036  bool equals(const Node &rhs) const override;
1037 
1041  bool operator==(const Node &rhs) const override;
1042 
1046  void serialize(
1047  ::tree::cbor::MapWriter &map,
1048  const ::tree::base::PointerMap &ids
1049  ) const override;
1050 
1054  static std::shared_ptr<ConstJson> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
1055 
1056 };
1057 
1061 class ConstReal : public Constant {
1062 public:
1063 
1068 
1072  ConstReal(const cqasm::v1::primitives::Real &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::Real>());
1073 
1077  void find_reachable(::tree::base::PointerMap &map) const override;
1078 
1082  void check_complete(const ::tree::base::PointerMap &map) const override;
1083 
1087  NodeType type() const override;
1088 
1089 protected:
1090 
1094  void visit_internal(VisitorBase &visitor, void *retval) override;
1095 
1096 public:
1097 
1102  ConstReal *as_const_real() override;
1103 
1108  const ConstReal *as_const_real() const override;
1109 
1113  One<Node> copy() const override;
1114 
1118  One<Node> clone() const override;
1119 
1123  bool equals(const Node &rhs) const override;
1124 
1128  bool operator==(const Node &rhs) const override;
1129 
1133  void serialize(
1134  ::tree::cbor::MapWriter &map,
1135  const ::tree::base::PointerMap &ids
1136  ) const override;
1137 
1141  static std::shared_ptr<ConstReal> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
1142 
1143 };
1144 
1148 class ConstRealMatrix : public Constant {
1149 public:
1150 
1155 
1159  ConstRealMatrix(const cqasm::v1::primitives::RMatrix &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::RMatrix>());
1160 
1164  void find_reachable(::tree::base::PointerMap &map) const override;
1165 
1169  void check_complete(const ::tree::base::PointerMap &map) const override;
1170 
1174  NodeType type() const override;
1175 
1176 protected:
1177 
1181  void visit_internal(VisitorBase &visitor, void *retval) override;
1182 
1183 public:
1184 
1189  ConstRealMatrix *as_const_real_matrix() override;
1190 
1195  const ConstRealMatrix *as_const_real_matrix() const override;
1196 
1200  One<Node> copy() const override;
1201 
1205  One<Node> clone() const override;
1206 
1210  bool equals(const Node &rhs) const override;
1211 
1215  bool operator==(const Node &rhs) const override;
1216 
1220  void serialize(
1221  ::tree::cbor::MapWriter &map,
1222  const ::tree::base::PointerMap &ids
1223  ) const override;
1224 
1228  static std::shared_ptr<ConstRealMatrix> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
1229 
1230 };
1231 
1235 class ConstString : public Constant {
1236 public:
1237 
1242 
1246  ConstString(const cqasm::v1::primitives::Str &value = cqasm::v1::primitives::initialize<cqasm::v1::primitives::Str>());
1247 
1251  void find_reachable(::tree::base::PointerMap &map) const override;
1252 
1256  void check_complete(const ::tree::base::PointerMap &map) const override;
1257 
1261  NodeType type() const override;
1262 
1263 protected:
1264 
1268  void visit_internal(VisitorBase &visitor, void *retval) override;
1269 
1270 public:
1271 
1276  ConstString *as_const_string() override;
1277 
1282  const ConstString *as_const_string() const override;
1283 
1287  One<Node> copy() const override;
1288 
1292  One<Node> clone() const override;
1293 
1297  bool equals(const Node &rhs) const override;
1298 
1302  bool operator==(const Node &rhs) const override;
1303 
1307  void serialize(
1308  ::tree::cbor::MapWriter &map,
1309  const ::tree::base::PointerMap &ids
1310  ) const override;
1311 
1315  static std::shared_ptr<ConstString> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
1316 
1317 };
1318 
1324 class Function : public Node {
1325 public:
1326 
1331 
1336 
1341 
1345  Function(const cqasm::v1::primitives::Str &name = cqasm::v1::primitives::initialize<cqasm::v1::primitives::Str>(), const Any<cqasm::v1::values::Node> &operands = cqasm::v1::primitives::initialize<Any<cqasm::v1::values::Node>>(), const One<cqasm::v1::types::Node> &return_type = cqasm::v1::primitives::initialize<One<cqasm::v1::types::Node>>());
1346 
1350  void find_reachable(::tree::base::PointerMap &map) const override;
1351 
1355  void check_complete(const ::tree::base::PointerMap &map) const override;
1356 
1360  NodeType type() const override;
1361 
1362 protected:
1363 
1367  void visit_internal(VisitorBase &visitor, void *retval) override;
1368 
1369 public:
1370 
1375  Function *as_function() override;
1376 
1381  const Function *as_function() const override;
1382 
1386  One<Node> copy() const override;
1387 
1391  One<Node> clone() const override;
1392 
1396  bool equals(const Node &rhs) const override;
1397 
1401  bool operator==(const Node &rhs) const override;
1402 
1406  void serialize(
1407  ::tree::cbor::MapWriter &map,
1408  const ::tree::base::PointerMap &ids
1409  ) const override;
1410 
1414  static std::shared_ptr<Function> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
1415 
1416 };
1417 
1422 class QubitRefs : public Reference {
1423 public:
1424 
1429 
1433  QubitRefs(const Many<ConstInt> &index = Many<ConstInt>());
1434 
1438  void find_reachable(::tree::base::PointerMap &map) const override;
1439 
1443  void check_complete(const ::tree::base::PointerMap &map) const override;
1444 
1448  NodeType type() const override;
1449 
1450 protected:
1451 
1455  void visit_internal(VisitorBase &visitor, void *retval) override;
1456 
1457 public:
1458 
1463  QubitRefs *as_qubit_refs() override;
1464 
1469  const QubitRefs *as_qubit_refs() const override;
1470 
1474  One<Node> copy() const override;
1475 
1479  One<Node> clone() const override;
1480 
1484  bool equals(const Node &rhs) const override;
1485 
1489  bool operator==(const Node &rhs) const override;
1490 
1494  void serialize(
1495  ::tree::cbor::MapWriter &map,
1496  const ::tree::base::PointerMap &ids
1497  ) const override;
1498 
1502  static std::shared_ptr<QubitRefs> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
1503 
1504 };
1505 
1509 class VariableRef : public Reference {
1510 public:
1511 
1516 
1521 
1525  void find_reachable(::tree::base::PointerMap &map) const override;
1526 
1530  void check_complete(const ::tree::base::PointerMap &map) const override;
1531 
1535  NodeType type() const override;
1536 
1537 protected:
1538 
1542  void visit_internal(VisitorBase &visitor, void *retval) override;
1543 
1544 public:
1545 
1550  VariableRef *as_variable_ref() override;
1551 
1556  const VariableRef *as_variable_ref() const override;
1557 
1561  One<Node> copy() const override;
1562 
1566  One<Node> clone() const override;
1567 
1571  bool equals(const Node &rhs) const override;
1572 
1576  bool operator==(const Node &rhs) const override;
1577 
1581  void serialize(
1582  ::tree::cbor::MapWriter &map,
1583  const ::tree::base::PointerMap &ids
1584  ) const override;
1585 
1589  static std::shared_ptr<VariableRef> deserialize(const ::tree::cbor::MapReader &map, ::tree::base::IdentifierMap &ids);
1590 
1591 };
1592 
1597 public:
1598 
1602  virtual ~VisitorBase() = default;
1603 
1604 protected:
1605 
1606  friend class Node;
1607  friend class BitRefs;
1608  friend class ConstAxis;
1609  friend class ConstBool;
1610  friend class ConstComplex;
1611  friend class ConstComplexMatrix;
1612  friend class ConstInt;
1613  friend class ConstJson;
1614  friend class ConstReal;
1615  friend class ConstRealMatrix;
1616  friend class ConstString;
1617  friend class Constant;
1618  friend class Function;
1619  friend class QubitRefs;
1620  friend class Reference;
1621  friend class VariableRef;
1622 
1626  virtual void raw_visit_node(Node &node, void *retval) = 0;
1627 
1631  virtual void raw_visit_bit_refs(BitRefs &node, void *retval) = 0;
1632 
1636  virtual void raw_visit_const_axis(ConstAxis &node, void *retval) = 0;
1637 
1641  virtual void raw_visit_const_bool(ConstBool &node, void *retval) = 0;
1642 
1646  virtual void raw_visit_const_complex(ConstComplex &node, void *retval) = 0;
1647 
1651  virtual void raw_visit_const_complex_matrix(ConstComplexMatrix &node, void *retval) = 0;
1652 
1656  virtual void raw_visit_const_int(ConstInt &node, void *retval) = 0;
1657 
1661  virtual void raw_visit_const_json(ConstJson &node, void *retval) = 0;
1662 
1666  virtual void raw_visit_const_real(ConstReal &node, void *retval) = 0;
1667 
1671  virtual void raw_visit_const_real_matrix(ConstRealMatrix &node, void *retval) = 0;
1672 
1676  virtual void raw_visit_const_string(ConstString &node, void *retval) = 0;
1677 
1681  virtual void raw_visit_constant(Constant &node, void *retval) = 0;
1682 
1686  virtual void raw_visit_function(Function &node, void *retval) = 0;
1687 
1691  virtual void raw_visit_qubit_refs(QubitRefs &node, void *retval) = 0;
1692 
1696  virtual void raw_visit_reference(Reference &node, void *retval) = 0;
1697 
1701  virtual void raw_visit_variable_ref(VariableRef &node, void *retval) = 0;
1702 
1703 };
1704 
1715 template <typename T>
1716 class Visitor : public VisitorBase {
1717 protected:
1718 
1722  void raw_visit_node(Node &node, void *retval) override;
1723 
1727  void raw_visit_bit_refs(BitRefs &node, void *retval) override;
1728 
1732  void raw_visit_const_axis(ConstAxis &node, void *retval) override;
1733 
1737  void raw_visit_const_bool(ConstBool &node, void *retval) override;
1738 
1742  void raw_visit_const_complex(ConstComplex &node, void *retval) override;
1743 
1747  void raw_visit_const_complex_matrix(ConstComplexMatrix &node, void *retval) override;
1748 
1752  void raw_visit_const_int(ConstInt &node, void *retval) override;
1753 
1757  void raw_visit_const_json(ConstJson &node, void *retval) override;
1758 
1762  void raw_visit_const_real(ConstReal &node, void *retval) override;
1763 
1767  void raw_visit_const_real_matrix(ConstRealMatrix &node, void *retval) override;
1768 
1772  void raw_visit_const_string(ConstString &node, void *retval) override;
1773 
1777  void raw_visit_constant(Constant &node, void *retval) override;
1778 
1782  void raw_visit_function(Function &node, void *retval) override;
1783 
1787  void raw_visit_qubit_refs(QubitRefs &node, void *retval) override;
1788 
1792  void raw_visit_reference(Reference &node, void *retval) override;
1793 
1797  void raw_visit_variable_ref(VariableRef &node, void *retval) override;
1798 
1799 public:
1800 
1804  virtual T visit_node(Node &node) = 0;
1805 
1809  virtual T visit_bit_refs(BitRefs &node) {
1810  return visit_reference(node);
1811  }
1812 
1816  virtual T visit_const_axis(ConstAxis &node) {
1817  return visit_constant(node);
1818  }
1819 
1823  virtual T visit_const_bool(ConstBool &node) {
1824  return visit_constant(node);
1825  }
1826 
1831  return visit_constant(node);
1832  }
1833 
1838  return visit_constant(node);
1839  }
1840 
1844  virtual T visit_const_int(ConstInt &node) {
1845  return visit_constant(node);
1846  }
1847 
1851  virtual T visit_const_json(ConstJson &node) {
1852  return visit_constant(node);
1853  }
1854 
1858  virtual T visit_const_real(ConstReal &node) {
1859  return visit_constant(node);
1860  }
1861 
1866  return visit_constant(node);
1867  }
1868 
1872  virtual T visit_const_string(ConstString &node) {
1873  return visit_constant(node);
1874  }
1875 
1879  virtual T visit_constant(Constant &node) {
1880  return visit_node(node);
1881  }
1882 
1886  virtual T visit_function(Function &node) {
1887  return visit_node(node);
1888  }
1889 
1893  virtual T visit_qubit_refs(QubitRefs &node) {
1894  return visit_reference(node);
1895  }
1896 
1900  virtual T visit_reference(Reference &node) {
1901  return visit_node(node);
1902  }
1903 
1907  virtual T visit_variable_ref(VariableRef &node) {
1908  return visit_reference(node);
1909  }
1910 
1911 };
1912 
1916  template <typename T>
1917  void Visitor<T>::raw_visit_node(Node &node, void *retval) {
1918  if (retval == nullptr) {
1919  this->visit_node(node);
1920  } else {
1921  *((T*)retval) = this->visit_node(node);
1922  };
1923  }
1924 
1928  template <>
1929  void Visitor<void>::raw_visit_node(Node &node, void *retval);
1930 
1934  template <typename T>
1935  void Visitor<T>::raw_visit_bit_refs(BitRefs &node, void *retval) {
1936  if (retval == nullptr) {
1937  this->visit_bit_refs(node);
1938  } else {
1939  *((T*)retval) = this->visit_bit_refs(node);
1940  };
1941  }
1942 
1946  template <>
1947  void Visitor<void>::raw_visit_bit_refs(BitRefs &node, void *retval);
1948 
1952  template <typename T>
1953  void Visitor<T>::raw_visit_const_axis(ConstAxis &node, void *retval) {
1954  if (retval == nullptr) {
1955  this->visit_const_axis(node);
1956  } else {
1957  *((T*)retval) = this->visit_const_axis(node);
1958  };
1959  }
1960 
1964  template <>
1965  void Visitor<void>::raw_visit_const_axis(ConstAxis &node, void *retval);
1966 
1970  template <typename T>
1971  void Visitor<T>::raw_visit_const_bool(ConstBool &node, void *retval) {
1972  if (retval == nullptr) {
1973  this->visit_const_bool(node);
1974  } else {
1975  *((T*)retval) = this->visit_const_bool(node);
1976  };
1977  }
1978 
1982  template <>
1983  void Visitor<void>::raw_visit_const_bool(ConstBool &node, void *retval);
1984 
1988  template <typename T>
1990  if (retval == nullptr) {
1991  this->visit_const_complex(node);
1992  } else {
1993  *((T*)retval) = this->visit_const_complex(node);
1994  };
1995  }
1996 
2000  template <>
2001  void Visitor<void>::raw_visit_const_complex(ConstComplex &node, void *retval);
2002 
2006  template <typename T>
2008  if (retval == nullptr) {
2009  this->visit_const_complex_matrix(node);
2010  } else {
2011  *((T*)retval) = this->visit_const_complex_matrix(node);
2012  };
2013  }
2014 
2018  template <>
2020 
2024  template <typename T>
2025  void Visitor<T>::raw_visit_const_int(ConstInt &node, void *retval) {
2026  if (retval == nullptr) {
2027  this->visit_const_int(node);
2028  } else {
2029  *((T*)retval) = this->visit_const_int(node);
2030  };
2031  }
2032 
2036  template <>
2037  void Visitor<void>::raw_visit_const_int(ConstInt &node, void *retval);
2038 
2042  template <typename T>
2043  void Visitor<T>::raw_visit_const_json(ConstJson &node, void *retval) {
2044  if (retval == nullptr) {
2045  this->visit_const_json(node);
2046  } else {
2047  *((T*)retval) = this->visit_const_json(node);
2048  };
2049  }
2050 
2054  template <>
2055  void Visitor<void>::raw_visit_const_json(ConstJson &node, void *retval);
2056 
2060  template <typename T>
2061  void Visitor<T>::raw_visit_const_real(ConstReal &node, void *retval) {
2062  if (retval == nullptr) {
2063  this->visit_const_real(node);
2064  } else {
2065  *((T*)retval) = this->visit_const_real(node);
2066  };
2067  }
2068 
2072  template <>
2073  void Visitor<void>::raw_visit_const_real(ConstReal &node, void *retval);
2074 
2078  template <typename T>
2080  if (retval == nullptr) {
2081  this->visit_const_real_matrix(node);
2082  } else {
2083  *((T*)retval) = this->visit_const_real_matrix(node);
2084  };
2085  }
2086 
2090  template <>
2092 
2096  template <typename T>
2098  if (retval == nullptr) {
2099  this->visit_const_string(node);
2100  } else {
2101  *((T*)retval) = this->visit_const_string(node);
2102  };
2103  }
2104 
2108  template <>
2109  void Visitor<void>::raw_visit_const_string(ConstString &node, void *retval);
2110 
2114  template <typename T>
2115  void Visitor<T>::raw_visit_constant(Constant &node, void *retval) {
2116  if (retval == nullptr) {
2117  this->visit_constant(node);
2118  } else {
2119  *((T*)retval) = this->visit_constant(node);
2120  };
2121  }
2122 
2126  template <>
2127  void Visitor<void>::raw_visit_constant(Constant &node, void *retval);
2128 
2132  template <typename T>
2133  void Visitor<T>::raw_visit_function(Function &node, void *retval) {
2134  if (retval == nullptr) {
2135  this->visit_function(node);
2136  } else {
2137  *((T*)retval) = this->visit_function(node);
2138  };
2139  }
2140 
2144  template <>
2145  void Visitor<void>::raw_visit_function(Function &node, void *retval);
2146 
2150  template <typename T>
2151  void Visitor<T>::raw_visit_qubit_refs(QubitRefs &node, void *retval) {
2152  if (retval == nullptr) {
2153  this->visit_qubit_refs(node);
2154  } else {
2155  *((T*)retval) = this->visit_qubit_refs(node);
2156  };
2157  }
2158 
2162  template <>
2163  void Visitor<void>::raw_visit_qubit_refs(QubitRefs &node, void *retval);
2164 
2168  template <typename T>
2169  void Visitor<T>::raw_visit_reference(Reference &node, void *retval) {
2170  if (retval == nullptr) {
2171  this->visit_reference(node);
2172  } else {
2173  *((T*)retval) = this->visit_reference(node);
2174  };
2175  }
2176 
2180  template <>
2181  void Visitor<void>::raw_visit_reference(Reference &node, void *retval);
2182 
2186  template <typename T>
2187  void Visitor<T>::raw_visit_variable_ref(VariableRef &node, void *retval) {
2188  if (retval == nullptr) {
2189  this->visit_variable_ref(node);
2190  } else {
2191  *((T*)retval) = this->visit_variable_ref(node);
2192  };
2193  }
2194 
2198  template <>
2199  void Visitor<void>::raw_visit_variable_ref(VariableRef &node, void *retval);
2200 
2208 class RecursiveVisitor : public Visitor<void> {
2209 public:
2210 
2214  void visit_bit_refs(BitRefs &node) override;
2215 
2219  void visit_const_axis(ConstAxis &node) override;
2220 
2224  void visit_const_bool(ConstBool &node) override;
2225 
2229  void visit_const_complex(ConstComplex &node) override;
2230 
2234  void visit_const_complex_matrix(ConstComplexMatrix &node) override;
2235 
2239  void visit_const_int(ConstInt &node) override;
2240 
2244  void visit_const_json(ConstJson &node) override;
2245 
2249  void visit_const_real(ConstReal &node) override;
2250 
2254  void visit_const_real_matrix(ConstRealMatrix &node) override;
2255 
2259  void visit_const_string(ConstString &node) override;
2260 
2264  void visit_constant(Constant &node) override;
2265 
2269  void visit_function(Function &node) override;
2270 
2274  void visit_qubit_refs(QubitRefs &node) override;
2275 
2279  void visit_reference(Reference &node) override;
2280 
2284  void visit_variable_ref(VariableRef &node) override;
2285 
2286 };
2287 
2291 class Dumper : public RecursiveVisitor {
2292 protected:
2293 
2297  std::ostream &out;
2298 
2302  int indent = 0;
2303 
2307  ::tree::base::PointerMap *ids;
2311  bool in_link = false;
2312 
2316  void write_indent();
2317 
2318 public:
2319 
2323  Dumper(std::ostream &out, int indent=0, ::tree::base::PointerMap *ids = nullptr) : out(out), indent(indent), ids(ids) {};
2324 
2328  void visit_node(Node &node) override;
2332  void visit_bit_refs(BitRefs &node) override;
2333 
2337  void visit_const_axis(ConstAxis &node) override;
2338 
2342  void visit_const_bool(ConstBool &node) override;
2343 
2347  void visit_const_complex(ConstComplex &node) override;
2348 
2352  void visit_const_complex_matrix(ConstComplexMatrix &node) override;
2353 
2357  void visit_const_int(ConstInt &node) override;
2358 
2362  void visit_const_json(ConstJson &node) override;
2363 
2367  void visit_const_real(ConstReal &node) override;
2368 
2372  void visit_const_real_matrix(ConstRealMatrix &node) override;
2373 
2377  void visit_const_string(ConstString &node) override;
2378 
2382  void visit_constant(Constant &node) override;
2383 
2387  void visit_function(Function &node) override;
2388 
2392  void visit_qubit_refs(QubitRefs &node) override;
2393 
2397  void visit_reference(Reference &node) override;
2398 
2402  void visit_variable_ref(VariableRef &node) override;
2403 
2404 };
2405 
2409 template <typename T>
2411  T retval;
2412  this->visit_internal(visitor, &retval);
2413  return retval;
2414 }
2415 
2419 template <>
2420 void Node::visit(Visitor<void> &visitor);
2421 
2425 std::ostream &operator<<(std::ostream &os, const Node &object);
2426 
2427 } // namespace values
2428 } // namespace v1
2429 } // namespace cqasm
2430 
Two-dimensional matrix of some kind of type.
cqasm::v1::primitives::RMatrix value
The contained value.
virtual T visit_const_axis(ConstAxis &node)
Visitor function for ConstAxis nodes.
Represents a reference to some storage location.
virtual T visit_constant(Constant &node)
Fallback function for Constant nodes.
virtual T visit_variable_ref(VariableRef &node)
Visitor function for VariableRef nodes.
std::complex< double > Complex
Complex number primitive used within the semantic trees.
Internal class for implementing the visitor pattern.
::tree::base::Base Base
Definition: cqasm-tree.hpp:20
void raw_visit_function(Function &node, void *retval) override
Internal visitor function for Function nodes.
void raw_visit_const_real_matrix(ConstRealMatrix &node, void *retval) override
Internal visitor function for ConstRealMatrix nodes.
bool operator!=(const Node &rhs) const
Pointer-based inequality operator.
void raw_visit_const_bool(ConstBool &node, void *retval) override
Internal visitor function for ConstBool nodes.
::tree::base::Maybe< T > Maybe
Definition: cqasm-tree.hpp:23
virtual T visit_function(Function &node)
Visitor function for Function nodes.
This can be returned by user-defined functions as a placeholder value for something that needs to be ...
::tree::base::Link< T > Link
Definition: cqasm-tree.hpp:35
std::ostream & operator<<(std::ostream &os, const Value &value)
Stream << overload for a single value.
::tree::base::OptLink< T > OptLink
Definition: cqasm-tree.hpp:38
Represents a qubit, or a set of qubits for single-gate-multiple-qubit notation.
void raw_visit_const_real(ConstReal &node, void *retval) override
Internal visitor function for ConstReal nodes.
void raw_visit_node(Node &node, void *retval) override
Internal visitor function for nodes of any type.
Represents a variable reference.
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.
virtual T visit_const_complex(ConstComplex &node)
Visitor function for ConstComplex nodes.
Visitor base class defaulting to DFS pre-order traversal.
Represents a value of type string.
void raw_visit_const_int(ConstInt &node, void *retval) override
Internal visitor function for ConstInt nodes.
Base class for the visitor pattern for the tree.
virtual T visit_const_complex_matrix(ConstComplexMatrix &node)
Visitor function for ConstComplexMatrix nodes.
virtual T visit_const_real_matrix(ConstRealMatrix &node)
Visitor function for ConstRealMatrix nodes.
double Real
Real number primitive used within the AST and semantic trees.
Main class for all nodes.
virtual T visit_const_bool(ConstBool &node)
Visitor function for ConstBool nodes.
T visit(Visitor< T > &visitor)
Visit this object.
void raw_visit_const_string(ConstString &node, void *retval) override
Internal visitor function for ConstString nodes.
cqasm::v1::primitives::Bool value
The contained value.
cqasm::v1::primitives::Int value
The contained value.
cqasm::v1::primitives::Real value
The contained value.
std::int64_t Int
Integer primitive used within the AST and semantic trees.
void raw_visit_const_json(ConstJson &node, void *retval) override
Internal visitor function for ConstJson nodes.
Main class for all nodes.
Link< cqasm::v1::semantic::Variable > variable
The referenced variable.
Represents a value of type complex.
void serialize(const error_model::ErrorModelRef &obj, ::tree::cbor::MapWriter &map)
virtual T visit_const_real(ConstReal &node)
Visitor function for ConstReal nodes.
Many< ConstInt > index
The qubit index that this is a measurement bit for, starting at 0.
Represents a value of type bool.
Many< ConstInt > index
Set of qubit indices referred to, starting at 0.
virtual T visit_const_int(ConstInt &node)
Visitor function for ConstInt nodes.
cqasm::v1::primitives::Complex value
The contained value.
cqasm::v1::primitives::CMatrix value
The contained value.
virtual T visit_reference(Reference &node)
Fallback function for Reference nodes.
void raw_visit_const_axis(ConstAxis &node, void *retval) override
Internal visitor function for ConstAxis nodes.
void raw_visit_const_complex(ConstComplex &node, void *retval) override
Internal visitor function for ConstComplex nodes.
T initialize()
Generates a default value for the given primitive type.
bool Bool
Boolean primitive used within the semantic trees.
Visitor class that debug-dumps a tree to a stream.
virtual T visit_const_json(ConstJson &node)
Visitor function for ConstJson nodes.
cqasm::tree::Any< T > Any
cqasm::tree::Link< T > Link
Defines primitive types for use in trees generated by tree-gen.
std::ostream & out
Output stream to dump to.
Namespace for the "new" cQASM 1.x API.
cqasm::v1::primitives::Str name
Name of the function as it appears or should appear in the cQASM file.
Wrapper for pulling parts of tree-gen&#39;s support library into libqasm.
void raw_visit_bit_refs(BitRefs &node, void *retval) override
Internal visitor function for BitRefs nodes.
Represents a value of type json.
Represents an axis value (x, y, or z).
void raw_visit_reference(Reference &node, void *retval) override
Internal visitor function for Reference nodes.
Represents a value of type real_matrix.
cqasm::v1::primitives::Str value
The contained value.
::tree::base::Any< T > Any
Definition: cqasm-tree.hpp:29
virtual T visit_bit_refs(BitRefs &node)
Visitor function for BitRefs nodes.
std::string Str
String primitive used within the AST and semantic trees.
cqasm::v1::primitives::Axis value
The contained value.
::tree::base::Many< T > Many
Definition: cqasm-tree.hpp:32
void raw_visit_qubit_refs(QubitRefs &node, void *retval) override
Internal visitor function for QubitRefs nodes.
Represents a measurement bit, or a number of measurement bits for conditional gates with multiple con...
::tree::base::PointerMap * ids
When non-null, the print node IDs from here instead of link contents.
Represents a constant value.
Any< cqasm::v1::values::Node > operands
Operands for the function.
cqasm::tree::Base Base
One< cqasm::v1::types::Node > return_type
Operands for the function.
cqasm::tree::One< T > One
Represents a value of type int.
Represents a value of type real.
virtual T visit_qubit_refs(QubitRefs &node)
Visitor function for QubitRefs nodes.
cqasm::tree::Many< T > Many
::tree::base::One< T > One
Definition: cqasm-tree.hpp:26
void raw_visit_const_complex_matrix(ConstComplexMatrix &node, void *retval) override
Internal visitor function for ConstComplexMatrix nodes.
Represents a value of type complex_matrix.
Dumper(std::ostream &out, int indent=0, ::tree::base::PointerMap *ids=nullptr)
Construct a dumping visitor.
Axis
Axis primitive used within the semantic trees.
Defines the types of values available within cQASM&#39;s type system, as well as some utility functions...
cqasm::v1::primitives::Str value
The contained value.
NodeType
Enumeration of all node types.
void raw_visit_variable_ref(VariableRef &node, void *retval) override
Internal visitor function for VariableRef nodes.
cqasm::tree::OptLink< T > OptLink
virtual T visit_const_string(ConstString &node)
Visitor function for ConstString nodes.
void raw_visit_constant(Constant &node, void *retval) override
Internal visitor function for Constant nodes.
cqasm::tree::Maybe< T > Maybe