-
-
Notifications
You must be signed in to change notification settings - Fork 53
Add WireIterator for the QCO Dialect
#1510
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
7071676
Implement Forward WireIterator
MatthiasReumann d1b207b
Add unit tests for forward iteration
MatthiasReumann d1f193c
Fix underscore
MatthiasReumann 9c0a51d
Add backwards iteration
MatthiasReumann aabe701
Fix linting issues
MatthiasReumann 6c2f205
Add backwards tests
MatthiasReumann 147d09e
Add static qubit tests
MatthiasReumann d54d3e2
Remove use_empty check
MatthiasReumann e625961
Apply bunny suggestions
MatthiasReumann dd7c56e
Improve semantic consistency
MatthiasReumann 10918bf
Linting and bunny
MatthiasReumann db5a5b3
Fix segfault
MatthiasReumann dbe5b9a
Merge branch 'main' into feat/qco-wireiterator
MatthiasReumann 01cfaa2
Improve coverage
MatthiasReumann c6fb67a
Update CHANGELOG.md
MatthiasReumann e9063cd
Apply suggestions from code review
MatthiasReumann fcab55e
Update CHANGELOG.md
MatthiasReumann f16f5e0
Fix compilation
MatthiasReumann 869fe4d
Add [[maybe_unused]] again
MatthiasReumann 9d3b24a
Merge branch 'main' into feat/qco-wireiterator
MatthiasReumann 0604dd0
Remove unused dialects
MatthiasReumann d100f20
Add WireIterator.cpp
MatthiasReumann 17c7392
Merge branch 'main' into feat/qco-wireiterator
MatthiasReumann 027bd68
Fix linting issues
MatthiasReumann 67f1ad7
Move static asserts to .cpp file
MatthiasReumann e594de9
Link against MLIRQCODialect
MatthiasReumann 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
Some comments aren't visible on the classic Files Changed page.
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
burgholzer marked this conversation as resolved.
Show resolved
Hide resolved
|
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,85 @@ | ||
| /* | ||
| * 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 <iterator> | ||
| #include <mlir/IR/Operation.h> | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| namespace mlir::qco { | ||
|
|
||
| /** | ||
| * @brief A bidirectional_iterator traversing the def-use chain of a qubit wire. | ||
| * | ||
| * The iterator follows the flow of a qubit through a sequence of quantum | ||
| * operations while respecting the semantics of the respective operation. | ||
| **/ | ||
| class [[nodiscard]] WireIterator { | ||
burgholzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public: | ||
| using iterator_category = std::bidirectional_iterator_tag; | ||
| using difference_type = std::ptrdiff_t; | ||
| using value_type = mlir::Operation*; | ||
|
|
||
| WireIterator() : op_(nullptr), qubit_(nullptr), isSentinel_(false) {} | ||
| explicit WireIterator(mlir::Value qubit) | ||
| : op_(qubit.getDefiningOp()), qubit_(qubit), isSentinel_(false) {} | ||
|
|
||
| /// @returns the operation the iterator points to. | ||
| [[nodiscard]] mlir::Operation* operation() const { return op_; } | ||
|
|
||
| /// @returns the operation the iterator points to. | ||
| [[nodiscard]] mlir::Operation* operator*() const { return operation(); } | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// @returns the qubit the iterator points to. | ||
| [[nodiscard]] mlir::Value qubit() const; | ||
|
|
||
| WireIterator& operator++() { | ||
| forward(); | ||
| return *this; | ||
| } | ||
|
|
||
| WireIterator operator++(int) { | ||
| auto tmp = *this; | ||
| operator++(); | ||
| return tmp; | ||
| } | ||
|
|
||
| WireIterator& operator--() { | ||
| backward(); | ||
| return *this; | ||
| } | ||
|
|
||
| WireIterator operator--(int) { | ||
| auto tmp = *this; | ||
| operator--(); | ||
| return tmp; | ||
| } | ||
|
|
||
| bool operator==(const WireIterator& other) const { | ||
| return other.qubit_ == qubit_ && other.op_ == op_ && | ||
| other.isSentinel_ == isSentinel_; | ||
| } | ||
|
|
||
| bool operator==([[maybe_unused]] std::default_sentinel_t s) const { | ||
| return isSentinel_; | ||
| } | ||
|
|
||
| private: | ||
| /// @brief Move to the next operation on the qubit wire. | ||
| void forward(); | ||
|
|
||
| /// @brief Move to the previous operation on the qubit wire. | ||
| void backward(); | ||
|
|
||
| mlir::Operation* op_; | ||
| mlir::Value qubit_; | ||
| bool isSentinel_; | ||
| }; | ||
| } // namespace mlir::qco | ||
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 |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ | |
|
|
||
| add_subdirectory(Builder) | ||
| add_subdirectory(IR) | ||
| add_subdirectory(Utils) | ||
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,44 @@ | ||
| # 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 | ||
|
|
||
| file(GLOB_RECURSE UTILS_CPP "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") | ||
|
|
||
| add_mlir_dialect_library( | ||
| MLIRQCOUtils | ||
| ${UTILS_CPP} | ||
| ADDITIONAL_HEADER_DIRS | ||
| ${PROJECT_SOURCE_DIR}/mlir/include/mlir/Dialect/QCO | ||
| DEPENDS | ||
| MLIRQCOOpsIncGen | ||
| MLIRQCOInterfacesIncGen | ||
| LINK_LIBS | ||
| PUBLIC | ||
| MLIRQCODialect | ||
| DISABLE_INSTALL) | ||
|
|
||
| mqt_mlir_target_use_project_options(MLIRQCOUtils) | ||
|
|
||
| # collect header files | ||
| file(GLOB_RECURSE UTILS_HEADERS_SOURCE "${MQT_MLIR_SOURCE_INCLUDE_DIR}/mlir/Dialect/QCO/Utils/*.h") | ||
| file(GLOB_RECURSE UTILS_HEADERS_BUILD "${MQT_MLIR_BUILD_INCLUDE_DIR}/mlir/Dialect/QCO/Utils/*.inc") | ||
|
|
||
| # add public headers using file sets | ||
| target_sources( | ||
| MLIRQCOUtils | ||
| PUBLIC FILE_SET | ||
| HEADERS | ||
| BASE_DIRS | ||
| ${MQT_MLIR_SOURCE_INCLUDE_DIR} | ||
| FILES | ||
| ${UTILS_HEADERS_SOURCE} | ||
| FILE_SET | ||
| HEADERS | ||
| BASE_DIRS | ||
| ${MQT_MLIR_BUILD_INCLUDE_DIR} | ||
| FILES | ||
| ${UTILS_HEADERS_BUILD}) |
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,105 @@ | ||
| /* | ||
| * 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/Utils/WireIterator.h" | ||
|
|
||
| #include "mlir/Dialect/QCO/IR/QCOInterfaces.h" | ||
| #include "mlir/Dialect/QCO/IR/QCOOps.h" | ||
|
|
||
| #include <cassert> | ||
| #include <iterator> | ||
| #include <llvm/ADT/TypeSwitch.h> | ||
| #include <llvm/Support/ErrorHandling.h> | ||
| #include <mlir/IR/Operation.h> | ||
| #include <mlir/IR/Value.h> | ||
| #include <mlir/Support/LLVM.h> | ||
|
|
||
| namespace mlir::qco { | ||
| mlir::Value WireIterator::qubit() const { | ||
| // A deallocation doesn't have an OpResult. | ||
| if (op_ != nullptr && mlir::isa<DeallocOp>(op_)) { | ||
| return nullptr; | ||
| } | ||
| return qubit_; | ||
| } | ||
|
|
||
| void WireIterator::forward() { | ||
| // If the iterator is a sentinel already, there is nothing to do. | ||
| if (isSentinel_) { | ||
| return; | ||
| } | ||
|
|
||
| // Find the user-operation of the qubit SSA value. | ||
| assert(qubit_.getNumUses() == 1 && "expected linear typing"); | ||
| op_ = *(qubit_.getUsers().begin()); | ||
|
|
||
| // A deallocation op defines the end of the qubit wire (dynamic and static). | ||
| if (mlir::isa<DeallocOp>(op_)) { | ||
| isSentinel_ = true; | ||
| return; | ||
| } | ||
|
|
||
| if (!(mlir::isa<AllocOp, StaticOp>(op_))) { | ||
| // Find the output from the input qubit SSA value. | ||
| mlir::TypeSwitch<mlir::Operation*>(op_) | ||
| .Case<UnitaryOpInterface>([&](UnitaryOpInterface op) { | ||
| qubit_ = op.getOutputForInput(qubit_); | ||
| }) | ||
| .Case<MeasureOp>([&](MeasureOp op) { qubit_ = op.getQubitOut(); }) | ||
| .Case<ResetOp>([&](ResetOp op) { qubit_ = op.getQubitOut(); }) | ||
| .Default([&](mlir::Operation* op) { | ||
| report_fatal_error("unknown op in def-use chain: " + | ||
| op->getName().getStringRef()); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| void WireIterator::backward() { | ||
| // If the iterator is a sentinel, reactivate the iterator. | ||
| if (isSentinel_) { | ||
| isSentinel_ = false; | ||
| return; | ||
| } | ||
|
|
||
| // For deallocations, qubit_ is an OpOperand. Hence, only get the def-op. | ||
| if (mlir::isa<DeallocOp>(op_)) { | ||
| op_ = qubit_.getDefiningOp(); | ||
| return; | ||
| } | ||
|
|
||
| // Allocations or static definitions define the start of the qubit wire. | ||
| // Consequently, stop and early exit. | ||
| if (mlir::isa<AllocOp, StaticOp>(op_)) { | ||
| return; | ||
| } | ||
|
|
||
| // Find the input from the output qubit SSA value. | ||
| mlir::TypeSwitch<mlir::Operation*>(op_) | ||
| .Case<UnitaryOpInterface>( | ||
| [&](UnitaryOpInterface op) { qubit_ = op.getInputForOutput(qubit_); }) | ||
| .Case<MeasureOp>([&](MeasureOp op) { qubit_ = op.getQubitIn(); }) | ||
| .Case<ResetOp>([&](ResetOp op) { qubit_ = op.getQubitIn(); }) | ||
| .Default([&](mlir::Operation* op) { | ||
| report_fatal_error("unknown op in def-use chain: " + | ||
| op->getName().getStringRef()); | ||
| }); | ||
|
|
||
| // Get the operation that produces the qubit value. | ||
| // If the current qubit SSA value is a BlockArgument (no defining op), stop. | ||
| op_ = qubit_.getDefiningOp(); | ||
| if (op_ == nullptr) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| static_assert(std::bidirectional_iterator<WireIterator>); | ||
| static_assert(std::sentinel_for<std::default_sentinel_t, WireIterator>, | ||
| "std::default_sentinel_t must be a sentinel for WireIterator."); | ||
| } // namespace mlir::qco |
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 |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ | |
| # Licensed under the MIT License | ||
|
|
||
| add_subdirectory(IR) | ||
| add_subdirectory(Utils) | ||
burgholzer marked this conversation as resolved.
Show resolved
Hide resolved
|
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,15 @@ | ||
| # 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 | ||
|
|
||
| set(qco_utils_target mqt-core-mlir-unittest-qco-utils) | ||
| add_executable(${qco_utils_target} test_wireiterator.cpp) | ||
| target_link_libraries(${qco_utils_target} PRIVATE GTest::gtest_main MLIRQCODialect MLIRQCOUtils | ||
| MLIRQCOProgramBuilder) | ||
| mqt_mlir_configure_unittest_target(${qco_utils_target}) | ||
|
|
||
| gtest_discover_tests(${qco_utils_target} PROPERTIES LABELS mqt-mlir-unittests DISCOVERY_TIMEOUT 60) |
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.