34 #ifndef QASM_PREPROCESSOR_H_ 35 #define QASM_PREPROCESSOR_H_ 44 static const std::string std_include_qasm =
45 "gate u3(theta,phi,lambda) q { U(theta,phi,lambda) q; }\n" 46 "gate u2(phi,lambda) q { U(pi/2,phi,lambda) q; }\n" 47 "gate u1(lambda) q { U(0,0,lambda) q; }\n" 48 "gate cx c,t { CX c,t; }\n" 49 "gate id a { U(0,0,0) a; }\n" 50 "gate u0(gamma) q { U(0,0,0) q; }\n" 51 "gate x a { u3(pi,0,pi) a; }\n" 52 "gate y a { u3(pi,pi/2,pi/2) a; }\n" 53 "gate z a { u1(pi) a; }\n" 54 "gate h a { u2(0,pi) a; }\n" 55 "gate s a { u1(pi/2) a; }\n" 56 "gate sdg a { u1(-pi/2) a; }\n" 57 "gate t a { u1(pi/4) a; }\n" 58 "gate tdg a { u1(-pi/4) a; }\n" 59 "gate rx(theta) a { u3(theta, -pi/2,pi/2) a; }\n" 60 "gate ry(theta) a { u3(theta,0,0) a; }\n" 61 "gate rz(phi) a { u1(phi) a; }\n" 62 "gate cz a,b { h b; cx a,b; h b; }\n" 63 "gate cy a,b { sdg b; cx a,b; s b; }\n" 64 "gate swap a,b { cx a,b; cx b,a; cx a,b; }\n" 65 "gate ch a,b { h b; sdg b; cx a,b; h b; t b; cx a,b; t b; h b; s b; x b; s " 67 "gate ccx a,b,c { h c; cx b,c; tdg c; cx a,c; t c; cx b,c; tdg c; cx a,c; " 68 "t b; t c; h c; cx a,b; t a; tdg b; cx a,b; }\n" 69 "gate crz(lambda) a,b { u1(lambda/2) b; cx a,b; u1(-lambda/2) b; cx a,b; " 71 "gate cu1(lambda) a,b { u1(lambda/2) a; cx a,b; u1(-lambda/2) b; cx a,b; " 73 "gate cu3(theta,phi,lambda) c,t { u1((lambda-phi)/2) t; cx c,t; " 74 "u3(-theta/2,0,-(phi+lambda)/2) t; cx c,t; u3(theta/2,phi,0) t; }\n";
83 static const std::string std_include =
84 "gate u3(theta,phi,lambda) q { U(theta,phi,lambda) q; }\n" 85 "gate u2(phi,lambda) q { U(pi/2,phi,lambda) q; }\n" 86 "gate u1(lambda) q { U(0,0,lambda) q; }\n" 87 "gate cx c,t { CX c,t; }\n" 88 "gate id a { U(0,0,0) a; }\n" 89 "gate u0(gamma) q { U(0,0,0) q; }\n" 90 "gate x a { u3(pi,0,pi) a; }\n" 91 "gate y a { u3(pi,pi/2,pi/2) a; }\n" 92 "gate z a { u1(pi) a; }\n" 93 "gate h a { u2(0,pi) a; }\n" 94 "gate s a { u1(pi/2) a; }\n" 95 "gate sdg a { u1(-pi/2) a; }\n" 96 "gate t a { u1(pi/4) a; }\n" 97 "gate tdg a { u1(-pi/4) a; }\n" 98 "gate r(theta, phi) a { u3(theta,phi-pi/2,-phi+pi/2) a; }\n" 99 "gate rx(theta) a { r(theta,0) a; } \n" 100 "gate ry(theta) a { r(theta,pi/2) a; }\n" 101 "gate rz(phi) a { u1(phi) a; } \n" 102 "gate cz a,b { h b; cx a,b; h b; }\n" 103 "gate cy a,b { sdg b; cx a,b; s b; }\n" 104 "gate swap a,b { cx a,b; cx b,a; cx a,b; }\n" 105 "gate cswap a,b,c { cx c,b; ccx a,b,c; cx c,b; }\n" 106 "gate ch a,b { s b; h b; t b; cx a,b; tdg b; h b; sdg b; }\n" 107 "gate ccx a,b,c { h c; cx b,c; tdg c; cx a,c; t c; cx b,c; tdg c; cx a,c; " 108 "t b; t c; h c; cx a,b; t a; tdg b; cx a,b; }\n" 109 "gate crz(lambda) a,b { u1(lambda/2) b; cx a,b; u1(-lambda/2) b; cx a,b; " 111 "gate cu1(lambda) a,b { u1(lambda/2) a; cx a,b; u1(-lambda/2) b; cx a,b; " 112 "u1(lambda/2) b; }\n" 113 "gate cu3(theta,phi,lambda) c,t { u1((lambda+phi)/2) c; " 114 "u1((lambda-phi)/2) t; cx c,t; u3(-theta/2,0,-(phi+lambda)/2) t; cx c,t; " 115 "u3(theta/2,phi,0) t; }\n";
123 using LexerPtr = std::unique_ptr<Lexer>;
125 std::vector<LexerPtr> lexer_stack_{};
126 LexerPtr current_lexer_ =
nullptr;
132 Preprocessor() =
default;
143 bool add_target_file(
const std::string& file_path) {
144 std::shared_ptr<std::ifstream> ifs(
new std::ifstream);
146 ifs->open(file_path, std::ifstream::in);
152 if (current_lexer_ !=
nullptr) {
153 lexer_stack_.push_back(std::move(current_lexer_));
155 current_lexer_ = std::unique_ptr<Lexer>(
new Lexer(ifs, file_path));
168 void add_target_stream(std::shared_ptr<std::istream> buffer,
169 const std::string& fname =
"") {
170 if (current_lexer_ !=
nullptr) {
171 lexer_stack_.push_back(std::move(current_lexer_));
173 current_lexer_ = std::unique_ptr<Lexer>(
new Lexer(buffer, fname));
186 if (current_lexer_ ==
nullptr) {
187 return Token(Location(), Token::Kind::eof,
"");
189 auto token = current_lexer_->next_token();
190 if (token.is(Token::Kind::kw_include)) {
192 token = current_lexer_->next_token();
193 }
else if (token.is(Token::Kind::eof)) {
194 if (!lexer_stack_.empty()) {
195 current_lexer_ = std::move(lexer_stack_.back());
196 lexer_stack_.pop_back();
197 token = current_lexer_->next_token();
199 current_lexer_ =
nullptr;
208 void print_all_tokens() {
209 Token current = next_token();
211 while (!current.is(Token::Kind::eof)) {
212 current.location().display(std::cout);
213 std::cout <<
": " << current <<
" " << (std::string) current
216 current = next_token();
228 void handle_include() {
229 auto token = current_lexer_->next_token();
230 if (token.is_not(Token::Kind::string)) {
231 std::cerr <<
"Error: Include must be followed by a file name\n";
235 std::string target = token;
236 token = current_lexer_->next_token();
237 if (token.is_not(Token::Kind::semicolon)) {
238 std::cerr <<
"Warning: Missing a ';'\n";
240 if (add_target_file(target)) {
242 }
else if (target ==
"qelib1.inc") {
243 add_target_stream(std::shared_ptr<std::istream>(
244 new std::stringstream(std_include)),
248 std::cerr <<
"Error: Couldn't open file " << target <<
"\n";
Quantum++ main namespace.
Definition: circuits.h:35