-
Notifications
You must be signed in to change notification settings - Fork 57
Add functionality to obtain some specs/info on quantum operations #2038
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
Draft
zsb-xqc
wants to merge
18
commits into
main
Choose a base branch
from
quantum-specs-info
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.
+271
−14
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b48f4d3
Add depth analysis
sengthai 9129b00
Improve import fromat
sengthai bd0f325
Improve the algo
sengthai 253c5a8
Merge branch 'main' into ppm-specs-depth
sengthai bb62293
Added changelog
sengthai c894d12
Rename keys
sengthai bffe380
Fix StringSaver
sengthai 544951e
First commit for adding quantum specs info pass (quantum-specs-info).…
zsb-xqc 0fedea0
Test file for quantum-specs-info pass.
zsb-xqc 0f9c9a4
Fix formatting
zsb-xqc 462960f
Merge branch 'main' into quantum-specs-info
zsb-xqc 9bff7cc
Fix error due to incorrect merging with stashed results.
zsb-xqc 6c8ae3b
Formatting fix
zsb-xqc 505d167
Formatting fix
zsb-xqc 59a1979
Update __init__.py
zsb-xqc 0f3381c
Update __init__.py
zsb-xqc 92f52af
Update compiler.py
zsb-xqc e053012
Update compiler.py
zsb-xqc 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
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
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
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
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 2025 Xanadu Quantum Technologies Inc. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#define DEBUG_TYPE "quantum-specs-info" | ||
|
||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "llvm/Support/Debug.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include <nlohmann/json.hpp> | ||
|
||
#include "QEC/IR/QECDialect.h" | ||
#include "Quantum/IR/QuantumDialect.h" | ||
#include "Quantum/IR/QuantumInterfaces.h" | ||
#include "Quantum/IR/QuantumOps.h" | ||
|
||
using namespace llvm; | ||
using namespace mlir; | ||
using namespace catalyst; | ||
using namespace catalyst::quantum; | ||
using namespace catalyst::qec; | ||
using json = nlohmann::json; | ||
|
||
namespace catalyst { | ||
namespace quantum { | ||
|
||
#define GEN_PASS_DEF_QUANTUMSPECSINFOPASS | ||
#define GEN_PASS_DECL_QUANTUMSPECSINFOPASS | ||
|
||
#include "Quantum/Transforms/Passes.h.inc" | ||
|
||
struct QuantumSpecsInfoPass : public impl::QuantumSpecsInfoPassBase<QuantumSpecsInfoPass> { | ||
using impl::QuantumSpecsInfoPassBase<QuantumSpecsInfoPass>::QuantumSpecsInfoPassBase; | ||
|
||
LogicalResult printQuantumSpecs() | ||
{ | ||
llvm::BumpPtrAllocator stringAllocator; | ||
llvm::DenseMap<StringRef, llvm::DenseMap<StringRef, int>> PPMSpecs; | ||
|
||
int numQuantumGates = 0; | ||
int numMeasureOp = 0; | ||
int numPPRotation = 0; | ||
int numPPMeasurement = 0; | ||
int numTotal = 0; | ||
|
||
// Walk over all operations in the IR (could be ModuleOp or FuncOp) | ||
WalkResult wr = getOperation()->walk([&](Operation *op) { | ||
if (isa<quantum::QuantumGate>(op)) { | ||
auto parentFuncOp = op->getParentOfType<func::FuncOp>(); | ||
StringRef funcName = parentFuncOp.getName(); | ||
// llvm::errs() << funcName; | ||
PPMSpecs[funcName]["QuantumGate_Count"] = ++numQuantumGates; | ||
PPMSpecs[funcName]["Total_Count"] = ++numTotal; | ||
return WalkResult::advance(); | ||
} | ||
|
||
if (isa<quantum::MeasureOp>(op)) { | ||
auto parentFuncOp = op->getParentOfType<func::FuncOp>(); | ||
StringRef funcName = parentFuncOp.getName(); | ||
// llvm::errs() << funcName; | ||
PPMSpecs[funcName]["MeasureOp_Count"] = ++numMeasureOp; | ||
PPMSpecs[funcName]["Total_Count"] = ++numTotal; | ||
return WalkResult::advance(); | ||
} | ||
// Count PPMs | ||
else if (isa<qec::PPMeasurementOp>(op)) { | ||
auto parentFuncOp = op->getParentOfType<func::FuncOp>(); | ||
StringRef funcName = parentFuncOp.getName(); | ||
PPMSpecs[funcName]["PPMeasurement_Count"] = ++numPPMeasurement; | ||
PPMSpecs[funcName]["Total_Count"] = ++numTotal; | ||
return WalkResult::advance(); | ||
} | ||
// Count PPRs | ||
else if (isa<qec::PPRotationOp>(op)) { | ||
auto parentFuncOp = op->getParentOfType<func::FuncOp>(); | ||
StringRef funcName = parentFuncOp.getName(); | ||
PPMSpecs[funcName]["PPRotation_Count"] = ++numPPRotation; | ||
PPMSpecs[funcName]["Total_Count"] = ++numTotal; | ||
return WalkResult::advance(); | ||
} | ||
// Skip other ops | ||
else { | ||
return WalkResult::skip(); | ||
} | ||
}); | ||
if (wr.wasInterrupted()) { | ||
return failure(); | ||
} | ||
|
||
std::error_code EC; | ||
// llvm::raw_fd_ostream fileOutputStream("test.txt", EC, llvm::sys::fs::OF_None); | ||
llvm::raw_fd_ostream fileOutputStream("test.txt", EC, llvm::sys::fs::OF_Append); | ||
if (EC) { | ||
llvm::errs() << "Error opening file: " << EC.message() << "\n"; | ||
return failure(); // Handle error | ||
} | ||
json PPMSpecsJson = PPMSpecs; | ||
llvm::outs() << PPMSpecsJson.dump(4) | ||
<< "\n"; // dump(4) makes an indent with 4 spaces when printing JSON | ||
fileOutputStream << PPMSpecsJson.dump(4) | ||
<< "\n"; // dump(4) makes an indent with 4 spaces when printing JSON | ||
fileOutputStream.flush(); | ||
return success(); | ||
} | ||
|
||
void runOnOperation() final | ||
{ | ||
if (failed(printQuantumSpecs())) { | ||
signalPassFailure(); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace quantum | ||
|
||
std::unique_ptr<Pass> createQuantumSpecsInfoPass() | ||
{ | ||
return std::make_unique<QuantumSpecsInfoPass>(); | ||
} | ||
|
||
} // namespace catalyst |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's nice to write into a file for future use. 👍 It would be great if we had an option for the user to disable or enable this feature.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing @sengthai!, Yes I will add to front end also and give option to enable/disable writing to file.