Maestro 0.3.1
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 {
20template <typename Time = Types::time_type>
22 public:
23 void clear() {
24 grammar.clear();
25 program.clear();
26 errorMessage.clear();
27 error = false;
28 }
29
30 // QASM3 `input` values are parse-time substitutions represented as doubles;
31 // no runtime classical state or symbolic value enters the Circuit IR.
32 std::shared_ptr<Circuits::Circuit<Time>> ParseAndTranslateWithParams(
33 const std::string& qasmInputStr,
34 const std::unordered_map<std::string, double>& params = {}) {
35 clear();
36
37 grammar.inputBindings = params;
38
39 std::string qasmInput = qasmInputStr;
40
41 return ParseAndTranslateImpl(qasmInput);
42 }
43
44 std::shared_ptr<Circuits::Circuit<Time>> ParseAndTranslate(
45 const std::string &qasmInputStr) {
46 clear();
47
48 std::string qasmInput = qasmInputStr;
49
50 return ParseAndTranslateImpl(qasmInput);
51 }
52
53 bool Failed() const { return error; }
54
55 const std::string &GetErrorMessage() const { return errorMessage; }
56
57 double GetVersion() const { return program.version; }
58
59 const std::vector<std::string> &GetComments() const {
60 return program.comments;
61 }
62
63 const std::vector<std::string> &GetIncludes() const {
64 return program.includes;
65 }
66
67 // The names declared by QASM3 `input` statements, in declaration order, so
68 // callers can discover what a circuit requires before supplying values via
69 // ParseAndTranslate's params map.
70 const std::vector<std::string> &GetInputs() const {
71 return grammar.inputNames;
72 }
73
74 protected:
75 std::shared_ptr<Circuits::Circuit<Time>> ParseAndTranslateImpl(
76 std::string& qasmInput) {
77 try {
78 auto it = qasmInput.begin();
79 QasmSkipper<std::string::iterator> skipper;
80 if (boost::spirit::qi::phrase_parse(it, qasmInput.end(), grammar, skipper,
81 program)) {
82 if (it == qasmInput.end()) {
83 grammar.ValidateInputBindings();
84 return program.ToCircuit<Time>(grammar.opaqueGates,
85 grammar.definedGates);
86 } else {
87 error = true;
88 errorMessage = "Error: Unparsed input remaining: '" +
89 std::string(it, qasmInput.end()) + "'";
90 }
91 } else {
92 error = true;
93 errorMessage = "Error: Parsing failed, unparsed input remaining: '" +
94 std::string(it, qasmInput.end()) + "'";
95 }
96 } catch (const std::exception& ex) {
97 error = true;
98 errorMessage = ex.what();
99 }
100
101 return nullptr;
102 }
103
104 qasm::QasmGrammar<> grammar;
106 std::string errorMessage;
107 bool error = false;
108};
109} // namespace qasm
110
111#endif //_QASMCIRC_H_
bool Failed() const
Definition QasmCirc.h:53
qasm::Program program
Definition QasmCirc.h:105
std::shared_ptr< Circuits::Circuit< Time > > ParseAndTranslateWithParams(const std::string &qasmInputStr, const std::unordered_map< std::string, double > &params={})
Definition QasmCirc.h:32
double GetVersion() const
Definition QasmCirc.h:57
const std::string & GetErrorMessage() const
Definition QasmCirc.h:55
const std::vector< std::string > & GetIncludes() const
Definition QasmCirc.h:63
std::shared_ptr< Circuits::Circuit< Time > > ParseAndTranslateImpl(std::string &qasmInput)
Definition QasmCirc.h:75
std::string errorMessage
Definition QasmCirc.h:106
std::shared_ptr< Circuits::Circuit< Time > > ParseAndTranslate(const std::string &qasmInputStr)
Definition QasmCirc.h:44
const std::vector< std::string > & GetComments() const
Definition QasmCirc.h:59
qasm::QasmGrammar grammar
Definition QasmCirc.h:104
const std::vector< std::string > & GetInputs() const
Definition QasmCirc.h:70