-
Notifications
You must be signed in to change notification settings - Fork 197
fix: Resolved #993 - Support >32 Qubit simulations on AMD GPUs via 3D grid folding #1016
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
Open
sun-9545sunoj
wants to merge
7
commits into
quantumlib:main
Choose a base branch
from
sun-9545sunoj:gpu-scaling-fix
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.
+319
−55
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4c272a1
fix: resolve #993 -remove formating noise and isolate MI300X scaling …
sun-9545sunoj 8edca1d
Update copyright year and usage message for CUDA
sun-9545sunoj de169cc
Refactor memory management namespace and functions
sun-9545sunoj 6632260
Refactor memory management namespace and functions
sun-9545sunoj 626c1e2
Refactor memory management namespace and functions
sun-9545sunoj 064145d
Fix syntax error in SetStateUniformKernel
sun-9545sunoj 19ce35c
Merge branch 'main' into gpu-scaling-fix
sun-9545sunoj 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 |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| // Copyright 2026 Google LLC. All Rights Reserved. | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| #include <complex> | ||
| #include <iomanip> | ||
| #include <limits> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <unistd.h> | ||
| #include <vector> | ||
|
|
||
| #include "../lib/bitstring.h" | ||
| #include "../lib/circuit_qsim_parser.h" | ||
| #include "../lib/formux.h" | ||
| #include "../lib/fuser_basic.h" | ||
| #include "../lib/gates_qsim.h" | ||
| #include "../lib/io_file.h" | ||
| #include "../lib/run_qsimh.h" | ||
| #include "../lib/simmux.h" | ||
| #include "../lib/simulator_cuda.h" | ||
| #include "../lib/util.h" | ||
| #include "../lib/util_cpu.h" | ||
|
|
||
| constexpr char usage[] = | ||
| "usage:\n ./qsimh_base_cuda.x -c circuit_file " | ||
| "-d maximum_time -k part1_qubits " | ||
| "-w prefix -p num_prefix_gates -r num_root_gates " | ||
| "-t num_threads -n num_dblocks -v verbosity -z\n"; | ||
|
|
||
| struct Options { | ||
| std::string circuit_file; | ||
| std::vector<unsigned> part1; | ||
| uint64_t prefix; | ||
| unsigned maxtime = std::numeric_limits<unsigned>::max(); | ||
| unsigned num_prefix_gatexs = 0; | ||
| unsigned num_root_gatexs = 0; | ||
| unsigned num_threads = 256; | ||
| unsigned num_dblocks = 16; | ||
| unsigned verbosity = 0; | ||
| bool denormals_are_zeros = false; | ||
| }; | ||
|
|
||
| Options GetOptions(int argc, char* argv[]) { | ||
| Options opt; | ||
|
|
||
| int k; | ||
|
|
||
| auto to_int = [](const std::string& word) -> unsigned { | ||
| return std::stoul(word); | ||
| }; | ||
|
|
||
| while ((k = getopt(argc, argv, "c:d:k:w:p:r:t:n:v:z")) != -1) { | ||
| switch (k) { | ||
| case 'c': | ||
| opt.circuit_file = optarg; | ||
| break; | ||
| case 'd': | ||
| opt.maxtime = std::stoul(optarg); | ||
| break; | ||
| case 'k': | ||
| qsim::SplitString(optarg, ',', to_int, opt.part1); | ||
| break; | ||
| case 'w': | ||
| opt.prefix = std::stoull(optarg); | ||
| break; | ||
| case 'p': | ||
| opt.num_prefix_gatexs = std::stoul(optarg); | ||
| break; | ||
| case 'r': | ||
| opt.num_root_gatexs = std::stoul(optarg); | ||
| break; | ||
| case 't': | ||
| opt.num_threads = std::stoul(optarg); | ||
| break; | ||
| case 'n': | ||
| opt.num_dblocks = std::stoul(optarg); | ||
| break; | ||
| case 'v': | ||
| opt.verbosity = std::stoul(optarg); | ||
| break; | ||
| case 'z': | ||
| opt.denormals_are_zeros = true; | ||
| break; | ||
| default: | ||
| qsim::IO::errorf(usage); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| return opt; | ||
| } | ||
|
|
||
| bool ValidateOptions(const Options& opt) { | ||
| if (opt.circuit_file.empty()) { | ||
| qsim::IO::errorf("circuit file is not provided.\n"); | ||
| qsim::IO::errorf(usage); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool ValidatePart1(unsigned num_qubits, const std::vector<unsigned>& part1) { | ||
| for (std::size_t i = 0; i < part1.size(); ++i) { | ||
| if (part1[i] >= num_qubits) { | ||
| qsim::IO::errorf("part 1 qubit indices are too large.\n"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| std::vector<unsigned> GetParts( | ||
| unsigned num_qubits, const std::vector<unsigned>& part1) { | ||
| std::vector<unsigned> parts(num_qubits, 0); | ||
|
|
||
| for (std::size_t i = 0; i < part1.size(); ++i) { | ||
| parts[part1[i]] = 1; | ||
| } | ||
|
|
||
| return parts; | ||
| } | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| using namespace qsim; | ||
|
|
||
| auto opt = GetOptions(argc, argv); | ||
| if (!ValidateOptions(opt)) { | ||
| return 1; | ||
| } | ||
|
|
||
| Circuit<GateQSim<float>> circuit; | ||
| if (!CircuitQsimParser<IOFile>::FromFile( | ||
| opt.maxtime, opt.circuit_file, circuit)) { | ||
| return 1; | ||
| } | ||
|
|
||
| if (!ValidatePart1(circuit.num_qubits, opt.part1)) { | ||
| return 1; | ||
| } | ||
| auto parts = GetParts(circuit.num_qubits, opt.part1); | ||
|
|
||
| if (opt.denormals_are_zeros) { | ||
| SetFlushToZeroAndDenormalsAreZeros(); | ||
| } | ||
|
|
||
| uint64_t num_bitstrings = | ||
| std::min(uint64_t{8}, uint64_t{1} << circuit.num_qubits); | ||
|
|
||
| std::vector<Bitstring> bitstrings; | ||
| bitstrings.reserve(num_bitstrings); | ||
| for (std::size_t i = 0; i < num_bitstrings; ++i) { | ||
| bitstrings.push_back(i); | ||
| } | ||
|
|
||
| struct Factory { | ||
| using Simulator = qsim::SimulatorCUDA<float>; | ||
| using StateSpace = Simulator::StateSpace; | ||
| using fp_type = Simulator::fp_type; | ||
|
|
||
| Factory(const StateSpace::Parameter& param) : param(param) {} | ||
|
|
||
| StateSpace CreateStateSpace() const { return StateSpace(param); } | ||
|
|
||
| Simulator CreateSimulator() const { return Simulator(); } | ||
|
|
||
| const StateSpace::Parameter& param; | ||
| }; | ||
|
|
||
| using HybridSimulator = | ||
| HybridSimulator<IO, GateQSim<float>, BasicGateFuser, For>; | ||
| using Runner = QSimHRunner<IO, HybridSimulator>; | ||
|
|
||
| Runner::Parameter param; | ||
| param.prefix = opt.prefix; | ||
| param.num_prefix_gatexs = opt.num_prefix_gatexs; | ||
| param.num_root_gatexs = opt.num_root_gatexs; | ||
| param.num_threads = | ||
| opt.num_threads; // This is reused for StateSpaceCUDA params implicitly | ||
| // if not careful, but here we separate. | ||
| param.verbosity = opt.verbosity; | ||
|
|
||
| std::vector<std::complex<Factory::fp_type>> results(num_bitstrings, 0); | ||
|
|
||
| // Setup CUDA parameters | ||
| Factory::StateSpace::Parameter cuda_param; | ||
| cuda_param.num_threads = opt.num_threads; | ||
| cuda_param.num_dblocks = opt.num_dblocks; | ||
|
|
||
| Factory factory(cuda_param); | ||
|
|
||
| if (Runner::Run(param, factory, circuit, parts, bitstrings, results)) { | ||
| static constexpr char const* bits[8] = { | ||
| "000", "001", "010", "011", "100", "101", "110", "111", | ||
| }; | ||
|
|
||
| unsigned s = 3 - std::min(unsigned{3}, circuit.num_qubits); | ||
|
|
||
| for (std::size_t i = 0; i < num_bitstrings; ++i) { | ||
| const auto& a = results[i]; | ||
| qsim::IO::messagef( | ||
| "%s:%16.8g%16.8g%16.8g\n", bits[i] + s, std::real(a), std::imag(a), | ||
| std::norm(a)); | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| } |
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
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.