From e475175093ff9c6ccf780645344e43e04aef19d8 Mon Sep 17 00:00:00 2001 From: paul0403 Date: Wed, 4 Jun 2025 07:46:02 -0400 Subject: [PATCH 1/2] Update pattern rewrite tutorial's code listing to use matchAndRewrite --- doc/dev/transforms.rst | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/doc/dev/transforms.rst b/doc/dev/transforms.rst index bf6191241..b1644cd43 100644 --- a/doc/dev/transforms.rst +++ b/doc/dev/transforms.rst @@ -299,6 +299,14 @@ Let's add the pattern-matching logic to the ``matchAndRewrite`` method: return failure(); } + // In the line `Operation *parent = qbs[0].getDefiningOp();`, + // we retrived the parent `Operation`, which only has methods on the + // base `Operation` class + // https://mlir.llvm.org/doxygen/classmlir_1_1Operation.html + // To use the specific methods and the auto-generated getters for + // the specific `QubitUnitaryOp`, we need to cast it + // This is just how c++ behaves: base class objects are unaware of methods + // in child classes. QubitUnitaryOp parentOp = cast(parent); ValueRange parentQbs = parentOp.getOutQubits(); @@ -308,10 +316,11 @@ Let's add the pattern-matching logic to the ``matchAndRewrite`` method: return failure(); } - for (auto [qb1, qb2] : llvm::zip(qbs, parentQbs)) + for (auto [qb1, qb2] : llvm::zip(qbs, parentQbs)) { if (qb1 != qb2) { return failure(); } + } // Rewrite logic // ... We have matched the pattern, now rewrite the IR here @@ -370,9 +379,9 @@ In C++ it will look as follows: // Pattern matching logic // ... match the pattern + ////////////////////////////////////////////////// + // Rewrite logic - ValueRange qbs = op.getInQubits(); - QubitUnitaryOp parentOp = cast(qbs[0].getDefiningOp()); // In the tablegen definition of `QubitUnitaryOp`, there is a // field called `$matrix`, storing the matrix for the unitary gate. @@ -536,7 +545,7 @@ fixed point is reached. If you are encoutering issues, or would like to quickly try out the merge unitary pass described in this section, you can have a look at or cherry-pick this commit which includes all changes described - in this section: https://github.com/PennyLaneAI/catalyst/commit/9afcc3500e12e5a51b78dda76cd4d27bdf4c8905 + in this section: https://github.com/PennyLaneAI/catalyst/commit/2c84b2402cb67c62a6de5137bbf5b41afaa5a328 Writing more general transformations From 5b5b6bdd4d9bd6838acde9ed89aac24bea0ddff3 Mon Sep 17 00:00:00 2001 From: Paul <79805239+paul0403@users.noreply.github.com> Date: Wed, 11 Jun 2025 09:37:51 -0400 Subject: [PATCH 2/2] Update doc/dev/transforms.rst Co-authored-by: David Ittah --- doc/dev/transforms.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/doc/dev/transforms.rst b/doc/dev/transforms.rst index b1644cd43..e4dc7fac1 100644 --- a/doc/dev/transforms.rst +++ b/doc/dev/transforms.rst @@ -304,9 +304,7 @@ Let's add the pattern-matching logic to the ``matchAndRewrite`` method: // base `Operation` class // https://mlir.llvm.org/doxygen/classmlir_1_1Operation.html // To use the specific methods and the auto-generated getters for - // the specific `QubitUnitaryOp`, we need to cast it - // This is just how c++ behaves: base class objects are unaware of methods - // in child classes. + // the specific `QubitUnitaryOp`, we need to cast it first. QubitUnitaryOp parentOp = cast(parent); ValueRange parentQbs = parentOp.getOutQubits();