-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix QPUD Library Mode Installer Build (#1435)
* Experimenting fixes for lib mode for qpud in the installer * Add %cpp_std to run commands * Use make_copyable_function util * Debug the missing symbol * Revert "Debug the missing symbol" This reverts commit 7e83939. * Try something * Trying a fix * Remove obsolete code comments * Adds strict C++-17 checks * Fixes for C++17 and unit tests * Remove the workaround for cnot gate in C++17 * Debug test issues for installer * Fixed issue with args by ref * Revert "Debug test issues for installer" This reverts commit 5e7bcbf. * Set seed for test repeatability * Fixes after merge In this PR, we've re-enabled C++17 tests for the installer build CI, hence we need to adjust those tests that use C++20 syntax. * Fixed a typo: REQURIES -> REQUIRES * Address code review Move C++17 tests into separate test files.
- Loading branch information
Showing
27 changed files
with
666 additions
and
57 deletions.
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.