-
Notifications
You must be signed in to change notification settings - Fork 14
Add in-memory IR parser #39
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
18 commits
Select commit
Hold shift + click to select a range
d1dff5c
Debug session with Narasinga
wongey 2f6f482
Update .gitignore for local files
wongey 2b991a1
Add module to parse in-memory LLVM IR string
wongey feb2640
Add test for from_bytes function
wongey cb2f3d5
Add link to build gtest with source if not found
wongey ff92375
Fix type errors
wongey dba450f
Require GTest and add instructions to build googletest locally
wongey 22a6481
Remove unncessary is_file variable and old non-working code
wongey 6c14c1b
Fix parse_ir_from_file test
wongey 393aebc
Remove local ignore from .gitignore
wongey 95c86f2
Remove cqiree files which belong to PR30
wongey 26f9ecc
Improve GTest finding and error message
wongey 760d98e
IWYU
wongey 06cb04d
Modify test so that it doesn't return a nullptr
wongey 22dd8b8
Add a QIR flag checker
wongey 5b122f6
Format Module.cc with clang-format
wongey ac09e20
iwyu
sethrj c19911f
Fix constructor ordering
sethrj 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,3 +48,5 @@ compile_commands.json | |
| cmake_install.cmake | ||
| /CMakeCache.txt | ||
| /CMakeFiles | ||
| .venv/ | ||
| *.sif | ||
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,246 @@ | ||
| //----------------------------------*-C++-*----------------------------------// | ||
| // Copyright 2025 UT-Battelle, LLC, and other QIR-EE developers. | ||
| // See the top-level COPYRIGHT file for details. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| //---------------------------------------------------------------------------// | ||
| //! \file cqiree/QireeManager.cc | ||
wongey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //---------------------------------------------------------------------------// | ||
| #include "QireeManager.hh" | ||
|
|
||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <stdexcept> | ||
|
|
||
| #include "qiree_config.h" | ||
|
|
||
| #include "qiree/Assert.hh" | ||
| #include "qiree/Executor.hh" | ||
| #include "qiree/Module.hh" | ||
| #include "qiree/QuantumInterface.hh" | ||
| #include "qiree/ResultDistribution.hh" | ||
| #include "qiree/SingleResultRuntime.hh" | ||
| #include "qirqsim/QsimQuantum.hh" | ||
| #include "qirqsim/QsimRuntime.hh" | ||
|
|
||
| #define CQIREE_FAIL(CODE, MESSAGE) \ | ||
| do \ | ||
| { \ | ||
| std::cerr << "qiree failure: " << MESSAGE << '\n'; \ | ||
| return ReturnCode::CODE; \ | ||
| } while (0) | ||
|
|
||
| namespace qiree | ||
| { | ||
| //---------------------------------------------------------------------------// | ||
| QireeManager::QireeManager() = default; | ||
| QireeManager::~QireeManager() = default; | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| QireeManager::ReturnCode | ||
| QireeManager::load_module(std::string_view data_contents) throw() | ||
| { | ||
| try | ||
| { | ||
| // Load module from memory contents | ||
| std::string content{data_contents}; | ||
| // Convert string_view to string for Module constructor | ||
| module_ = Module::from_bytes(content); | ||
| } | ||
| catch (std::exception const& e) | ||
| { | ||
| CQIREE_FAIL(fail_load, e.what()); | ||
| } | ||
| return ReturnCode::success; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| QireeManager::ReturnCode QireeManager::load_module(std::string filename) throw() | ||
| { | ||
| try | ||
| { | ||
| module_ = std::make_unique<Module>(filename); | ||
| QIREE_ENSURE(*module_); | ||
| } | ||
| catch (std::exception const& e) | ||
| { | ||
| std::cerr << "qiree failure: " << e.what() << '\n'; | ||
| CQIREE_FAIL(fail_load, e.what()); | ||
| } | ||
|
|
||
| return ReturnCode::success; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| QireeManager::ReturnCode QireeManager::num_quantum_reg(int& result) const | ||
| throw() | ||
| { | ||
| if (!module_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "cannot query module before module load"); | ||
| } | ||
| if (execute_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "cannot query module after creating executor"); | ||
| } | ||
|
|
||
| auto attrs = module_->load_entry_point_attrs(); | ||
| result = attrs.required_num_qubits; | ||
| return ReturnCode::success; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| QireeManager::ReturnCode QireeManager::num_classical_reg(int& result) const | ||
| throw() | ||
| { | ||
| if (!module_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "cannot query module before module load"); | ||
| } | ||
| if (execute_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "cannot query module after creating executor"); | ||
| } | ||
|
|
||
| auto attrs = module_->load_entry_point_attrs(); | ||
| result = attrs.required_num_results; | ||
| return ReturnCode::success; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| QireeManager::ReturnCode | ||
| QireeManager::setup_executor(std::string_view backend, | ||
| std::string_view config_json) throw() | ||
| { | ||
| if (!module_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "cannot create executor before module load"); | ||
| } | ||
| if (execute_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "cannot create executor again"); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| if (!config_json.empty()) | ||
| { | ||
| QIREE_NOT_IMPLEMENTED("CQiree JSON configuration input"); | ||
| } | ||
|
|
||
| if (backend == "qsim") | ||
| { | ||
| #if QIREE_USE_QSIM | ||
| // Create runtime interface: give runtime a pointer to quantum | ||
| // (lifetime of the reference is guaranteed by our shared pointer | ||
| // copy) | ||
| constexpr unsigned long int seed = 0; | ||
| auto quantum = std::make_shared<QsimQuantum>(std::cout, seed); | ||
| runtime_ = std::make_shared<QsimRuntime>(std::cout, *quantum); | ||
| quantum_ = std::move(quantum); | ||
| #else | ||
| QIREE_NOT_CONFIGURED("QSim"); | ||
| #endif | ||
| } | ||
| else if (backend == "xacc") | ||
| { | ||
| #if QIREE_USE_XACC | ||
| QIREE_NOT_IMPLEMENTED("XACC backend for CQiree"); | ||
| #else | ||
| QIREE_NOT_CONFIGURED("XACC"); | ||
| #endif | ||
| } | ||
| else | ||
| { | ||
| QIREE_VALIDATE(false, | ||
| << "unknown backend name '" << backend << "'"); | ||
| } | ||
| } | ||
| catch (std::exception const& e) | ||
| { | ||
| CQIREE_FAIL(fail_load, | ||
| "error while creating quantum runtimes: " << e.what()); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| // Create executor with the module, quantum and runtime interfaces | ||
| QIREE_ASSERT(module_ && *module_); | ||
| execute_ = std::make_unique<Executor>(std::move(*module_)); | ||
| } | ||
| catch (std::exception const& e) | ||
| { | ||
| CQIREE_FAIL(fail_load, "error while creating executor: " << e.what()); | ||
| } | ||
|
|
||
| return ReturnCode::success; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| QireeManager::ReturnCode QireeManager::execute(int num_shots) throw() | ||
| { | ||
| if (execute_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "cannot create executor again"); | ||
| } | ||
|
|
||
| if (num_shots <= 0) | ||
| { | ||
| CQIREE_FAIL(invalid_input, "num_shots was nonpositive"); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| QIREE_ASSERT(runtime_ && quantum_); | ||
| result_ = std::make_unique<ResultDistribution>(); | ||
|
|
||
| for (auto i = 0; i < num_shots; ++i) | ||
| { | ||
| (*execute_)(*quantum_, *runtime_); | ||
| result_->accumulate(runtime_->result()); | ||
| } | ||
| } | ||
| catch (std::exception const& e) | ||
| { | ||
| CQIREE_FAIL(fail_execute, e.what()); | ||
| } | ||
| return ReturnCode::success; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| QireeManager::ReturnCode QireeManager::num_results(int& count) const throw() | ||
| { | ||
| if (!result_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "execute has not been called"); | ||
| } | ||
|
|
||
| count = static_cast<int>(result_->size()); | ||
|
|
||
| return ReturnCode::success; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| QireeManager::ReturnCode | ||
| QireeManager::get_result(int index, std::string_view key, int* count) const | ||
| throw() | ||
| { | ||
| if (!result_) | ||
| { | ||
| CQIREE_FAIL(not_ready, "execute has not been called"); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| (void)sizeof(key); | ||
| (void)sizeof(count); | ||
| QIREE_NOT_IMPLEMENTED("getting results"); | ||
| } | ||
| catch (std::exception const& e) | ||
| { | ||
| CQIREE_FAIL(fail_execute, | ||
| "could not retrieve index " << index << ": " << e.what()); | ||
| } | ||
| return ReturnCode::success; | ||
| } | ||
| //---------------------------------------------------------------------------// | ||
| } // namespace qiree | ||
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
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.