31 operator std::string()
const {
32 return declType +
" " +
id +
"[" + std::to_string(
index) +
"]";
42 template <
typename,
typename>
struct result {
46 template <
typename ID,
typename IND>
61 boost::fusion::vector<std::string, std::vector<Expression>,
MixedListType>;
62using GatecallType = std::variant<SimpleGatecallType, ExpGatecallType>;
65 boost::fusion::vector<std::vector<Expression>,
ArgumentType>;
68using UopType = std::variant<UGateCallType, CXGateCallType, GatecallType>;
105 boost::fusion::vector<std::vector<std::string>, double,
106 std::vector<std::string>, std::vector<StatementType>>;
109using MeasureType = boost::fusion::vector<ArgumentType, ArgumentType>;
113using CondOpType = boost::fusion::vector<std::string, int, QopType>;
116 boost::fusion::vector<std::string, std::vector<std::string>,
117 std::vector<std::string>>;
121 boost::fusion::vector<std::string, std::vector<std::string>,
122 std::vector<std::string>>;
130 std::unordered_map<std::string, IndexedId> &creg_map,
133 id_copy.
base = counter;
135 counter +=
static_cast<int>(std::round(id_copy.
Eval()));
137 creg_map[id_copy.
id] = id_copy;
153 std::unordered_map<std::string, IndexedId> &qreg_map,
156 id_copy.
base = counter;
158 counter +=
static_cast<int>(std::round(id_copy.
Eval()));
159 qreg_map[id_copy.
id] = id_copy;
209 const std::unordered_map<std::string, IndexedId> &creg_map,
210 const std::unordered_map<std::string, IndexedId> &qreg_map)
const {
218 if (std::holds_alternative<IndexedId>(arg1)) {
219 IndexedId indexedId = std::get<IndexedId>(arg1);
220 auto it = qreg_map.find(indexedId.
id);
221 if (it != qreg_map.end()) {
222 int base = it->second.base;
225 }
else if (std::holds_alternative<std::string>(arg1)) {
226 std::string
id = std::get<std::string>(arg1);
227 auto it = qreg_map.find(
id);
228 if (it != qreg_map.end()) {
229 int base = it->second.base;
230 int size =
static_cast<int>(std::round(it->second.Eval()));
231 for (
int i = 0; i < size; ++i)
232 stmt.
qubits.push_back(base + i);
240 if (std::holds_alternative<IndexedId>(arg2)) {
241 IndexedId indexedId = std::get<IndexedId>(arg2);
242 auto it = creg_map.find(indexedId.
id);
243 if (it != creg_map.end()) {
244 int base = it->second.base;
247 }
else if (std::holds_alternative<std::string>(arg2)) {
248 std::string
id = std::get<std::string>(arg2);
249 auto it = creg_map.find(
id);
250 if (it != creg_map.end()) {
251 int base = it->second.base;
252 int size =
static_cast<int>(std::round(it->second.Eval()));
253 for (
int i = 0; i < size; ++i)
254 stmt.
cbits.push_back(base + i);
271 const std::unordered_map<std::string, IndexedId> &qreg_map)
const {
277 if (std::holds_alternative<IndexedId>(reset)) {
278 IndexedId indexedId = std::get<IndexedId>(reset);
279 auto it = qreg_map.find(indexedId.
id);
280 if (it != qreg_map.end()) {
281 int base = it->second.base;
284 }
else if (std::holds_alternative<std::string>(reset)) {
285 std::string
id = std::get<std::string>(reset);
286 auto it = qreg_map.find(
id);
287 if (it != qreg_map.end()) {
288 int base = it->second.base;
289 int size =
static_cast<int>(std::round(it->second.Eval()));
290 for (
int i = 0; i < size; ++i)
291 stmt.
qubits.push_back(base + i);
308 const std::unordered_map<std::string, IndexedId> &qreg_map)
const {
311 std::set<int> qubit_set;
313 for (
const auto &b : barrier) {
316 if (std::holds_alternative<IndexedId>(b)) {
317 IndexedId indexedId = std::get<IndexedId>(b);
318 auto it = qreg_map.find(indexedId.
id);
319 if (it != qreg_map.end()) {
320 int base = it->second.base;
321 qubit_set.insert(base + indexedId.
index);
323 }
else if (std::holds_alternative<std::string>(b)) {
324 std::string
id = std::get<std::string>(b);
325 auto it = qreg_map.find(
id);
326 if (it != qreg_map.end()) {
327 int base = it->second.base;
328 int size =
static_cast<int>(std::round(it->second.Eval()));
329 for (
int i = 0; i < size; ++i)
330 qubit_set.insert(base + i);
335 stmt.
qubits.assign(qubit_set.begin(), qubit_set.end());
350 std::unordered_map<std::string, StatementType> &opaqueGates,
351 const std::unordered_map<std::string, IndexedId> &qreg_map)
const {
355 std::string gateName = boost::fusion::at_c<0>(opaqueDecl);
360 const std::vector<std::string> ¶ms = boost::fusion::at_c<1>(opaqueDecl);
361 const std::vector<std::string> &args = boost::fusion::at_c<2>(opaqueDecl);
367 opaqueGates[gateName] = stmt;
381 const boost::fusion::vector<
GateDeclType, std::vector<GateDeclOpType>>
383 std::unordered_map<std::string, StatementType> &definedGates)
const {
387 const GateDeclType &declInfo = boost::fusion::at_c<0>(gateDecl);
389 const std::string &gateName = boost::fusion::at_c<0>(declInfo);
390 const std::vector<std::string> ¶ms = boost::fusion::at_c<1>(declInfo);
391 const std::vector<std::string> &args = boost::fusion::at_c<2>(declInfo);
394 throw std::invalid_argument(
395 "Gate declaration must have at least one qubit argument: " +
397 else if (definedGates.find(gateName) !=
402 throw std::invalid_argument(
"Gate already defined: " + gateName);
408 const std::vector<GateDeclOpType> &declOps =
409 boost::fusion::at_c<1>(gateDecl);
411 for (
const auto &op : declOps) {
412 if (std::holds_alternative<UopType>(op)) {
413 const UopType &uop = std::get<UopType>(op);
423 definedGates[gateName] = stmt;
AbstractSyntaxTree()=default
IndexedId(const std::string &id, int index)
QuantumGateType
The type of quantum gates.
std::variant< UGateCallType, CXGateCallType, GatecallType > UopType
boost::fusion::vector< std::string, MixedListType > SimpleGatecallType
std::variant< double, int, std::string > SimpleExpType
MixedListType BarrierType
std::vector< ArgumentType > MixedListType
boost::fusion::vector< std::vector< std::string >, double, std::vector< std::string >, std::vector< StatementType > > ProgramType
boost::fusion::vector< std::string, std::vector< Expression >, MixedListType > ExpGatecallType
std::variant< SimpleGatecallType, ExpGatecallType > GatecallType
phx::function< AddQregExpr > AddQreg
boost::fusion::vector< ArgumentType, ArgumentType > CXGateCallType
boost::fusion::vector< std::vector< Expression >, ArgumentType > UGateCallType
phx::function< MakeIndexedIdExpression > MakeIndexedId
phx::function< AddCommentExpr > AddComment
phx::function< AddDeclarationExpr > AddDeclaration
phx::function< AddBarrierExpr > AddBarrier
boost::fusion::vector< std::string, std::vector< std::string >, std::vector< std::string > > GateDeclType
std::vector< std::string > SimpleBarrierType
phx::function< AddCregExpr > AddCreg
boost::fusion::vector< std::string, int, QopType > CondOpType
phx::function< AddOpaqueDeclExpr > AddOpaqueDecl
std::variant< std::string, IndexedId > ArgumentType
boost::fusion::vector< std::string, std::vector< std::string >, std::vector< std::string > > OpaqueDeclType
QoperationStatement StatementType
phx::function< AddMeasureExpr > AddMeasure
phx::function< AddGateDeclExpr > AddGateDecl
std::variant< UopType, SimpleBarrierType > GateDeclOpType
boost::fusion::vector< ArgumentType, ArgumentType > MeasureType
phx::function< AddResetExpr > AddReset
QoperationStatement operator()(const BarrierType &barrier, const std::unordered_map< std::string, IndexedId > &qreg_map) const
IndexedId operator()(int &counter, std::unordered_map< std::string, IndexedId > &creg_map, const IndexedId &id) const
QoperationStatement operator()(const IndexedId &id) const
QoperationStatement operator()(const boost::fusion::vector< GateDeclType, std::vector< GateDeclOpType > > &gateDecl, std::unordered_map< std::string, StatementType > &definedGates) const
QoperationStatement operator()(const MeasureType &measure, const std::unordered_map< std::string, IndexedId > &creg_map, const std::unordered_map< std::string, IndexedId > &qreg_map) const
QoperationStatement operator()(const OpaqueDeclType &opaqueDecl, std::unordered_map< std::string, StatementType > &opaqueGates, const std::unordered_map< std::string, IndexedId > &qreg_map) const
IndexedId operator()(int &counter, std::unordered_map< std::string, IndexedId > &qreg_map, const IndexedId &id) const
QoperationStatement operator()(const ResetType &reset, const std::unordered_map< std::string, IndexedId > &qreg_map) const
IndexedId operator()(const ID &id, IND index) const
std::vector< UopType > declOps
std::vector< std::string > qubitsDecl
std::vector< double > parameters
std::vector< std::string > paramsDecl
std::vector< int > qubits
Circuits::QuantumGateType gateType