libqasm
library for handling cQASM files
cqasm-v1-instruction.cpp
Go to the documentation of this file.
1 
5 #include "cqasm-utils.hpp"
7 #include "cqasm-v1-semantic.hpp"
8 
9 namespace cqasm {
10 namespace v1 {
11 namespace instruction {
12 
27  const std::string &name,
28  const std::string &param_types,
29  bool allow_conditional,
30  bool allow_parallel,
31  bool allow_reused_qubits,
32  bool allow_different_index_sizes
33 ) :
34  name(name),
35  param_types(types::from_spec(param_types)),
36  allow_conditional(allow_conditional),
37  allow_parallel(allow_parallel),
38  allow_reused_qubits(allow_reused_qubits),
39  allow_different_index_sizes(allow_different_index_sizes)
40 {}
41 
45 bool Instruction::operator==(const Instruction& rhs) const {
47  && param_types == rhs.param_types
51 }
52 
56 std::ostream &operator<<(std::ostream &os, const Instruction &insn) {
57  os << insn.name << insn.param_types;
58  return os;
59 }
60 
64 std::ostream &operator<<(std::ostream &os, const InstructionRef &insn) {
65  if (insn.empty()) {
66  os << "unresolved";
67  } else {
68  os << *insn;
69  }
70  return os;
71 }
72 
73 } // namespace instruction
74 
75 namespace primitives {
76 
77 template <>
78 void serialize(const instruction::InstructionRef &obj, ::tree::cbor::MapWriter &map) {
79  if (obj.empty()) {
80  return;
81  }
82  map.append_string("n", obj->name);
83  map.append_bool("c", obj->allow_conditional);
84  map.append_bool("p", obj->allow_parallel);
85  map.append_bool("r", obj->allow_reused_qubits);
86  map.append_bool("d", obj->allow_different_index_sizes);
87  auto aw = map.append_array("t");
88  for (const auto &t : obj->param_types) {
89  aw.append_binary(::tree::base::serialize(t));
90  }
91  aw.close();
92 }
93 
94 template <>
95 instruction::InstructionRef deserialize(const ::tree::cbor::MapReader &map) {
96  if (!map.count("n")) {
97  return {};
98  }
99  auto insn = tree::make<instruction::Instruction>(
100  map.at("n").as_string(),
101  "",
102  map.at("c").as_bool(),
103  map.at("p").as_bool(),
104  map.at("r").as_bool(),
105  map.at("d").as_bool()
106  );
107  auto ar = map.at("t").as_array();
108  for (size_t i = 0; i < ar.size(); i++) {
109  insn->param_types.add(::tree::base::deserialize<types::Node>(ar.at(i).as_binary()));
110  }
111  return std::move(insn);
112 }
113 
114 } // namespace primitives
115 } // namespace v1
116 } // namespace cqasm
void serialize(const instruction::InstructionRef &obj, ::tree::cbor::MapWriter &map)
tree::Maybe< Instruction > InstructionRef
Optional reference to an instruction, used within the semantic tree.
This file contains the Instruction class and support types, each instance representing an instruction...
std::ostream & operator<<(std::ostream &os, const Instruction &insn)
Stream << overload for instructions.
Types from_spec(const std::string &spec)
Constructs a set of types from a shorthand string representation.
Toplevel namespace with entry points for the new API.
bool allow_reused_qubits
Whether to allow usage of the same qubit in different arguments.
bool allow_conditional
Whether this instruction supports conditional execution by means of the c- notation.
Defines various utility functions.
instruction::InstructionRef deserialize(const ::tree::cbor::MapReader &map)
Deserializes the given primitive object from CBOR.
bool allow_parallel
Whether this instruction can be used in a bundle.
Defines the types for the cQASM semantic tree, based on the classes from cqasm::tree.
Representation of an available instruction (also known as gate) in the instruction set...
bool operator==(const Instruction &rhs) const
Equality operator.
types::Types param_types
The vector of parameter types that this instruction expects.
Namespace for the "new" cQASM 1.x API.
bool case_insensitive_equals(const std::string &lhs, const std::string &rhs)
Case-insensitive string compare.
Definition: cqasm-utils.cpp:26
Instruction(const std::string &name, const std::string &param_types="", bool allow_conditional=true, bool allow_parallel=true, bool allow_reused_qubits=false, bool allow_different_index_sizes=false)
Creates a new instruction.
std::string name
The name of the instruction.