Loading mlir/dialect/include/Quantum/QuantumOps.td +20 −0 Original line number Diff line number Diff line Loading @@ -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 mlir/dialect/lib/Quantum/QuantumDialect.cpp +35 −1 Original line number Diff line number Diff line Loading @@ -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. //===----------------------------------------------------------------------===// Loading @@ -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) { Loading mlir/dialect/lib/Quantum/QuantumOps.cpp +3 −5 Original line number Diff line number Diff line Loading @@ -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; } } Loading mlir/parsers/qasm3/examples/test_opt.qasm 0 → 100644 +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 mlir/parsers/qasm3/examples/test_opt_permute.qasm 0 → 100644 +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
mlir/dialect/include/Quantum/QuantumOps.td +20 −0 Original line number Diff line number Diff line Loading @@ -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
mlir/dialect/lib/Quantum/QuantumDialect.cpp +35 −1 Original line number Diff line number Diff line Loading @@ -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. //===----------------------------------------------------------------------===// Loading @@ -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) { Loading
mlir/dialect/lib/Quantum/QuantumOps.cpp +3 −5 Original line number Diff line number Diff line Loading @@ -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; } } Loading
mlir/parsers/qasm3/examples/test_opt.qasm 0 → 100644 +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
mlir/parsers/qasm3/examples/test_opt_permute.qasm 0 → 100644 +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];