Unverified Commit 985b8cb7 authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #195 from tnguyen-ornl/tnguyen/fix-subroutine-invoke

OpenQASM3: Fix subroutine invoke
parents e9e0624a 7ed61f30
Loading
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
@@ -33,6 +33,57 @@ print(p1);
  EXPECT_FALSE(qcor::execute(sub1, "check_parity_sub"));
}

TEST(qasm3VisitorTester, checkSubroutineDeuteron) {
  const std::string sub1 = R"#(OPENQASM 3;
include "stdgates.inc";

const shots = 1024;
// State-preparation:
def ansatz(float[64]:theta) qubit[2]:q {
    x q[0];
    ry(theta) q[1];
    cx q[1], q[0];
}

def deuteron(float[64]:theta) qubit[2]:q -> float[64] {
    bit first, second;
    float[64] num_parity_ones = 0.0;
    float[64] result;
    for i in [0:shots] {
        ansatz(theta) q;
        // Change measurement basis
        h q;
        // Measure
        first = measure q[0];
        second = measure q[1];
        if (first != second) {
            num_parity_ones += 1.0;
        }
        // Reset
        reset q;
    }

    // Compute expectation value
    result = (shots - num_parity_ones) / shots - num_parity_ones / shots;
    return result;
}

float[64] theta, exp_val;
qubit qq[2];
// Try a theta value:
const step = 2 * pi / 19;
theta = -pi + step;
print("Theta = ", theta);
exp_val = deuteron(theta) qq;
print("Avg <X0X1> = ", exp_val);
)#";
  auto mlir = qcor::mlir_compile(sub1, "check_deuteron",
                                 qcor::OutputType::MLIR, false);

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

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
+10 −1
Original line number Diff line number Diff line
@@ -1206,7 +1206,16 @@ antlrcpp::Any qasm3_expression_generator::visitExpressionTerminator(
        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 arg = param_exp_generator.current_value;
        if (arg.getType().isa<mlir::MemRefType>()) {
          auto element_type =
              arg.getType().cast<mlir::MemRefType>().getElementType();
          if (!(element_type.isa<mlir::IntegerType>() &&
                element_type.getIntOrFloatBitWidth() == 1)) {
            arg = builder.create<mlir::LoadOp>(location, arg);
          }
        }
        operands.push_back(arg);
      }

      // Here we add all global variables