Commit 95d7fbd2 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

First pass of supporting FTQC



Hook codegen for If statements.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent b10fb2ff
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -80,6 +80,14 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,
      line += PP.getSpelling(Toks[i]);
      line += " ";
    }

    // If statement:
    if (Toks[i].is(clang::tok::TokenKind::kw_if)) {
      line += " ";
      i += 1;
      line += PP.getSpelling(Toks[i]);
    }

    last_col_number = col_number;
  }

@@ -93,9 +101,9 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,
  // Tracking the scope of for loops by their indent
  std::stack<int> for_loop_indent;
  for (const auto &line : lines) {
    // std::cout << "processing line " << line_counter << " of " << lines.size()
    //           << ": " << line.first << ", " << line.second << std::boolalpha
    //           << ", " << !for_loop_indent.empty() << "\n";
    std::cout << "processing line " << line_counter << " of " << lines.size()
              << ": " << line.first << ", " << line.second << std::boolalpha
              << ", " << !for_loop_indent.empty() << "\n";

    pyxasm_visitor visitor(bufferNames);
    // Should we close a 'for' scope after this statement
+36 −0
Original line number Diff line number Diff line
@@ -259,6 +259,42 @@ class pyxasm_visitor : public pyxasmBaseVisitor {
    return visitChildren(context);
  }

  virtual antlrcpp::Any
  visitIf_stmt(pyxasmParser::If_stmtContext *ctx) override {
    // Only support single clause atm
    if (ctx->test().size() == 1) {
      std::stringstream ss;
      std::string ifConditionExpr = ctx->test(0)->getText();

      if (ifConditionExpr.find("Measure") != std::string::npos) {
        // Found measure in an if statement instruction.
        const auto replaceMeasureInst = [](std::string &s,
                                           const std::string &search,
                                           const std::string &replace) {
          for (size_t pos = 0;; pos += replace.length()) {
            pos = s.find(search, pos);
            if (pos == std::string::npos) {
              break;
            }
            if (!isspace(s[pos + search.length()]) &&
                (s[pos + search.length()] != '(')) {
              continue;
            }
            s.erase(pos, search.length());
            s.insert(pos, replace);
          }
        };
        // Handle FTQC Measure in *if* conditional clause
        replaceMeasureInst(ifConditionExpr, "Measure", " quantum::mz");
      }

      ss << "if (" << ifConditionExpr << ")\n";
      result.first = ss.str();
      return 0;
    }
    return visitChildren(ctx);
  }

 private:
  // Replaces common Python constants, e.g. 'math.pi' or 'numpy.pi'.
  // Note: the library names have been resolved to their original names.