Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
QasmCirc.h
Go to the documentation of this file.
1
12#pragma once
13
14#ifndef _QASMCIRC_H_
15#define _QASMCIRC_H_
16
17#include "Qasm.h"
18
19namespace qasm
20{
21template <typename Time = Types::time_type> class QasmToCirc
22{
23 public:
24 void clear()
25 {
26 grammar.clear();
27 program.clear();
28 errorMessage.clear();
29 error = false;
30 }
31
32 // QASM3 `input` values are parse-time substitutions represented as doubles;
33 // no runtime classical state or symbolic value enters the Circuit IR.
34 std::shared_ptr<Circuits::Circuit<Time>> ParseAndTranslateWithParams(const std::string &qasmInputStr,
35 const std::unordered_map<std::string, double> &params = {})
36 {
37 clear();
38
39 grammar.inputBindings = params;
40
41 std::string qasmInput = qasmInputStr;
42
43 return ParseAndTranslateImpl(qasmInput);
44 }
45
46 std::shared_ptr<Circuits::Circuit<Time>> ParseAndTranslate(const std::string &qasmInputStr)
47 {
48 clear();
49
50 std::string qasmInput = qasmInputStr;
51
52 return ParseAndTranslateImpl(qasmInput);
53 }
54
55 bool Failed() const
56 {
57 return error;
58 }
59
60 const std::string &GetErrorMessage() const
61 {
62 return errorMessage;
63 }
64
65 double GetVersion() const
66 {
67 return program.version;
68 }
69
70 const std::vector<std::string> &GetComments() const
71 {
72 return program.comments;
73 }
74
75 const std::vector<std::string> &GetIncludes() const
76 {
77 return program.includes;
78 }
79
80 // The names declared by QASM3 `input` statements, in declaration order, so
81 // callers can discover what a circuit requires before supplying values via
82 // ParseAndTranslate's params map.
83 const std::vector<std::string> &GetInputs() const
84 {
85 return grammar.inputNames;
86 }
87
88 protected:
89 std::shared_ptr<Circuits::Circuit<Time>> ParseAndTranslateImpl(std::string &qasmInput)
90 {
91 try
92 {
93 auto it = qasmInput.begin();
94 QasmSkipper<std::string::iterator> skipper;
95 if (boost::spirit::qi::phrase_parse(it, qasmInput.end(), grammar, skipper, program))
96 {
97 if (it == qasmInput.end())
98 {
99 grammar.ValidateInputBindings();
100 return program.ToCircuit<Time>(grammar.opaqueGates, grammar.definedGates);
101 }
102 else
103 {
104 error = true;
105 errorMessage = "Error: Unparsed input remaining: '" + std::string(it, qasmInput.end()) + "'";
106 }
107 }
108 else
109 {
110 error = true;
111 errorMessage = "Error: Parsing failed, unparsed input remaining: '" + std::string(it, qasmInput.end()) + "'";
112 }
113 }
114 catch (const std::exception &ex)
115 {
116 error = true;
117 errorMessage = ex.what();
118 }
119
120 return nullptr;
121 }
122
123 qasm::QasmGrammar<> grammar;
125 std::string errorMessage;
126 bool error = false;
127};
128} // namespace qasm
129
130#endif //_QASMCIRC_H_
bool Failed() const
Definition QasmCirc.h:55
qasm::Program program
Definition QasmCirc.h:124
std::shared_ptr< Circuits::Circuit< Time > > ParseAndTranslateWithParams(const std::string &qasmInputStr, const std::unordered_map< std::string, double > &params={})
Definition QasmCirc.h:34
double GetVersion() const
Definition QasmCirc.h:65
const std::string & GetErrorMessage() const
Definition QasmCirc.h:60
const std::vector< std::string > & GetIncludes() const
Definition QasmCirc.h:75
std::shared_ptr< Circuits::Circuit< Time > > ParseAndTranslateImpl(std::string &qasmInput)
Definition QasmCirc.h:89
std::string errorMessage
Definition QasmCirc.h:125
std::shared_ptr< Circuits::Circuit< Time > > ParseAndTranslate(const std::string &qasmInputStr)
Definition QasmCirc.h:46
const std::vector< std::string > & GetComments() const
Definition QasmCirc.h:70
qasm::QasmGrammar grammar
Definition QasmCirc.h:123
const std::vector< std::string > & GetInputs() const
Definition QasmCirc.h:83