Skip to content
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

NVQC Optimizations for VQE (C++ and Python) #1901

Merged
merged 70 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
f70e94c
launchVQE stubs
bmhowe23 Jun 5, 2024
1c57ec9
Continue plumbing - first exec done (but as observe, not VQE)
bmhowe23 Jun 5, 2024
93f7737
Add optimizer serialization (untested)
bmhowe23 Jun 6, 2024
b820966
Add a few from_json methods
bmhowe23 Jun 6, 2024
c426348
Tweaks; parses on server now
bmhowe23 Jun 6, 2024
32a2d8d
Runs VQE on remote server (but no answers sent back yet)
bmhowe23 Jun 6, 2024
a51208c
Add cudaq::optimization_result to ExecutionContext to save results
bmhowe23 Jun 6, 2024
65324b4
Finish up answer plumbing
bmhowe23 Jun 6, 2024
9245c8e
Fix a FIXME
bmhowe23 Jun 6, 2024
b2808a1
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jun 6, 2024
66ac7ac
Remove comments to make spellchecker happy
bmhowe23 Jun 6, 2024
7ba14d4
Change isRemote() to false for remote simulator to make tests happy […
bmhowe23 Jun 6, 2024
9b39c6d
Comment out the new isRemote() function
bmhowe23 Jun 6, 2024
78ca1a5
Create supports_remote_vqe/supportsRemoteVQE instead of using is_remote
bmhowe23 Jun 7, 2024
343df8a
Add startingArgIdx to synthesizer and update VQE to use it with tuples
bmhowe23 Jun 7, 2024
b514c34
Use serializeArgs instead of old kludgy way
bmhowe23 Jun 7, 2024
eb68406
Update to variadic template arguments
bmhowe23 Jun 7, 2024
0e67fe2
Add initial gradient ser/deser
bmhowe23 Jun 8, 2024
717383e
Update gradient code to allow alternative to argMapper.
bmhowe23 Jun 8, 2024
8de6217
Fix simulator handling for VQE on server
bmhowe23 Jun 9, 2024
92773c4
Implement the rest of gradients (seems to be working now)
bmhowe23 Jun 9, 2024
2eb3a90
Misc cleanup / comments
bmhowe23 Jun 10, 2024
6c5bacd
Continued cleanup; reference --> pointer
bmhowe23 Jun 10, 2024
fc7f0c2
Add ability to clone gradients. Update vqe.h accordingly.
bmhowe23 Jun 10, 2024
b713834
Add const qualifiers
bmhowe23 Jun 10, 2024
5203e2c
Clean up JSON structure with optional fields
bmhowe23 Jun 10, 2024
99250c2
Cleanup and revert unnecessary test change
bmhowe23 Jun 10, 2024
d5872cf
Stash test change
bmhowe23 Jun 10, 2024
c9af65e
Merge branch 'main' into pr-cpp-vqe4nvqc (checked build, not test)
bmhowe23 Jun 26, 2024
cb29f7e
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 1, 2024
ec4b50d
Fix merge
bmhowe23 Jul 1, 2024
61bacc4
clang-format
bmhowe23 Jul 1, 2024
15fa38a
Fix library mode remote-sim tests
bmhowe23 Jul 1, 2024
a6b7b51
Fix test_remote_platform.py failures for state overlap
bmhowe23 Jul 1, 2024
00d68b7
Allow Python cudaq.vqe() to invoke new remote VQE code
bmhowe23 Jul 2, 2024
71578ae
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 2, 2024
de89f1b
Fix nvcc compilation issue and get_state_tester.cu test
bmhowe23 Jul 2, 2024
645ed22
Handle the case when there is no argMapper
bmhowe23 Jul 2, 2024
fc4c9a0
Fix some C++ tests by undoing gradient changes that aren't needed
bmhowe23 Jul 2, 2024
e881340
Revert temporary vqe_h2.cpp changes
bmhowe23 Jul 3, 2024
e4ceaac
Revert "Revert temporary vqe_h2.cpp changes"
bmhowe23 Jul 3, 2024
c6ecddf
Revert "Fix some C++ tests by undoing gradient changes that aren't ne…
bmhowe23 Jul 3, 2024
bd3de82
Let's try this again - alternate fix for C++ tests
bmhowe23 Jul 3, 2024
3879346
Fix docs issue
bmhowe23 Jul 3, 2024
3e16756
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 3, 2024
45437db
Refine C++ changes to limit changes from original baseline
bmhowe23 Jul 3, 2024
e15974f
Guard NVCF feature since it is not released yet
bmhowe23 Jul 3, 2024
0aedecf
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 3, 2024
1f77862
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 5, 2024
c4e1d79
Add some C++ tests
bmhowe23 Jul 5, 2024
fccb5a6
Fix bug in some VQE code paths for C++
bmhowe23 Jul 5, 2024
757f2f2
Format
bmhowe23 Jul 5, 2024
540d28c
Update maxcut example
bmhowe23 Jul 5, 2024
307cd21
Add comments for new parameters
bmhowe23 Jul 5, 2024
ae3122b
VQE sendRequest updates for NVQC
bmhowe23 Jul 5, 2024
129b56a
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 5, 2024
03a6878
Fix C++17 test failures
bmhowe23 Jul 7, 2024
8e5ffc1
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 7, 2024
e18aea0
Check if kernel args are compatible with optimal execution
bmhowe23 Jul 8, 2024
4d2b03f
Fixup - Fix C++17 test failures
bmhowe23 Jul 8, 2024
3b33e8a
Another C++17 workaround
bmhowe23 Jul 8, 2024
a7cbaf2
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 8, 2024
0ce2587
Address PR comments
bmhowe23 Jul 9, 2024
beb48eb
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 10, 2024
b9ac638
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 12, 2024
7a3c6df
Move remote_vqe helper function into internal namespace
bmhowe23 Jul 12, 2024
bc2c39f
Move duplicated warning message into helper function
bmhowe23 Jul 12, 2024
253f431
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 12, 2024
336c908
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 15, 2024
76e29a7
Merge branch 'main' into pr-cpp-vqe4nvqc
bmhowe23 Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Fix some C++ tests by undoing gradient changes that aren't ne…
…eded"

This reverts commit fc4c9a0.
  • Loading branch information
bmhowe23 committed Jul 3, 2024
commit c6ecddfdb4880b66125bbfc52a6cd6aa05d391ee
9 changes: 9 additions & 0 deletions runtime/cudaq/algorithms/gradient.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class gradient {
return cudaq::observe(ansatz_functor, h, x);
}

/// Copy constructor. Derived classes should implement the clone() method.
gradient(const gradient &o) {
ansatz_functor = o.ansatz_functor;
serializedArgs = o.serializedArgs;
}

public:
/// Constructor, takes the quantum kernel with prescribed signature
gradient(std::function<void(std::vector<double>)> &&kernel)
Expand Down Expand Up @@ -141,6 +147,9 @@ class gradient {
const std::function<double(std::vector<double>)> &func,
double funcAtX) = 0;

/// Clone the object. Must be implemented by derived classes.
virtual std::unique_ptr<cudaq::gradient> clone() = 0;

virtual ~gradient() = default;
};
} // namespace cudaq
15 changes: 14 additions & 1 deletion runtime/cudaq/algorithms/gradients/central_difference.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,34 @@ namespace cudaq::gradients {
class central_difference : public gradient {
public:
using gradient::gradient;
double step = 1e-4;
static constexpr double default_step = 1e-4;
double step = default_step;

central_difference(double s = default_step) : gradient(), step(s) {}

virtual std::unique_ptr<cudaq::gradient> clone() override {
return std::make_unique<central_difference>(step);
}

void compute(const std::vector<double> &x, std::vector<double> &dx,
const spin_op &h, double exp_h) override {
auto tmpX = x;
for (std::size_t i = 0; i < x.size(); i++) {
// increase value to x_i + dx_i
tmpX[i] += step;
// auto savepx = tmpX[i];
auto px = getExpectedValue(tmpX, h);
// decrease the value to x_i - dx_i
tmpX[i] -= 2 * step;
// auto savemx = tmpX[i];
auto mx = getExpectedValue(tmpX, h);
// return value back to x_i
tmpX[i] += step;
dx[i] = (px - mx) / (2. * step);
// printf("compute: tmp[%lu]=%.16f dx[%lu]=%.16f step=%.16f px=%.16f
// mx=%.16f "
// "savepx=%.16f savemx=%.16f\n",
// i, tmpX[i], i, dx[i], step, px, mx, savepx, savemx);
}
}

Expand Down
9 changes: 8 additions & 1 deletion runtime/cudaq/algorithms/gradients/forward_difference.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ namespace cudaq::gradients {
class forward_difference : public gradient {
public:
using gradient::gradient;
double step = 1e-4;
static constexpr double default_step = 1e-4;
double step = default_step;

forward_difference(double s = default_step) : gradient(), step(s) {}

virtual std::unique_ptr<cudaq::gradient> clone() override {
return std::make_unique<forward_difference>(step);
}

/// @brief Compute the `forward_difference` gradient
void compute(const std::vector<double> &x, std::vector<double> &dx,
Expand Down
10 changes: 9 additions & 1 deletion runtime/cudaq/algorithms/gradients/parameter_shift.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ namespace cudaq::gradients {
class parameter_shift : public gradient {
public:
using gradient::gradient;
double shiftScalar = 0.5;
static constexpr double default_shiftScalar = 0.5;
double shiftScalar = default_shiftScalar;

parameter_shift(double s = default_shiftScalar)
: gradient(), shiftScalar(s) {}

virtual std::unique_ptr<cudaq::gradient> clone() override {
return std::make_unique<parameter_shift>(shiftScalar);
}

void compute(const std::vector<double> &x, std::vector<double> &dx,
const spin_op &h, double exp_h) override {
Expand Down
8 changes: 7 additions & 1 deletion runtime/cudaq/algorithms/vqe.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,17 @@ optimization_result vqe(QuantumKernel &&kernel, cudaq::gradient &gradient,
}

auto requires_grad = optimizer.requiresGradients();
std::unique_ptr<cudaq::gradient> newGrad;
if (requires_grad) {
newGrad = gradient.clone();
if constexpr (sizeof...(args) > 0)
newGrad->setArgs(kernel, args...);
}
return optimizer.optimize(n_params, [&](const std::vector<double> &x,
std::vector<double> &grad_vec) {
double e = cudaq::observe(kernel, H, x, args...);
if (requires_grad) {
gradient.compute(x, grad_vec, H, e);
newGrad->compute(x, grad_vec, H, e);
}
return e;
});
Expand Down