-
-
Notifications
You must be signed in to change notification settings - Fork 69
✨ Add decompose-multi-controlled pass for multi-controlled X/Z decomposition
#1810
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
Open
simon1hofmann
wants to merge
14
commits into
main
Choose a base branch
from
decompose-multi-controlled-gates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bec1fdf
✨ Implement `decompose-multi-controlled` pass for decomposing multi-c…
simon1hofmann d48be9c
✨ Extend `decompose-multi-controlled` pass to support decomposition o…
simon1hofmann f0de507
📝 Update copyright year in QCO dialect files to 2025 and remove outda…
simon1hofmann 08a9dfa
📝 Refine comments in McxSynthesis.cpp for clarity and update test des…
simon1hofmann d47641d
🚨 Fix linter warnings
simon1hofmann e0b53a4
📝 Update CHANGELOG.md to include pull request link for `decompose-mul…
simon1hofmann 0ef3f71
☂️ Increase coverage
simon1hofmann dff6eeb
🚨 Fix linter warnings
simon1hofmann 9ec9e6f
🐇 Address Rabbit's comments
cf57d47
✨ Add project options for MLIRQCOTransforms in CMakeLists.txt
0292b3e
🎨 pre-commit fixes
pre-commit-ci[bot] b2f30eb
☂️ Increase coverage
55176d1
🐇 Address Rabbit's comments
968d06c
Merge branch 'main' into decompose-multi-controlled-gates
simon1hofmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
mlir/include/mlir/Dialect/QCO/Transforms/Decomposition/MultiControlled.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright (c) 2023 - 2026 Chair for Design Automation, TUM | ||
| * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH | ||
| * All rights reserved. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Licensed under the MIT License | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <mlir/IR/Builders.h> | ||
| #include <mlir/IR/Location.h> | ||
| #include <mlir/IR/Value.h> | ||
| #include <mlir/Support/LLVM.h> | ||
|
|
||
| namespace mlir::qco::decomposition { | ||
|
|
||
| /** | ||
| * @brief Emits a decomposition of a multi-controlled X gate. | ||
| * | ||
| * @details Emits a sequence of one- and two-qubit gates that, taken together, | ||
| * implement the multi-controlled X gate that flips @p target whenever all @p | ||
| * controls are in the @f$|1\rangle@f$ state. The decomposition follows Huang | ||
| * and Palsberg (PLDI 2024), composing Iten et al. (Phys. Rev. A 93, 032318, | ||
| * 2016) dirty-ancilla subcircuits for large control counts. The emitted gates | ||
| * are `h`, `t`, `tdg`, `p`, and `cx`. | ||
| * | ||
| * @note Adapted from ``synth_mcx_noaux_hp24`` and ``synth_mcx_n_dirty_i15`` in | ||
| * the IBM Qiskit framework. | ||
| * (C) Copyright IBM 2025 | ||
| * | ||
| * This code is licensed under the Apache License, Version 2.0. You may | ||
| * obtain a copy of this license in the LICENSE.txt file in the root | ||
| * directory of this source tree or at | ||
| * https://www.apache.org/licenses/LICENSE-2.0. | ||
| * | ||
| * Any modifications or derivative works of this code must retain this | ||
| * copyright notice, and modified files need to carry a notice | ||
| * indicating that they have been altered from the originals. | ||
| * | ||
| * @param builder Builder positioned at the desired insertion point. | ||
| * @param loc Location attached to the emitted operations. | ||
| * @param controls Current SSA values of the control qubits. | ||
| * @param target Current SSA value of the target qubit. | ||
| * @return The updated SSA values, ordered as `[controls..., target]`. | ||
| * | ||
| * @pre @p controls must contain at least two qubits. | ||
| */ | ||
| [[nodiscard]] SmallVector<Value> synthesizeMcx(OpBuilder& builder, Location loc, | ||
| ValueRange controls, | ||
| Value target); | ||
|
|
||
| /** | ||
| * @brief Emits a decomposition of a multi-controlled Z gate. | ||
| * | ||
| * @details Emits the same gate set as @ref synthesizeMcx (`h`, `t`, `tdg`, `p`, | ||
| * and `cx`). For @f$k \ge 2@f$ controls, emits the HP24 MCX core directly, | ||
| * since @f$\mathrm{MCZ} = | ||
| * H_{\text{target}}\,(H_{\text{target}}\,\mathrm{CORE}\, | ||
| * H_{\text{target}})\,H_{\text{target}} = \mathrm{CORE}@f$ and the MCX | ||
| * Hadamard bookends cancel under this conjugation. | ||
| * | ||
| * @param builder Builder positioned at the desired insertion point. | ||
| * @param loc Location attached to the emitted operations. | ||
| * @param controls Current SSA values of the control qubits. | ||
| * @param target Current SSA value of the target qubit. | ||
| * @return The updated SSA values, ordered as `[controls..., target]`. | ||
| * | ||
| * @pre @p controls must contain at least two qubits. | ||
| */ | ||
| [[nodiscard]] SmallVector<Value> synthesizeMcz(OpBuilder& builder, Location loc, | ||
| ValueRange controls, | ||
| Value target); | ||
|
|
||
| } // namespace mlir::qco::decomposition |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
mlir/lib/Dialect/QCO/Transforms/Decomposition/DecomposeMultiControlled.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Copyright (c) 2023 - 2026 Chair for Design Automation, TUM | ||
| * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH | ||
| * All rights reserved. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Licensed under the MIT License | ||
| */ | ||
|
|
||
| #include "mlir/Dialect/QCO/IR/QCOInterfaces.h" | ||
| #include "mlir/Dialect/QCO/IR/QCOOps.h" | ||
| #include "mlir/Dialect/QCO/Transforms/Decomposition/MultiControlled.h" | ||
| #include "mlir/Dialect/QCO/Transforms/Passes.h" | ||
| #include "mlir/Dialect/Utils/Utils.h" | ||
|
|
||
| #include <mlir/Dialect/Arith/IR/Arith.h> // IWYU pragma: keep (Passes.h.inc) | ||
| #include <mlir/IR/MLIRContext.h> | ||
| #include <mlir/IR/PatternMatch.h> | ||
| #include <mlir/Support/LLVM.h> | ||
| #include <mlir/Support/LogicalResult.h> | ||
| #include <mlir/Transforms/GreedyPatternRewriteDriver.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <utility> | ||
|
|
||
| namespace mlir::qco { | ||
|
|
||
| #define GEN_PASS_DEF_DECOMPOSEMULTICONTROLLED | ||
| #include "mlir/Dialect/QCO/Transforms/Passes.h.inc" | ||
|
|
||
| namespace { | ||
|
|
||
| /** | ||
| * @brief Decomposes a multi-controlled Pauli-X or Pauli-Z gate into elementary | ||
| * one- and two-qubit gates. | ||
| * | ||
| * @details Matches `qco.ctrl` with a single `qco.x` or `qco.z` body when the | ||
| * control count is at least `minControls_` (and at least two, as enforced by | ||
| * the pass). Single-control `CX`/`CZ` and other gates are left unchanged. | ||
| */ | ||
| struct DecomposeMultiControlledPauliPattern final : OpRewritePattern<CtrlOp> { | ||
| explicit DecomposeMultiControlledPauliPattern(MLIRContext* context, | ||
| uint64_t minControls) | ||
| : OpRewritePattern<CtrlOp>(context), minControls_(minControls) {} | ||
|
|
||
| LogicalResult matchAndRewrite(CtrlOp op, | ||
| PatternRewriter& rewriter) const override { | ||
| if (op.getNumControls() < minControls_ || op.getNumTargets() != 1) { | ||
| return failure(); | ||
| } | ||
| auto inner = utils::getSoleBodyUnitary<UnitaryOpInterface>(*op.getBody()); | ||
| if (!inner) { | ||
| return failure(); | ||
| } | ||
|
|
||
| rewriter.setInsertionPoint(op); | ||
| const auto controls = op.getControlsIn(); | ||
| const auto target = op.getInputTarget(0); | ||
| const auto loc = op.getLoc(); | ||
|
|
||
| if (isa<XOp>(inner.getOperation())) { | ||
| rewriter.replaceOp( | ||
| op, decomposition::synthesizeMcx(rewriter, loc, controls, target)); | ||
| return success(); | ||
| } | ||
| if (isa<ZOp>(inner.getOperation())) { | ||
| rewriter.replaceOp( | ||
| op, decomposition::synthesizeMcz(rewriter, loc, controls, target)); | ||
| return success(); | ||
| } | ||
| return failure(); | ||
| } | ||
|
|
||
| private: | ||
| uint64_t minControls_; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Pass that decomposes multi-controlled X and Z gates into elementary | ||
| * gates. | ||
| */ | ||
| struct DecomposeMultiControlled final | ||
| : impl::DecomposeMultiControlledBase<DecomposeMultiControlled> { | ||
| using DecomposeMultiControlledBase::DecomposeMultiControlledBase; | ||
|
|
||
| protected: | ||
| void runOnOperation() override { | ||
| if (minControls < 2) { | ||
| getOperation().emitError() | ||
| << "decompose-multi-controlled requires min-controls >= 2"; | ||
| signalPassFailure(); | ||
| return; | ||
| } | ||
|
|
||
| RewritePatternSet patterns(&getContext()); | ||
| patterns.add<DecomposeMultiControlledPauliPattern>(&getContext(), | ||
| minControls); | ||
|
|
||
| if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) { | ||
| signalPassFailure(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| } // namespace mlir::qco |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.