|
| 1 | +#include "qbin_compiler/qasm_expander.hpp" |
| 2 | +#include "qbin_compiler/tools.hpp" |
| 3 | + |
| 4 | +#include <regex> |
| 5 | + |
| 6 | +using qbin_compiler::util::to_lower_ascii; |
| 7 | +using qbin_compiler::util::trim; |
| 8 | +using qbin_compiler::util::vlog; |
| 9 | + |
| 10 | +namespace qbin_compiler { |
| 11 | + namespace frontend { |
| 12 | + |
| 13 | + std::vector<std::string> StatementExpander::expand( |
| 14 | + const std::vector<std::string>& nondef_lines, |
| 15 | + const GateRegistry& gates, |
| 16 | + bool verbose) |
| 17 | + { |
| 18 | + std::vector<std::string> canonical; |
| 19 | + |
| 20 | + auto ensure_semi = [](const std::string& t) { |
| 21 | + if (!t.empty() && t.back() == ';') return t; |
| 22 | + std::string r = t; |
| 23 | + r.push_back(';'); |
| 24 | + return r; |
| 25 | + }; |
| 26 | + |
| 27 | + auto expand_recursive = [&](auto&& self, |
| 28 | + const std::string& stmt, |
| 29 | + std::vector<std::string>& out) -> void { |
| 30 | + std::string s = trim(stmt); |
| 31 | + if (s.empty()) return; |
| 32 | + std::string sl = to_lower_ascii(s); |
| 33 | + |
| 34 | + // skip barrier/reset |
| 35 | + if (sl.rfind("barrier", 0) == 0) { vlog(verbose, "skip barrier"); return; } |
| 36 | + if (sl.rfind("reset", 0) == 0) { vlog(verbose, "skip reset"); return; } |
| 37 | + |
| 38 | + // measure |
| 39 | + { |
| 40 | + static std::regex rem1(R"(^\s*measure\s+(.+?)\s*->\s*(.+?)\s*;?$)", std::regex::icase); |
| 41 | + static std::regex rem2(R"(^\s*(.+?)\s*=\s*measure\s+(.+?)\s*;?$)", std::regex::icase); |
| 42 | + std::smatch m; |
| 43 | + if (std::regex_match(s, m, rem1)) { |
| 44 | + std::string q = trim(m[1].str()); |
| 45 | + std::string c = trim(m[2].str()); |
| 46 | + out.push_back(ensure_semi(c + " = measure " + q)); |
| 47 | + return; |
| 48 | + } |
| 49 | + if (std::regex_match(s, m, rem2)) { |
| 50 | + out.push_back(ensure_semi(trim(m[1].str()) + " = measure " + trim(m[2].str()))); |
| 51 | + return; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // two-qubit gates |
| 56 | + { |
| 57 | + static std::regex r2(R"(^\s*(cx|cz|swap)\s+(.+?)\s*,\s*(.+?)\s*;?$)", std::regex::icase); |
| 58 | + std::smatch m; |
| 59 | + if (std::regex_match(s, m, r2)) { |
| 60 | + std::string op = to_lower_ascii(m[1].str()); |
| 61 | + std::string a = trim(m[2].str()); |
| 62 | + std::string b = trim(m[3].str()); |
| 63 | + out.push_back(ensure_semi(op + " " + a + ", " + b)); |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // one-qubit gates |
| 69 | + { |
| 70 | + static std::regex r1(R"(^\s*(h|x|y|z|s|sdg|t|tdg|sx|sxdg)\s+(.+?)\s*;?$)", std::regex::icase); |
| 71 | + std::smatch m; |
| 72 | + if (std::regex_match(s, m, r1)) { |
| 73 | + std::string op = to_lower_ascii(m[1].str()); |
| 74 | + std::string a = trim(m[2].str()); |
| 75 | + out.push_back(ensure_semi(op + " " + a)); |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // parametric gates |
| 81 | + { |
| 82 | + static std::regex rp(R"(^\s*(rx|ry|rz|phase)\s*\(\s*(.+?)\s*\)\s+(.+?)\s*;?$)", std::regex::icase); |
| 83 | + std::smatch m; |
| 84 | + if (std::regex_match(s, m, rp)) { |
| 85 | + std::string op = to_lower_ascii(m[1].str()); |
| 86 | + std::string expr = trim(m[2].str()); |
| 87 | + std::string a = trim(m[3].str()); |
| 88 | + out.push_back(ensure_semi(op + "(" + expr + ") " + a)); |
| 89 | + return; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // custom gate calls |
| 94 | + { |
| 95 | + static std::regex rcall(R"(^\s*([A-Za-z_]\w*)\s+(.+?)\s*;?$)"); |
| 96 | + std::smatch m; |
| 97 | + if (std::regex_match(s, m, rcall)) { |
| 98 | + std::string name = to_lower_ascii(trim(m[1].str())); |
| 99 | + std::string rest = trim(m[2].str()); |
| 100 | + if (gates.hasGate(name)) { |
| 101 | + auto args = util::split_commas(rest, true); |
| 102 | + auto expanded = gates.getGate(name).expand(args); |
| 103 | + for (auto& e : expanded) { |
| 104 | + self(self, e, out); |
| 105 | + } |
| 106 | + return; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // fallback: keep as-is |
| 112 | + out.push_back(ensure_semi(s)); |
| 113 | + }; |
| 114 | + |
| 115 | + // process all lines |
| 116 | + for (auto& s : nondef_lines) { |
| 117 | + std::vector<std::string> tmp; |
| 118 | + expand_recursive(expand_recursive, s, tmp); |
| 119 | + canonical.insert(canonical.end(), tmp.begin(), tmp.end()); |
| 120 | + } |
| 121 | + |
| 122 | + return canonical; |
| 123 | + } |
| 124 | + |
| 125 | + } // namespace frontend |
| 126 | +} // namespace qbin_compiler |
0 commit comments