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

Added more test cases for modified regions



also, add a handling case for qubits that are not associated with any qreg.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent a5782e05
Loading
Loading
Loading
Loading
+52 −0
Original line number Diff line number Diff line
@@ -598,6 +598,58 @@ z target;
    // __quantum__rt__start/__quantum__rt__end)
    EXPECT_EQ(countSubstring(llvm, "@__quantum__qis__z"), 3);
  }

  {
    // Check control bit isolation
    // (all qubits involved in modified regions are isolated before and after)
    const std::string src = R"#(OPENQASM 3;
include "qelib1.inc";

qubit control;
qubit target;

x control;
ctrl @ h control, target;
x control;
)#";
    auto llvm =
        qcor::mlir_compile(src, "test_kernel", qcor::OutputType::LLVMIR, false);
    std::cout << "LLVM:\n" << llvm << "\n";

    // Get the main kernel section only (there is the oracle LLVM section as
    // well)
    llvm = llvm.substr(llvm.find("@__internal_mlir_test_kernel"));
    const auto last = llvm.find_first_of("}");
    llvm = llvm.substr(0, last + 1);
    std::cout << "LLVM:\n" << llvm << "\n";
    EXPECT_EQ(countSubstring(llvm, "@__quantum__qis__x"), 2);
    EXPECT_EQ(countSubstring(llvm, "@__quantum__qis__h"), 1);
  }

  {
    // Check qubits in arrays
    const std::string src = R"#(OPENQASM 3;
include "qelib1.inc";
qubit q[2];
x q[0];
x q[1];
ctrl @ h q[0], q[1];
x q[0];
x q[1];
)#";
    auto llvm =
        qcor::mlir_compile(src, "test_kernel", qcor::OutputType::LLVMIR, false);
    std::cout << "LLVM:\n" << llvm << "\n";

    // Get the main kernel section only (there is the oracle LLVM section as
    // well)
    llvm = llvm.substr(llvm.find("@__internal_mlir_test_kernel"));
    const auto last = llvm.find_first_of("}");
    llvm = llvm.substr(0, last + 1);
    std::cout << "LLVM:\n" << llvm << "\n";
    EXPECT_EQ(countSubstring(llvm, "@__quantum__qis__x"), 4);
    EXPECT_EQ(countSubstring(llvm, "@__quantum__qis__h"), 1);
  }
}

int main(int argc, char **argv) {
+17 −0
Original line number Diff line number Diff line
@@ -227,7 +227,24 @@ void ScopedSymbolTable::invalidate_qubit_extracts(
    }
  }
}

void ScopedSymbolTable::erase_symbol(const std::string &var_name) {
  if (var_name.find("%") == std::string::npos && has_symbol(var_name)) {
    mlir::Value var = get_symbol(var_name);
    if (var.getType().isa<mlir::OpaqueType>() &&
        var.getType().cast<mlir::OpaqueType>().getTypeData() == "Qubit") {
      const std::string qreg_name =
          "__qcor__mlir__single_qubit_register_" + var_name;
      if (!has_symbol(qreg_name)) {
        // Bail out if we cannot track the (internal) originating qreg.
        // e.g., function/subroutine arguments.
        // TODO: we need a mechanism to restart SSA use-def chain in this case
        // whereby re-extract is not possible.
        return;
      }
    }
  }

  for (auto &table : scoped_symbol_tables) {
    table.erase_symbol(var_name);
  }