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

Added create callable Op to export QASM3 as QIR callable



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent aeea3f1d
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ def ArgvType : OpaqueType<"quantum", "ArgvType", "opaque argv type">;
def QregType : OpaqueType<"quantum", "QregType", "opaque qreg type">;
def StringType : OpaqueType<"quantum", "StringType", "opaque string type">;
def TupleType : OpaqueType<"quantum", "Tuple", "opaque tuple type">;
def CallableType : OpaqueType<"quantum", "Callable", "opaque callable type">;

def QallocOp : QuantumOp<"qalloc", []> {
    let arguments = (ins AnyI64Attr:$size, StrAttr:$name);
@@ -181,4 +182,11 @@ def TupleUnpackOp : QuantumOp<"tupleUnpack", []> {
  p << "q.tupleUnpack" << "(" << op.tuple() << ") : " << op.result().getType(); }];
}

def CreateCallableOp : QuantumOp<"createCallable", []> {
    let arguments = (ins FlatSymbolRefAttr:$functors);
    let results = (outs CallableType:$callable);
    let printer = [{  auto op = *this;
  p << "q.createCallable" << "(" << op.functors() << ") : " << op.callable().getType(); }];
}

#endif // Quantum_OPS
 No newline at end of file
+15 −5
Original line number Diff line number Diff line
#include "qasm3_visitor.hpp"

namespace {
void add_body_wrapper(mlir::OpBuilder &builder, const std::string &func_name, mlir::ModuleOp& moduleOp, mlir::FuncOp& wrapped_func) {
void add_body_wrapper(mlir::OpBuilder &builder, const std::string &func_name,
                      mlir::ModuleOp &moduleOp, mlir::FuncOp &wrapped_func) {
  // define internal void @body__wrapper(%Tuple* %capture-tuple, %Tuple*
  // %arg-tuple, %Tuple* %result-tuple)
  const std::string wrapper_fn_name = func_name + "__body__wrapper";
@@ -10,6 +11,10 @@ void add_body_wrapper(mlir::OpBuilder &builder, const std::string &func_name, ml
  llvm::StringRef tuple_type_name("Tuple");
  mlir::Identifier dialect = mlir::Identifier::get("quantum", context);
  auto tuple_type = mlir::OpaqueType::get(context, dialect, tuple_type_name);
  llvm::StringRef callable_type_name("Callable");
  auto callable_type =
      mlir::OpaqueType::get(context, dialect, callable_type_name);

  const std::vector<mlir::Type> argument_types{tuple_type, tuple_type,
                                               tuple_type};
  auto func_type = builder.getFunctionType(argument_types, llvm::None);
@@ -25,12 +30,17 @@ void add_body_wrapper(mlir::OpBuilder &builder, const std::string &func_name, ml
  mlir::Value arg_tuple = arguments[1];
  auto fn_type = wrapped_func.getType().cast<mlir::FunctionType>();
  mlir::TypeRange arg_types(fn_type.getInputs());
  auto unpackOp =
      builder.create<mlir::quantum::TupleUnpackOp>(builder.getUnknownLoc(), arg_types, arg_tuple);
  auto unpackOp = builder.create<mlir::quantum::TupleUnpackOp>(
      builder.getUnknownLoc(), arg_types, arg_tuple);
  auto call_op = builder.create<mlir::CallOp>(builder.getUnknownLoc(),
                                              wrapped_func, unpackOp.result());
  builder.restoreInsertionPoint(main_block);
  moduleOp.push_back(function_op);
  builder.setInsertionPointToStart(&moduleOp.getRegion().getBlocks().front());

  auto callable_create_op = builder.create<mlir::quantum::CreateCallableOp>(
      builder.getUnknownLoc(), callable_type,
      builder.getSymbolRefAttr(function_op));
  builder.restoreInsertionPoint(main_block);
}
}; // namespace
namespace qcor {