-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NVQC] Support direct kernel invocation with a return value (#1969)
* Enable simple return values from remote execution in MLIR/NVQC mode * Don't test return values with 0.7 nvqc service * Update runtime/common/BaseRemoteSimulatorQPU.h Co-authored-by: Eric Schweitz <eschweitz@nvidia.com> * Update runtime/common/ExecutionContext.h Co-authored-by: Eric Schweitz <eschweitz@nvidia.com> * Update runtime/cudaq/platform/default/rest_server/helpers/RestRemoteServer.cpp Co-authored-by: Eric Schweitz <eschweitz@nvidia.com> * Update runtime/cudaq/platform/default/rest_server/helpers/RestRemoteServer.cpp Co-authored-by: Eric Schweitz <eschweitz@nvidia.com> * Add error checks and docs * Code format --------- Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
- Loading branch information
1 parent
fead557
commit 478e00c
Showing
7 changed files
with
202 additions
and
17 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,101 @@ | ||
/******************************************************************************* | ||
* 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++20 | ||
|
||
// clang-format off | ||
// 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 <iostream> | ||
|
||
struct rwpe { | ||
double operator()(const int n_iter, double mu, double sigma) __qpu__ { | ||
int iteration = 0; | ||
|
||
// Allocate the qubits | ||
cudaq::qvector q(2); | ||
|
||
// Alias them | ||
auto &aux = q.front(); | ||
auto &target = q.back(); | ||
|
||
x(q[1]); | ||
|
||
while (iteration < n_iter) { | ||
h(aux); | ||
rz(1.0 - (mu / sigma), aux); | ||
rz(.25 / sigma, target); | ||
x<cudaq::ctrl>(aux, target); | ||
rz(-.25 / sigma, target); | ||
x<cudaq::ctrl>(aux, target); | ||
h(aux); | ||
if (mz(aux)) { | ||
x(aux); | ||
mu += sigma * .6065; | ||
} else { | ||
mu -= sigma * .6065; | ||
} | ||
|
||
sigma *= .7951; | ||
iteration += 1; | ||
} | ||
|
||
return 2. * mu; | ||
} | ||
}; | ||
|
||
struct returnTrue { | ||
bool operator()() __qpu__ { | ||
cudaq::qubit q; | ||
x(q); | ||
return mz(q); | ||
} | ||
}; | ||
|
||
struct returnFalse { | ||
bool operator()() __qpu__ { | ||
cudaq::qubit q, r; | ||
x(q); | ||
return mz(q) && mz(r); | ||
} | ||
}; | ||
|
||
struct returnInt { | ||
int operator()(int iters) __qpu__ { | ||
cudaq::qubit q; | ||
int count = 0; | ||
for (int i = 0; i < iters; ++i) { | ||
h(q); | ||
if (mz(q)) { | ||
count++; | ||
x(q); | ||
} | ||
} | ||
return count; | ||
} | ||
}; | ||
|
||
int main() { | ||
int n_iterations = 24; | ||
double mu = 0.7951, sigma = 0.6065; | ||
auto phase = rwpe{}(n_iterations, mu, sigma); | ||
|
||
assert(std::abs(phase - 0.49) < 0.05); | ||
|
||
assert(returnTrue{}()); | ||
|
||
assert(!returnFalse{}()); | ||
cudaq::set_random_seed(123); | ||
const int oneCount = returnInt{}(1000); | ||
std::cout << "One count = " << oneCount << "\n"; | ||
// We expect ~ 50% one. | ||
assert(oneCount > 100); | ||
} |