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

Added custom printer and a simple test



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 4788b9bd
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -239,6 +239,7 @@ def YieldOp : QuantumOp<"yield", [NoSideEffect, Terminator]> {
  let summary = "conditional termination operation";
  let arguments = (ins Variadic<AnyType>:$results);
  let builders = [OpBuilderDAG<(ins), [{ /* nothing to do */ }]>];
  let printer = [{ p << "q.yield"; }];
}

def ConditionalOp : QuantumOp<"ifOp", [SingleBlockImplicitTerminator<"YieldOp">, RecursiveSideEffects, NoRegionArguments]> {
@@ -264,6 +265,23 @@ def ConditionalOp : QuantumOp<"ifOp", [SingleBlockImplicitTerminator<"YieldOp">,
      return OpBuilder::atBlockTerminator(body, listener);
    }
  }];
  
  let printer = [{  
    auto op = *this;
    p << "q.If " << op.result_bit();  
    p.printRegion(op.thenRegion(),
                /*printEntryBlockArgs=*/false,
                /*printBlockTerminators=*/false);

    // Print the 'else' regions if it exists and has a block.
    auto &elseRegion = op.elseRegion();
    if (!elseRegion.empty()) {
      p << " else";
      p.printRegion(elseRegion,
                  /*printEntryBlockArgs=*/false,
                  /*printBlockTerminators=*/false);
    }
  }];
}

#endif // Quantum_OPS
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
@@ -97,3 +97,9 @@ add_test(NAME qcor_qasm3_test_compute_action COMMAND qasm3CompilerTester_Compute
target_include_directories(qasm3CompilerTester_ComputeAction PRIVATE . ../../ ${CMAKE_SOURCE_DIR}/tools/clang-wrapper/ ${CMAKE_SOURCE_DIR}/handlers ${XACC_ROOT}/include/gtest)
target_link_libraries(qasm3CompilerTester_ComputeAction qcor-mlir-api gtest gtest_main qcor-clang-wrapper qcor-syntax-handler ${CLANG_LIBS})


add_executable(qasm3CompilerTester_nisq_ifstmt test_nisq_if_statements.cpp)
add_test(NAME qcor_qasm3_test_nisq_ifstmt COMMAND qasm3CompilerTester_nisq_ifstmt)
target_include_directories(qasm3CompilerTester_nisq_ifstmt PRIVATE . ../../ ${CMAKE_SOURCE_DIR}/tools/clang-wrapper/ ${CMAKE_SOURCE_DIR}/handlers ${XACC_ROOT}/include/gtest)
target_link_libraries(qasm3CompilerTester_nisq_ifstmt qcor-mlir-api gtest gtest_main qcor-clang-wrapper qcor-syntax-handler ${CLANG_LIBS})
+40 −0
Original line number Diff line number Diff line
#include "qcor_mlir_api.hpp"
#include "gtest/gtest.h"

namespace {
// returns count of non-overlapping occurrences of 'sub' in 'str'
int countSubstring(const std::string &str, const std::string &sub) {
  if (sub.length() == 0)
    return 0;
  int count = 0;
  for (size_t offset = str.find(sub); offset != std::string::npos;
       offset = str.find(sub, offset + sub.length())) {
    ++count;
  }
  return count;
}
} // namespace

TEST(qasm3NisqIfStmtTester, checkMlirGen) {
  const std::string src = R"#(OPENQASM 3;
include "qelib1.inc";

qubit q;
bit c;
h q;
c = measure q;
if (c) {
  x q;
}
h q;
)#";
  auto mlir =
      qcor::mlir_compile(src, "test", qcor::OutputType::MLIR, true);
  std::cout << "MLIR:\n" << mlir << "\n";
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  return ret;
}
 No newline at end of file