Commit f73023e3 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding subroutine test

parent 7940bf07
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -36,3 +36,8 @@ add_executable(qasm3CompilerTester_QuantumInstructions test_quantum_instructions
add_test(NAME qcor_qasm3_test_qinsts COMMAND qasm3CompilerTester_QuantumInstructions)
target_include_directories(qasm3CompilerTester_QuantumInstructions PRIVATE . ../../ ${XACC_ROOT}/include/gtest)
target_link_libraries(qasm3CompilerTester_QuantumInstructions qcor-mlir-api gtest gtest_main)

add_executable(qasm3CompilerTester_Subroutine test_subroutine.cpp)
add_test(NAME qcor_qasm3_test_subroutine COMMAND qasm3CompilerTester_Subroutine)
target_include_directories(qasm3CompilerTester_Subroutine PRIVATE . ../../ ${XACC_ROOT}/include/gtest)
target_link_libraries(qasm3CompilerTester_Subroutine qcor-mlir-api gtest gtest_main)
+40 −0
Original line number Diff line number Diff line
#include "gtest/gtest.h"
#include "qcor_mlir_api.hpp"

TEST(qasm3VisitorTester, checkSubroutine1) {
  const std::string sub1 = R"#(OPENQASM 3;
include "qelib1.inc";
const n = 10;
def parity(bit[n]:cin) -> bit {
  bit c;
  for i in [0: n-1] {
    c ^= cin[i];
  }
  return c;
}

bit b[n] = "1110001010";
bit p;
p = parity(b);
QCOR_EXPECT_TRUE(p == 1);
print(p);


bit b1[n] = "1111001010";
bit p1;
p1 = parity(b1);
QCOR_EXPECT_TRUE(p1 == 0);
print(p1);
)#";
  auto mlir = qcor::mlir_compile("qasm3", sub1, "check_parity_sub",
                                 qcor::OutputType::MLIR, false);

  std::cout << mlir << "\n";
  EXPECT_FALSE(qcor::execute("qasm3", sub1, "check_parity_sub"));
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  return ret;
}
 No newline at end of file
+22 −1
Original line number Diff line number Diff line
@@ -194,7 +194,6 @@ antlrcpp::Any qasm3_expression_generator::visitComparsionExpression(
        }
        update_current_value(builder.create<mlir::CmpFOp>(
            location, antlr_to_mlir_fpredicate[op], lhs, rhs));
        
      }
      return 0;
    } else {
@@ -1170,6 +1169,28 @@ antlrcpp::Any qasm3_expression_generator::visitExpressionTerminator(
    update_current_value(call_op.getResult(0));

    return 0;
  } else if (auto kernel_call = ctx->kernelCall()) {
    // kernelCall
    // : Identifier LPAREN expressionList? RPAREN
    // ;
    auto func =
        symbol_table.get_seen_function(kernel_call->Identifier()->getText());

    std::vector<mlir::Value> operands;
    auto expression_list = kernel_call->expressionList()->expression();
    for (auto expression : expression_list) {
      qasm3_expression_generator param_exp_generator(
          builder, symbol_table, file_name);
      param_exp_generator.visit(expression);
      operands.push_back(param_exp_generator.current_value);
    }

    auto call_op = builder.create<mlir::CallOp>(location, func,
                                                llvm::makeArrayRef(operands));
    update_current_value(call_op.getResult(0));

    return 0;

  }

  else {