-
-
Notifications
You must be signed in to change notification settings - Fork 54
✨ Add walkUnit Driver
#1583
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
✨ Add walkUnit Driver
#1583
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
939e306
Fix SSA dominance issues
MatthiasReumann 4b64016
Add walkUnit function
MatthiasReumann f6674b2
Add Drivers.cpp
MatthiasReumann 4512b8f
Fix linting
MatthiasReumann 8166ae2
Update CHANGELOG.md
MatthiasReumann 297d931
Add additional assert statement
MatthiasReumann efc9e16
Add doxygen for walkUnit
MatthiasReumann c13905e
Add drivers unittest
MatthiasReumann c0a47fd
Fix lint
MatthiasReumann b0a730d
Merge branch 'main' into enh/alternative-commit-trial
MatthiasReumann 3f521c9
Update CMakeLists.txt
MatthiasReumann 8f06cfc
Apply PR suggestions
MatthiasReumann b7be148
Remove no discard
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * 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" | ||
| #include "mlir/Dialect/QCO/IR/QCOOps.h" | ||
|
|
||
| #include <llvm/ADT/TypeSwitch.h> | ||
| #include <mlir/IR/Region.h> | ||
| #include <mlir/IR/Value.h> | ||
| #include <mlir/Support/LLVM.h> | ||
|
|
||
| #include <cstddef> | ||
| #include <utility> | ||
|
|
||
| namespace mlir::qco { | ||
| class Qubits { | ||
| /** | ||
| * @brief Specifies the qubit "location" (static or dynamic). | ||
burgholzer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| enum class QubitLocation : std::uint8_t { Hardware, Program }; | ||
|
|
||
| public: | ||
| /** | ||
| * @brief Add qubit with automatically assigned dynamic index. | ||
| */ | ||
| [[maybe_unused]] void add(TypedValue<QubitType> q); | ||
|
|
||
| /** | ||
| * @brief Add qubit with static index. | ||
| */ | ||
| void add(TypedValue<QubitType> q, std::size_t hw); | ||
|
|
||
| /** | ||
| * @brief Remap the qubit value from prev to next. | ||
| */ | ||
| void remap(TypedValue<QubitType> prev, TypedValue<QubitType> next); | ||
|
|
||
| /** | ||
| * @brief Remove the qubit value. | ||
| */ | ||
| void remove(TypedValue<QubitType> q); | ||
|
|
||
| /** | ||
| * @returns the qubit value assigned to a program index. | ||
| */ | ||
| [[maybe_unused]] TypedValue<QubitType> getProgramQubit(std::size_t index); | ||
|
|
||
| /** | ||
| * @returns the qubit value assigned to a hardware index. | ||
| */ | ||
| TypedValue<QubitType> getHardwareQubit(std::size_t index); | ||
|
|
||
| private: | ||
| DenseMap<std::size_t, TypedValue<QubitType>> programToValue_; | ||
| DenseMap<std::size_t, TypedValue<QubitType>> hardwareToValue_; | ||
| DenseMap<TypedValue<QubitType>, std::pair<QubitLocation, std::size_t>> | ||
| valueToIndex_; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Perform top-down non-recursive walk of all operations within the a | ||
burgholzer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * region and apply callback function. | ||
burgholzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @details The signature of the callback function is: | ||
| * | ||
| * (Operation*, Qubits& q) -> WalkResult | ||
| * | ||
| * where the Qubits object tracks the front of qubit SSA values. | ||
| */ | ||
| template <typename Fn> void walkUnit(Region& region, Fn&& fn) { | ||
| const auto ffn = std::forward<Fn>(fn); | ||
|
|
||
| Qubits qubits; | ||
| for (Operation& curr : region.getOps()) { | ||
| if (ffn(&curr, qubits).wasInterrupted()) { | ||
| break; | ||
| }; | ||
|
|
||
| TypeSwitch<Operation*>(&curr) | ||
| .template Case<StaticOp>( | ||
| [&](StaticOp op) { qubits.add(op.getQubit(), op.getIndex()); }) | ||
| .template Case<AllocOp>([&](AllocOp op) { qubits.add(op.getResult()); }) | ||
| .template Case<UnitaryOpInterface>([&](UnitaryOpInterface op) { | ||
| for (const auto& [prevV, nextV] : | ||
| llvm::zip(op.getInputQubits(), op.getOutputQubits())) { | ||
| const auto prevQ = cast<TypedValue<QubitType>>(prevV); | ||
| const auto nextQ = cast<TypedValue<QubitType>>(nextV); | ||
| qubits.remap(prevQ, nextQ); | ||
| } | ||
| }) | ||
| .template Case<ResetOp>([&](ResetOp op) { | ||
| qubits.remap(op.getQubitIn(), op.getQubitOut()); | ||
| }) | ||
| .template Case<MeasureOp>([&](MeasureOp op) { | ||
| qubits.remap(op.getQubitIn(), op.getQubitOut()); | ||
| }) | ||
| .template Case<DeallocOp>( | ||
| [&](DeallocOp op) { qubits.remove(op.getQubit()); }); | ||
| } | ||
| } | ||
| } // 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.