-
Notifications
You must be signed in to change notification settings - Fork 186
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
Fix QPUD Library Mode Installer Build #1435
Merged
1tnguyen
merged 28 commits into
NVIDIA:main
from
1tnguyen:tnguyen/installer-qpud-lib-mode
Apr 17, 2024
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4a85c98
Experimenting fixes for lib mode for qpud in the installer
1tnguyen 81eafb1
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen 79fe9b0
Add %cpp_std to run commands
1tnguyen 91be627
Use make_copyable_function util
1tnguyen 7e83939
Debug the missing symbol
1tnguyen be18e22
Revert "Debug the missing symbol"
1tnguyen d1707af
Try something
1tnguyen 63d6fdb
Trying a fix
1tnguyen afb5779
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen c04a393
Remove obsolete code comments
1tnguyen 0a0e5e3
Adds strict C++-17 checks
1tnguyen 22c6016
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen 59aa9d0
Fixes for C++17 and unit tests
1tnguyen 8d2c726
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen ebe8283
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen 21dc2b0
Remove the workaround for cnot gate in C++17
1tnguyen 5e7bcbf
Debug test issues for installer
1tnguyen 321d034
Fixed issue with args by ref
1tnguyen 9823eee
Revert "Debug test issues for installer"
1tnguyen be42b61
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen 6782d41
Set seed for test repeatability
1tnguyen d04c4eb
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen f249a56
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen 19beb05
Fixes after merge
1tnguyen ef84840
Fixed a typo: REQURIES -> REQUIRES
1tnguyen 2c76555
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen 4ad7ecd
Address code review
1tnguyen 574dcef
Merge branch 'main' into tnguyen/installer-qpud-lib-mode
1tnguyen 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 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 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 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 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 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 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 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,47 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. * | ||
* All rights reserved. * | ||
* * | ||
* This source code and the accompanying materials are made available under * | ||
* the terms of the Apache License 2.0 which accompanies this distribution. * | ||
******************************************************************************/ | ||
|
||
// REQUIRES: c++17 | ||
// REQUIRES: remote-sim | ||
|
||
// clang-format off | ||
// RUN: nvq++ %cpp_std --target remote-mqpu --remote-mqpu-auto-launch 1 %s -o %t && %t | ||
// RUN: nvq++ %cpp_std --enable-mlir --target remote-mqpu --remote-mqpu-auto-launch 1 %s -o %t && %t | ||
// clang-format on | ||
|
||
#include <cudaq.h> | ||
|
||
void ghz(std::size_t N) __qpu__ { | ||
cudaq::qvector q(N); | ||
h(q[0]); | ||
for (int i = 0; i < N - 1; i++) { | ||
cx(q[i], q[i + 1]); | ||
} | ||
mz(q); | ||
} | ||
|
||
void ansatz(double theta) __qpu__ { | ||
cudaq::qvector q(2); | ||
x(q[0]); | ||
ry(theta, q[1]); | ||
cx(q[1], q[0]); | ||
} | ||
|
||
int main() { | ||
auto counts = cudaq::sample(ghz, 10); | ||
counts.dump(); | ||
assert(counts.size() == 2); | ||
using namespace cudaq::spin; | ||
cudaq::spin_op h = 5.907 - 2.1433 * x(0) * x(1) - 2.1433 * y(0) * y(1) + | ||
.21829 * z(0) - 6.125 * z(1); | ||
|
||
double energy = cudaq::observe(ansatz, h, .59); | ||
printf("Energy is %lf\n", energy); | ||
assert(std::abs(energy + 1.748794) < 1e-3); | ||
return 0; | ||
} |
This file contains 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 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,62 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. * | ||
* All rights reserved. * | ||
* * | ||
* This source code and the accompanying materials are made available under * | ||
* the terms of the Apache License 2.0 which accompanies this distribution. * | ||
******************************************************************************/ | ||
|
||
// REQUIRES: c++17 | ||
// REQUIRES: remote-sim | ||
|
||
// clang-format off | ||
// RUN: nvq++ %cpp_std --target remote-mqpu --remote-mqpu-auto-launch 1 %s -o %t && %t | ||
// RUN: nvq++ %cpp_std --enable-mlir --target remote-mqpu --remote-mqpu-auto-launch 1 %s -o %t && %t | ||
// clang-format on | ||
|
||
#include <cudaq.h> | ||
#include <cudaq/algorithm.h> | ||
#include <cudaq/builder.h> | ||
#include <cudaq/gradients.h> | ||
#include <cudaq/optimizers.h> | ||
|
||
struct ansatz { | ||
auto operator()(double theta) __qpu__ { | ||
cudaq::qvector q(2); | ||
x(q[0]); | ||
ry(theta, q[1]); | ||
cx(q[1], q[0]); | ||
} | ||
}; | ||
|
||
int main() { | ||
using namespace cudaq::spin; | ||
cudaq::spin_op h = 5.907 - 2.1433 * x(0) * x(1) - 2.1433 * y(0) * y(1) + | ||
.21829 * z(0) - 6.125 * z(1); | ||
{ | ||
// Simple `cudaq::observe` test | ||
double energy = cudaq::observe(ansatz{}, h, .59); | ||
printf("Energy is %lf\n", energy); | ||
assert(std::abs(energy + 1.748794) < 1e-3); | ||
} | ||
{ | ||
// Full VQE test with gradients | ||
auto argMapper = [&](std::vector<double> x) { | ||
return std::make_tuple(x[0]); | ||
}; | ||
cudaq::gradients::parameter_shift gradient(ansatz{}, argMapper); | ||
gradient.shiftScalar = 1e-1; | ||
cudaq::optimizers::lbfgs optimizer_lbfgs; | ||
optimizer_lbfgs.max_line_search_trials = 10; | ||
auto [opt_val, opt_params] = optimizer_lbfgs.optimize( | ||
1, [&](const std::vector<double> &x, std::vector<double> &grad_vec) { | ||
double e = cudaq::observe(ansatz{}, h, x[0]); | ||
gradient.compute(x, grad_vec, h, e); | ||
printf("<H>(%lf, %lf) = %lf\n", x[0], x[1], e); | ||
return e; | ||
}); | ||
printf("Optimal value = %.16lf\n", opt_val); | ||
assert(std::abs(opt_val + 1.748794) < 1e-3); | ||
} | ||
return 0; | ||
} |
This file contains 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 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,55 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. * | ||
* All rights reserved. * | ||
* * | ||
* This source code and the accompanying materials are made available under * | ||
* the terms of the Apache License 2.0 which accompanies this distribution. * | ||
******************************************************************************/ | ||
|
||
// REQUIRES: remote-sim | ||
// REQUIRES: c++17 | ||
|
||
// clang-format off | ||
// RUN: nvq++ %cpp_std --target remote-mqpu --remote-mqpu-auto-launch 3 %s -o %t && %t | ||
// RUN: nvq++ %cpp_std --enable-mlir --target remote-mqpu --remote-mqpu-auto-launch 3 %s -o %t && %t | ||
// clang-format on | ||
|
||
#include <cudaq.h> | ||
|
||
struct ansatz { | ||
auto operator()(double theta) __qpu__ { | ||
cudaq::qvector q(2); | ||
x(q[0]); | ||
ry(theta, q[1]); | ||
cx(q[1], q[0]); | ||
} | ||
}; | ||
|
||
int main() { | ||
using namespace cudaq::spin; | ||
cudaq::spin_op h = 5.907 - 2.1433 * x(0) * x(1) - 2.1433 * y(0) * y(1) + | ||
.21829 * z(0) - 6.125 * z(1); | ||
// Observe takes the kernel, the spin_op, and the concrete | ||
// parameters for the kernel | ||
auto energyFuture = cudaq::observe_async(/*qpu_id=*/0, ansatz{}, h, .59); | ||
const double shift = 0.001; | ||
auto plusFuture = | ||
cudaq::observe_async(/*qpu_id=*/1, ansatz{}, h, .59 + shift); | ||
auto minusFuture = | ||
cudaq::observe_async(/*qpu_id=*/2, ansatz{}, h, .59 - shift); | ||
const auto energy = energyFuture.get().expectation(); | ||
const double gradient = | ||
(plusFuture.get().expectation() - minusFuture.get().expectation()) / | ||
(2 * shift); | ||
printf("Energy is %lf\n", energy); | ||
printf("Gradient is %lf\n", gradient); | ||
assert(std::abs(energy + 1.748794) < 1e-3); | ||
// Shots-based observe async. API | ||
cudaq::set_random_seed(13); | ||
auto energyFutureShots = | ||
cudaq::observe_async(/*shots=*/8192, /*qpu_id=*/0, ansatz{}, h, .59); | ||
const auto energyShots = energyFutureShots.get().expectation(); | ||
printf("Energy (shots) is %lf\n", energyShots); | ||
assert(std::abs(energyShots + 1.748794) < 0.1); | ||
return 0; | ||
} |
This file contains 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.
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.
The removal of these lines probably means we need to edit the comments in
runtime/common/JIT.h
, too.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.
Fixed #1533.