Skip to content

Commit e917017

Browse files
authored
Reorganize graph-decomposition passes into a folder and deduplicate shared helpers (#3010)
**Context:** Reorg the `graph-decomposition`, `decompose-lowering` and `graph-solver` src into a single `GraphDecomposition` folder, and extracts their shared logic into a `DecompUtils.hpp` to remove duplications. No functional change! this is a structural/cleanup PR. **Possible Drawbacks:** - `DecompUtils` is scoped to `GraphDecomposition`. `QuantumDialect.cpp` and `RegisterDecompRuleResourcePass.cpp` also reference `target_gate`, but they live in other passes/libs and I left them untouched to avoid cross-lib dependency. **Related GitHub Issues:** #2977 (comment) [sc-124463]
1 parent 19279f7 commit e917017

15 files changed

Lines changed: 134 additions & 70 deletions

File tree

mlir/lib/Quantum/Transforms/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
set(LIBRARY_NAME quantum-transforms)
22

3-
add_subdirectory(DecompGraphSolver)
3+
add_subdirectory(GraphDecomposition/DecompGraphSolver)
44

55
file(GLOB SRC
66
AdjointLowering/AdjointLowering.cpp
@@ -10,7 +10,10 @@ file(GLOB SRC
1010
BufferizableOpInterfaceImpl.cpp
1111
CancelInversesPatterns.cpp
1212
ConversionPatterns.cpp
13-
DecomposeLoweringPatterns.cpp
13+
GraphDecomposition/DecompUtils.cpp
14+
GraphDecomposition/DecomposeLoweringPatterns.cpp
15+
GraphDecomposition/decompose_lowering.cpp
16+
GraphDecomposition/graph_decomposition.cpp
1417
DisentangleCNOT.cpp
1518
DisentangleSWAP.cpp
1619
GridsynthPatterns.cpp
@@ -21,10 +24,8 @@ file(GLOB SRC
2124
SplitMultipleTapes.cpp
2225
cancel_inverses.cpp
2326
cp_global_buffers.cpp
24-
decompose_lowering.cpp
2527
dynamic_one_shot.cpp
2628
emit_catalyst_pyface.cpp
27-
graph_decomposition.cpp
2829
gridsynth.cpp
2930
ions_decompositions.cpp
3031
loop_boundary_optimization.cpp
@@ -64,5 +65,6 @@ target_include_directories(${LIBRARY_NAME} PUBLIC
6465
${PROJECT_SOURCE_DIR}/include
6566
${CMAKE_BINARY_DIR}/include
6667
AdjointLowering/
67-
DecompGraphSolver/
68+
GraphDecomposition/
69+
GraphDecomposition/DecompGraphSolver/
6870
)

mlir/lib/Quantum/Transforms/DecompGraphSolver/CMakeLists.txt renamed to mlir/lib/Quantum/Transforms/GraphDecomposition/DecompGraphSolver/CMakeLists.txt

File renamed without changes.

mlir/lib/Quantum/Transforms/DecompGraphSolver/DGBuilder.cpp renamed to mlir/lib/Quantum/Transforms/GraphDecomposition/DecompGraphSolver/DGBuilder.cpp

File renamed without changes.

mlir/lib/Quantum/Transforms/DecompGraphSolver/DGBuilder.hpp renamed to mlir/lib/Quantum/Transforms/GraphDecomposition/DecompGraphSolver/DGBuilder.hpp

File renamed without changes.

mlir/lib/Quantum/Transforms/DecompGraphSolver/DGSolver.cpp renamed to mlir/lib/Quantum/Transforms/GraphDecomposition/DecompGraphSolver/DGSolver.cpp

File renamed without changes.

mlir/lib/Quantum/Transforms/DecompGraphSolver/DGSolver.hpp renamed to mlir/lib/Quantum/Transforms/GraphDecomposition/DecompGraphSolver/DGSolver.hpp

File renamed without changes.

mlir/lib/Quantum/Transforms/DecompGraphSolver/DGTypes.hpp renamed to mlir/lib/Quantum/Transforms/GraphDecomposition/DecompGraphSolver/DGTypes.hpp

File renamed without changes.

mlir/lib/Quantum/Transforms/DecompGraphSolver/DGUtils.hpp renamed to mlir/lib/Quantum/Transforms/GraphDecomposition/DecompGraphSolver/DGUtils.hpp

File renamed without changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2026 Xanadu Quantum Technologies Inc.
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "DecompUtils.hpp"
16+
17+
#include "mlir/IR/BuiltinAttributes.h"
18+
19+
using namespace mlir;
20+
21+
namespace catalyst {
22+
namespace quantum {
23+
namespace DecompUtils {
24+
25+
bool isDecompositionFunction(func::FuncOp func) { return func->hasAttr(target_gate_attr_name); }
26+
27+
StringRef getTargetGateName(func::FuncOp func)
28+
{
29+
if (auto target_op_attr = func->getAttrOfType<StringAttr>(target_gate_attr_name)) {
30+
return target_op_attr.getValue();
31+
}
32+
return StringRef{};
33+
}
34+
35+
uint64_t getNumWires(func::FuncOp func)
36+
{
37+
if (auto num_wires_attr = func->getAttrOfType<IntegerAttr>("num_wires")) {
38+
return num_wires_attr.getValue().getZExtValue();
39+
}
40+
return 0;
41+
}
42+
43+
bool isInDecompRule(Operation *op)
44+
{
45+
while (auto parentOp = op->getParentOp()) {
46+
if (auto funcOp = dyn_cast<func::FuncOp>(parentOp)) {
47+
if (funcOp->hasAttr(target_gate_attr_name)) {
48+
return true;
49+
}
50+
}
51+
op = parentOp;
52+
}
53+
return false;
54+
}
55+
56+
} // namespace DecompUtils
57+
} // namespace quantum
58+
} // namespace catalyst
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2026 Xanadu Quantum Technologies Inc.
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <cstdint>
18+
19+
#include "llvm/ADT/StringRef.h"
20+
#include "mlir/Dialect/Func/IR/FuncOps.h"
21+
#include "mlir/IR/Operation.h"
22+
23+
namespace catalyst {
24+
namespace quantum {
25+
26+
/**
27+
* @brief Shared helpers for the graph-decomposition and decompose-lowering passes.
28+
*
29+
* A decomposition rule is a `func.func` carrying the `target_gate` attribute, naming the
30+
* operator it decomposes. These utilities centralize the attribute names and the queries
31+
* that identify such rules so the passes agree on a single definition.
32+
*/
33+
namespace DecompUtils {
34+
35+
/// Attribute marking a `func.func` as a decomposition rule for the named target gate.
36+
inline constexpr llvm::StringRef target_gate_attr_name = "target_gate";
37+
38+
/// Attribute holding the target gate set a circuit function should decompose into.
39+
inline constexpr llvm::StringRef decomp_gateset_attr_name = "decomp_gateset";
40+
41+
/// True if @p func is a decomposition rule (i.e. carries the `target_gate` attribute).
42+
bool isDecompositionFunction(mlir::func::FuncOp func);
43+
44+
/// The target gate a decomposition rule decomposes, or an empty ref if @p func is not a rule.
45+
llvm::StringRef getTargetGateName(mlir::func::FuncOp func);
46+
47+
/// The `num_wires` attribute of @p func, or 0 if absent.
48+
uint64_t getNumWires(mlir::func::FuncOp func);
49+
50+
/// True if @p op is nested inside a decomposition rule function.
51+
bool isInDecompRule(mlir::Operation *op);
52+
53+
} // namespace DecompUtils
54+
} // namespace quantum
55+
} // namespace catalyst

0 commit comments

Comments
 (0)