Unverified Commit 85894c95 authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #171 from tnguyen-ornl/tnguyen/mlir-value-semantics

Added MLIR optimization passes and code refactor
parents 9a14001d 09c51bf3
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -152,4 +152,24 @@ def CreateStringLiteralOp : QuantumOp<"createString", []> {
  p << "q.create_string(\"" << op.text() << "\")"; }];
}

// Cast QIR Result to bool (i1 type)
def ResultCastOp : QuantumOp<"resultCast", []> {
    let arguments = (ins ResultType:$measure_result);
    let results = (outs I1:$bit_result);
    let printer = [{  auto op = *this;
  p << "q.resultCast" << "(" << op.measure_result() << ") : " << op.bit_result().getType(); }];
}

// Sign-Unsign cast:
// Rationale: std dialect only accepts signless type (i.e. int but not uint)
// we need to have this cast op in the dialect to finally lower to LLVM cast 
// which can handle int -> uint casting at the final lowering phase.
// Note: std.index_cast cannot handle int -> unit casting (one of the type must be an index type).
def IntegerCastOp : QuantumOp<"integerCast", []> {
    let arguments = (ins AnyInteger:$input);
    let results = (outs AnyInteger:$output);
    let printer = [{  auto op = *this;
  p << "q.integerCast" << "(" << op.input() << ") : " << op.output().getType(); }];
}

#endif // Quantum_OPS
 No newline at end of file
+35 −1
Original line number Diff line number Diff line
@@ -2,10 +2,43 @@
#include "Quantum/QuantumDialect.h"
#include "Quantum/QuantumOps.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/Transforms/InliningUtils.h"

using namespace mlir;
using namespace mlir::quantum;
namespace {
/// Inliner interface
/// This class defines the interface for handling inlining with Quantum
/// operations.
// We simplify inherit from the base interface class and override
/// the necessary methods.
struct QuantumInlinerInterface : public DialectInlinerInterface {
  using DialectInlinerInterface::DialectInlinerInterface;

  /// This hook checks to see if the given callable operation is legal to inline
  /// into the given call.
  /// Operations in Quantum dialect are always legal to inline.
  bool isLegalToInline(Operation *call, Operation *callable,
                       bool wouldBeCloned) const final {
    return true;
  }

  /// This hook checks to see if the given operation is legal to inline into the
  /// given region.
  /// Only inline VSOp for now:
  // FIXME: there is a weird error when qalloc is inlined at MLIR level
  // hence, just allow VSOp to be inlined for the timebeing.
  // i.e. all quantum subroutines that only contain VSOp's can be inlined.
  bool isLegalToInline(Operation *op, Region *regione, bool,
                       BlockAndValueMapping &) const final {
    if (dyn_cast_or_null<mlir::quantum::ValueSemanticsInstOp>(op)) {
      return true;
    }

    return false;
  }
};
} // namespace
//===----------------------------------------------------------------------===//
// Quantum dialect.
//===----------------------------------------------------------------------===//
@@ -15,6 +48,7 @@ void QuantumDialect::initialize() {
#define GET_OP_LIST
#include "Quantum/QuantumOps.cpp.inc"
      >();
  addInterfaces<QuantumInlinerInterface>();
}

// static void print(mlir::OpAsmPrinter &printer, mlir::quantum::InstOp op) {
+3 −5
Original line number Diff line number Diff line
@@ -3,14 +3,12 @@
#include "Quantum/QuantumDialect.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/Builders.h"

bool isOpaqueTypeWithName(mlir::Type type, std::string dialect,
                          std::string type_name) {
  if (type.isa<mlir::OpaqueType>() && dialect == "quantum") {
    if (type_name == "Qubit") {
      return true;
    }
    if (type_name == "Result") {
    if (type_name == "Qubit" || type_name == "Result" || type_name == "Array" ||
        type_name == "ArgvType" || type_name == "QregType" ||
        type_name == "StringType") {
      return true;
    }
  }
+11 −0
Original line number Diff line number Diff line
OPENQASM 3;
include "qelib1.inc";

const n = 2;

qubit q[n];

x q[0];
ry(1.2345) q[1];
y q[1];
x q[1];
 No newline at end of file
+11 −0
Original line number Diff line number Diff line
OPENQASM 3;
include "qelib1.inc";

const n = 2;

qubit q[n];

// Rz can be moved forward (pass CX) to combine with z
rz(1.2345) q[0];
cx q[0], q[1];
z q[0];
Loading