-
-
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 10 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
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,169 @@ | ||
| /* | ||
| * 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/Dialect/QCO/IR/QCODialect.h" | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <iterator> | ||
| #include <llvm/ADT/TypeSwitch.h> | ||
| #include <llvm/Support/ErrorHandling.h> | ||
| #include <mlir/IR/Operation.h> | ||
| #include <mlir/Support/LLVM.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 qubit the iterator points to. | ||
| [[nodiscard]] mlir::Value qubit() const { | ||
| // A deallocation doesn't have an OpResult. | ||
burgholzer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (mlir::isa<qco::DeallocOp>(op_)) { | ||
| return nullptr; | ||
| } | ||
| return qubit_; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// @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
|
||
| WireIterator& operator++() { | ||
| forward(); | ||
| return *this; | ||
| } | ||
|
|
||
| WireIterator operator++(int) { | ||
| auto tmp = *this; | ||
| ++*this; | ||
MatthiasReumann marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return tmp; | ||
| } | ||
|
|
||
| WireIterator& operator--() { | ||
| backward(); | ||
| return *this; | ||
| } | ||
|
|
||
| WireIterator operator--(int) { | ||
| auto tmp = *this; | ||
| --*this; | ||
MatthiasReumann marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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() { | ||
| // 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<qco::DeallocOp>(op_)) { | ||
| isSentinel_ = true; | ||
| return; | ||
| } | ||
|
|
||
| if (!(mlir::isa<qco::AllocOp, qco::StaticOp>(op_))) { | ||
| // Find the output from the input qubit SSA value. | ||
| mlir::TypeSwitch<mlir::Operation*>(op_) | ||
| .Case<qco::UnitaryOpInterface>([&](qco::UnitaryOpInterface op) { | ||
| qubit_ = op.getOutputForInput(qubit_); | ||
| }) | ||
| .Case<qco::MeasureOp>( | ||
| [&](qco::MeasureOp op) { qubit_ = op.getQubitOut(); }) | ||
| .Case<qco::ResetOp>( | ||
| [&](qco::ResetOp op) { qubit_ = op.getQubitOut(); }) | ||
| .Default([&](mlir::Operation* op) { | ||
| report_fatal_error("unknown op in def-use chain: " + | ||
| op->getName().getStringRef()); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// @brief Move to the previous operation on the qubit wire. | ||
| void 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<qco::AllocOp, StaticOp>(op_)) { | ||
| return; | ||
| } | ||
|
|
||
| // Find the input from the output qubit SSA value. | ||
| mlir::TypeSwitch<mlir::Operation*>(op_) | ||
| .Case<qco::UnitaryOpInterface>([&](qco::UnitaryOpInterface op) { | ||
| qubit_ = op.getInputForOutput(qubit_); | ||
| }) | ||
| .Case<qco::MeasureOp>( | ||
| [&](qco::MeasureOp op) { qubit_ = op.getQubitIn(); }) | ||
| .Case<qco::ResetOp>([&](qco::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; | ||
| } | ||
| } | ||
|
|
||
| mlir::Operation* op_; | ||
| mlir::Value qubit_; | ||
| bool isSentinel_; | ||
| }; | ||
|
|
||
| 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."); | ||
burgholzer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } // 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
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 | ||
|
|
||
| add_executable(mqt-core-mlir-qco-dialect-utils-test test_wireiterator.cpp) | ||
|
|
||
| target_link_libraries( | ||
| mqt-core-mlir-qco-dialect-utils-test PRIVATE GTest::gtest_main MLIRQCODialect MLIRLLVMDialect | ||
| MLIRQCOProgramBuilder) | ||
|
|
||
| gtest_discover_tests(mqt-core-mlir-qco-dialect-utils-test) |
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,132 @@ | ||
| /* | ||
| * 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/Builder/QCOProgramBuilder.h" | ||
| #include "mlir/Dialect/QCO/IR/QCODialect.h" | ||
| #include "mlir/Dialect/QCO/Utils/WireIterator.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <iterator> | ||
| #include <llvm/Support/Debug.h> | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #include <memory> | ||
| #include <mlir/Dialect/Arith/IR/Arith.h> | ||
| #include <mlir/Dialect/ControlFlow/IR/ControlFlow.h> | ||
| #include <mlir/Dialect/Func/IR/FuncOps.h> | ||
| #include <mlir/Dialect/LLVMIR/LLVMDialect.h> | ||
| #include <mlir/Dialect/SCF/IR/SCF.h> | ||
| #include <mlir/IR/DialectRegistry.h> | ||
| #include <mlir/IR/MLIRContext.h> | ||
| #include <utility> | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace { | ||
| class WireIteratorTest : public testing::TestWithParam<bool> { | ||
| protected: | ||
| void SetUp() override { | ||
| DialectRegistry registry; | ||
| registry | ||
| .insert<qco::QCODialect, arith::ArithDialect, cf::ControlFlowDialect, | ||
| func::FuncDialect, scf::SCFDialect, LLVM::LLVMDialect>(); | ||
burgholzer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| context = std::make_unique<MLIRContext>(); | ||
| context->appendDialectRegistry(registry); | ||
| context->loadAllAvailableDialects(); | ||
| } | ||
|
|
||
| std::unique_ptr<MLIRContext> context; | ||
| }; | ||
| } // namespace | ||
|
|
||
| TEST_P(WireIteratorTest, MixedUse) { | ||
| const bool isDynamic = GetParam(); | ||
|
|
||
| // Build circuit. | ||
| qco::QCOProgramBuilder builder(context.get()); | ||
| builder.initialize(); | ||
| const auto q00 = isDynamic ? builder.allocQubit() : builder.staticQubit(0); | ||
| const auto q10 = isDynamic ? builder.allocQubit() : builder.staticQubit(1); | ||
| const auto q01 = builder.h(q00); | ||
| const auto [q02, q11] = builder.cx(q01, q10); | ||
| const auto [q03, c0] = builder.measure(q02); | ||
| const auto q04 = builder.reset(q03); | ||
| builder.dealloc(q04); | ||
| builder.dealloc(q11); | ||
|
|
||
| // Setup WireIterator. | ||
| auto module = builder.finalize(); | ||
MatthiasReumann marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| qco::WireIterator it(q00); | ||
|
|
||
| // | ||
| // Test: Forward Iteration | ||
| // | ||
|
|
||
| ASSERT_EQ(it.operation(), q00.getDefiningOp()); // qco.alloc | ||
| ASSERT_EQ(it.qubit(), q00); | ||
|
|
||
| ++it; | ||
| ASSERT_EQ(it.operation(), q01.getDefiningOp()); // qco.h | ||
| ASSERT_EQ(it.qubit(), q01); | ||
|
|
||
| ++it; | ||
| ASSERT_EQ(it.operation(), q02.getDefiningOp()); // qco.ctrl | ||
| ASSERT_EQ(it.qubit(), q02); | ||
|
|
||
| ++it; | ||
| ASSERT_EQ(it.operation(), q03.getDefiningOp()); // qco.measure | ||
| ASSERT_EQ(it.qubit(), q03); | ||
|
|
||
| ++it; | ||
| ASSERT_EQ(it.operation(), q04.getDefiningOp()); // qco.reset | ||
| ASSERT_EQ(it.qubit(), q04); | ||
|
|
||
| ++it; | ||
| ASSERT_EQ(it.operation(), *(q04.getUsers().begin())); // qco.dealloc | ||
| ASSERT_EQ(it.qubit(), nullptr); | ||
|
|
||
| ++it; | ||
| ASSERT_EQ(it, std::default_sentinel); | ||
|
|
||
| ++it; | ||
| ASSERT_EQ(it, std::default_sentinel); | ||
|
|
||
| // | ||
| // Test: Backward Iteration | ||
| // | ||
|
|
||
| --it; | ||
| ASSERT_EQ(it.operation(), *(q04.getUsers().begin())); // qco.dealloc | ||
| ASSERT_EQ(it.qubit(), nullptr); | ||
|
|
||
| --it; | ||
| ASSERT_EQ(it.operation(), q04.getDefiningOp()); // qco.reset | ||
| ASSERT_EQ(it.qubit(), q04); | ||
|
|
||
| --it; | ||
| ASSERT_EQ(it.operation(), q03.getDefiningOp()); // qco.measure | ||
| ASSERT_EQ(it.qubit(), q03); | ||
|
|
||
| --it; | ||
| ASSERT_EQ(it.operation(), q02.getDefiningOp()); // qco.ctrl | ||
| ASSERT_EQ(it.qubit(), q02); | ||
|
|
||
| --it; | ||
| ASSERT_EQ(it.operation(), q01.getDefiningOp()); // qco.h | ||
| ASSERT_EQ(it.qubit(), q01); | ||
|
|
||
| --it; | ||
| ASSERT_EQ(it.operation(), q00.getDefiningOp()); // qco.alloc or qco.static | ||
| ASSERT_EQ(it.qubit(), q00); | ||
| } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(DynamicAndStatic, WireIteratorTest, ::testing::Bool(), | ||
| [](const ::testing::TestParamInfo<bool>& info) { | ||
| return info.param ? "Dynamic" : "Static"; | ||
| }); | ||
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.