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

Lower slice op to qir call



Construct the Range object using Undef and Insert operations.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent aaee38ba
Loading
Loading
Loading
Loading
+36 −7
Original line number Diff line number Diff line
#include "gtest/gtest.h"
#include "qcor_mlir_api.hpp"
#include "gtest/gtest.h"

TEST(qasm3VisitorTester, checkAlias) {
  std::cout << "HOWDY\n";
  const std::string src = R"#(OPENQASM 3;
  {
    // Test 1: Alias by indices
    const std::string alias_by_indicies = R"#(OPENQASM 3;
include "qelib1.inc";
qubit q[6];
// myreg[0,1,2] refers to the qubit q[1,3,5]
@@ -26,10 +27,38 @@ QCOR_EXPECT_TRUE(m[3] == 1);
QCOR_EXPECT_TRUE(m[4] == 0);
QCOR_EXPECT_TRUE(m[5] == 1);
)#";
  auto mlir =
      qcor::mlir_compile("qasm3", src, "test", qcor::OutputType::MLIR, true);
    auto mlir = qcor::mlir_compile("qasm3", alias_by_indicies, "test",
                                   qcor::OutputType::MLIR, true);
    std::cout << "MLIR:\n" << mlir << "\n";
    EXPECT_FALSE(qcor::execute("qasm3", alias_by_indicies, "test"));
  }
  // Test 2: Alias by slice:
  {
    const std::string alias_by_slice = R"#(OPENQASM 3;
include "qelib1.inc";
qubit q[6];
// Range without step size
let myreg1 = q[0:3];

// Range with step size (0, 2, 4)
let myreg2 = q[0:2:5];

// Range with negative step:
let myreg3 = q[4:-1:2];

// Range with start = stop
// This is q[5]
let myreg4 = q[5:5];

// Range using negative indexing:
// Last 3 qubits
let myreg5 = q[-4:-1];
)#";
    auto mlir = qcor::mlir_compile("qasm3", alias_by_slice, "test",
                                   qcor::OutputType::MLIR, true);
    std::cout << "MLIR:\n" << mlir << "\n";
  EXPECT_FALSE(qcor::execute("qasm3", src, "test"));
    // EXPECT_FALSE(qcor::execute("qasm3", alias_by_slice, "test"));
  }
}

int main(int argc, char **argv) {
+15 −20
Original line number Diff line number Diff line
@@ -832,17 +832,18 @@ class QarraySliceOpLowering : public ConversionPattern {
    auto input_array = qArraySliceOp.qreg();
    auto slice_range = qArraySliceOp.slice_range();
    assert(slice_range.size() == 3);
    llvm::ArrayRef<int64_t> shaperef{1};
    auto mem_type = mlir::MemRefType::get(shaperef, rewriter.getI64Type());
    auto ptrTy = LLVM::LLVMPointerType::get(range_type);
    Value one = rewriter.create<LLVM::ConstantOp>(
        loc, IntegerType::get(rewriter.getContext(), 32),
        rewriter.getIntegerAttr(rewriter.getI64Type(), 1));
    Value slice_range_alloca =
        rewriter.create<LLVM::AllocaOp>(loc, ptrTy, one, /*alignment=*/0);

    // TODO: Set the start, step, end values in the Range struct
    
    // Create a Range object:
    auto rangeObj = [&]() {
      auto desc = rewriter.create<LLVM::UndefOp>(loc, range_type);
      auto insertStart = rewriter.create<LLVM::InsertValueOp>(
          loc, desc, slice_range[0], rewriter.getI64ArrayAttr(0));
      auto insertStep = rewriter.create<LLVM::InsertValueOp>(
          loc, insertStart, slice_range[1], rewriter.getI64ArrayAttr(1));
      auto insertEnd = rewriter.create<LLVM::InsertValueOp>(
          loc, insertStep, slice_range[2], rewriter.getI64ArrayAttr(2));

      return insertEnd.res();
    }();

    // Retrieve the input array
    auto qreg_name_attr = input_array.getDefiningOp()->getAttr("name");
@@ -855,17 +856,11 @@ class QarraySliceOpLowering : public ConversionPattern {
        rewriter.getIntegerAttr(rewriter.getI64Type(),
                                /* dim = 0, 1d */ 0));

    auto range_deref =
        rewriter.create<LLVM::LoadOp>(loc, range_type, slice_range_alloca);
    auto array_slice_qir_call = rewriter.create<mlir::CallOp>(
    // Make the call
    rewriter.create<mlir::CallOp>(
        loc, symbol_ref, array_qbit_type,
        ArrayRef<Value>({array_var, dim_id_int, range_deref}));

    // Get the returned qubit array pointer Value
    auto qbit_array = array_slice_qir_call.getResult(0);

        ArrayRef<Value>({array_var, dim_id_int, rangeObj}));
    // Remove the old QuantumDialect QarraySliceOp
    rewriter.replaceOp(op, qbit_array);
    rewriter.eraseOp(op);

    return success();