Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjoint canonicalization #1205

Draft
wants to merge 62 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
0d85eaf
Add pattern
rmoyard Sep 30, 2024
248ab5e
Update structure
rmoyard Sep 30, 2024
9ccb01a
Update
rmoyard Oct 3, 2024
3e5d1d1
Working draft
rmoyard Oct 3, 2024
c4f46ba
Update
rmoyard Oct 3, 2024
c9d56b6
Merge branch 'main' into merge_rotations
rmoyard Oct 7, 2024
7286abc
renamed to `ChainedNamedHermitianOpRewritePattern`
paul0403 Oct 7, 2024
e1f54e5
Add test
rmoyard Oct 7, 2024
4f3f9e2
Merge branch 'merge_rotations' of https://github.com/PennyLaneAI/cata…
rmoyard Oct 7, 2024
043a5f0
MLIR test: CRY switch qubits
rmoyard Oct 7, 2024
1f87943
add pattern
paul0403 Oct 7, 2024
1458937
preprocess with cse pass so we can check param SSA values;
paul0403 Oct 7, 2024
b8b9913
tests
paul0403 Oct 7, 2024
43fcce7
format
paul0403 Oct 7, 2024
799d96e
test with explicit rotation angles
paul0403 Oct 7, 2024
b60519e
test with different explicit params
paul0403 Oct 7, 2024
84c758c
cano test
rmoyard Oct 7, 2024
e3447ef
Update
rmoyard Oct 7, 2024
a43d9ac
changelog
paul0403 Oct 7, 2024
05f5a5b
Initial draft multiRZ
rmoyard Oct 7, 2024
bc5faaf
Typo
rmoyard Oct 7, 2024
485c6d6
ctrl gates
paul0403 Oct 8, 2024
9dcfdc6
Merge remote-tracking branch 'origin/main' into cancel_inverse_adjoint
paul0403 Oct 8, 2024
90ddcf1
remove template type in parent getter (a value will have just one def…
paul0403 Oct 8, 2024
b9acfe6
factor out a parent gate verifier analysis, so it can be reused with …
paul0403 Oct 8, 2024
13eb094
add all test cases for ctrl
paul0403 Oct 9, 2024
91beb68
Merge remote-tracking branch 'origin/main' into cancel_inverse_adjoint
paul0403 Oct 9, 2024
ebb5169
make the named hermitian pattern use the common analysis as well
paul0403 Oct 9, 2024
5c77fb3
one more test
paul0403 Oct 9, 2024
0823116
Merge remote-tracking branch 'origin/main' into cancel_inverse_adjoint
paul0403 Oct 9, 2024
9ae9ba7
follow include order guideline
paul0403 Oct 9, 2024
738c96a
`verified` --> `succeeded`
paul0403 Oct 9, 2024
e78cca1
move namecheck before wire verification
paul0403 Oct 10, 2024
93b1ed1
Merge remote-tracking branch 'origin/cancel_inverse_adjoint' into mer…
rmoyard Oct 10, 2024
6324fd0
Add analysis integration
rmoyard Oct 10, 2024
a6b8424
MultiRz case
rmoyard Oct 10, 2024
b24703a
Split verifier into a "normal" one and an aggressive one.
paul0403 Oct 10, 2024
2f95838
use aggressive for named gates
paul0403 Oct 10, 2024
d092540
Merge remote-tracking branch 'origin/main' into cancel_inverse_adjoint
paul0403 Oct 10, 2024
9bc658b
add multirz
paul0403 Oct 10, 2024
1fe2622
Merge remote-tracking branch 'origin/main' into cancel_inverse_adjoint
paul0403 Oct 10, 2024
552fae2
Merge remote-tracking branch 'origin/main' into cancel_inverse_adjoint
paul0403 Oct 11, 2024
9a7800a
Merge branch 'cancel_inverse_adjoint' into merge_rotations
rmoyard Oct 11, 2024
0660b68
changelog
paul0403 Oct 11, 2024
a7cb5af
change aggressive name to VerifyParentGateAndNameAnalysis
paul0403 Oct 11, 2024
b54fcb7
Merge branch 'cancel_inverse_adjoint' into merge_rotations
rmoyard Oct 11, 2024
02dc927
changelog grammar
paul0403 Oct 11, 2024
7ba892f
Add multirz test
rmoyard Oct 11, 2024
4fb926f
Merge branch 'cancel_inverse_adjoint' into merge_rotations
rmoyard Oct 11, 2024
4ae4281
Update doc
rmoyard Oct 11, 2024
9cee8c9
Merge branch 'merge_rotations' of https://github.com/PennyLaneAI/cata…
rmoyard Oct 11, 2024
1b970da
Update mlir/lib/Quantum/Transforms/MergeRotationsPatterns.cpp
rmoyard Oct 11, 2024
ca6df1b
Update
rmoyard Oct 11, 2024
f394838
Merge branch 'merge_rotations' of https://github.com/PennyLaneAI/cata…
rmoyard Oct 11, 2024
090d285
Merge branch 'main' into merge_rotations
rmoyard Oct 11, 2024
d0789f0
Merge branch 'merge_rotations' of https://github.com/PennyLaneAI/cata…
rmoyard Oct 11, 2024
bd0ddb6
Update
rmoyard Oct 11, 2024
01cd3f5
Remove erase
rmoyard Oct 11, 2024
def5b3a
Update
rmoyard Oct 11, 2024
0213ae7
Pylint
rmoyard Oct 11, 2024
39c8a7a
Draft
rmoyard Oct 11, 2024
edfcd8e
Merge branch 'main' into adjoint_canonicalization
rmoyard Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mlir/include/Quantum/IR/QuantumOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ def CustomOp : UnitaryGate_Op<"custom", [DifferentiableGate, NoMemoryEffect,
return getParams();
}
}];
let hasCanonicalizeMethod = 1;
}

def GlobalPhaseOp : UnitaryGate_Op<"gphase", [DifferentiableGate, AttrSizedOperandSegments]> {
Expand Down
30 changes: 30 additions & 0 deletions mlir/lib/Quantum/IR/QuantumOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/OpImplementation.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include <optional>

Expand All @@ -34,6 +36,34 @@ using namespace catalyst::quantum;
//===----------------------------------------------------------------------===//
// Quantum op canonicalizers.
//===----------------------------------------------------------------------===//
static const mlir::StringSet<> hermitianOps = {"Hadamard", "PauliX", "PauliY", "PauliZ", "CNOT",
"CY", "CZ", "SWAP", "Toffoli"};
static const mlir::StringSet<> rotationsOps = {"RX", "RY", "RZ", "PhaseShift", "Rot",
"CRX", "CRY", "CRZ", "ControlledPhaseShift", "CRot"};
LogicalResult CustomOp::canonicalize(CustomOp op, mlir::PatternRewriter &rewriter)
{
if (op.getAdjoint()) {
auto name = op.getGateName();
if (hermitianOps.contains(name)) {
op.setAdjoint(false);
return success();
}
else if (rotationsOps.contains(name)) {
auto params = op.getParams();
SmallVector<Value> paramsMinus;
for (auto param : params) {
rewriter.create<mlir::arith::NegFOp>(op.getLoc(), param);
}
auto adjointOp = rewriter.create<CustomOp>(op.getLoc(), op.getOutQubits().getTypes(), op.getOutCtrlQubits().getTypes(),
paramsMinus, op.getInQubits(), name, nullptr,
op.getInCtrlQubits(), op.getInCtrlValues());
rewriter.replaceOp(op, adjointOp.getResults());
return success();
}
return failure();
};
return failure();
}

LogicalResult DeallocOp::canonicalize(DeallocOp dealloc, mlir::PatternRewriter &rewriter)
{
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Quantum/Transforms/merge_rotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct MergeRotationsPass : impl::MergeRotationsPassBase<MergeRotationsPass> {
}

RewritePatternSet patterns(&getContext());
catalyst::quantum::CustomOp::getCanonicalizationPatterns(patterns, &getContext());
populateMergeRotationsPatterns(patterns);

if (failed(applyPatternsAndFoldGreedily(targetfunc, std::move(patterns)))) {
Expand Down
Loading