-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ryan <3620100+rmshaffer@users.noreply.github.com>
- Loading branch information
Showing
47 changed files
with
1,943 additions
and
7 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
98 changes: 98 additions & 0 deletions
98
runtime/cudaq/platform/default/rest/helpers/braket/BraketExecutor.cpp
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,98 @@ | ||
/******************************************************************************* | ||
* 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. * | ||
******************************************************************************/ | ||
|
||
#include "BraketExecutor.h" | ||
#include "BraketServerHelper.h" | ||
|
||
namespace cudaq { | ||
|
||
details::future | ||
BraketExecutor::execute(std::vector<KernelExecution> &codesToExecute) { | ||
auto braketServerHelper = dynamic_cast<BraketServerHelper *>(serverHelper); | ||
assert(braketServerHelper); | ||
braketServerHelper->setShots(shots); | ||
|
||
auto [dummy1, dummy2, messages] = | ||
braketServerHelper->createJob(codesToExecute); | ||
|
||
cudaq::debug("messages[0] = {}", messages[0].dump()); | ||
|
||
auto const &message = messages[0]; | ||
|
||
CountsDictionary counts; | ||
|
||
std::string const defaultBucket = defaultBucketFuture.get(); | ||
std::string const defaultPrefix = "tasks-cudaq"; | ||
|
||
Aws::Braket::Model::CreateQuantumTaskRequest req; | ||
|
||
auto config = braketServerHelper->getConfig(); | ||
cudaq::info("Backend config: {}, shots {}", config, shots); | ||
config.insert({"shots", std::to_string(shots)}); | ||
|
||
req.SetAction(message["action"]); | ||
req.SetDeviceArn(message["deviceArn"]); | ||
req.SetShots(message["shots"]); | ||
req.SetOutputS3Bucket(defaultBucket); | ||
req.SetOutputS3KeyPrefix(defaultPrefix); | ||
|
||
auto response = braketClient.CreateQuantumTask(req); | ||
|
||
if (response.IsSuccess()) { | ||
std::string taskArn = response.GetResult().GetQuantumTaskArn(); | ||
cudaq::info("Created {}", taskArn); | ||
Aws::Braket::Model::QuantumTaskStatus taskStatus; | ||
Aws::Braket::Model::GetQuantumTaskRequest getTaskReq; | ||
getTaskReq.SetQuantumTaskArn(taskArn); | ||
|
||
auto r = braketClient.GetQuantumTask(getTaskReq); | ||
do { | ||
std::this_thread::sleep_for(std::chrono::milliseconds{100}); | ||
r = braketClient.GetQuantumTask(getTaskReq); | ||
taskStatus = r.GetResult().GetStatus(); | ||
} while (taskStatus != Aws::Braket::Model::QuantumTaskStatus::COMPLETED && | ||
taskStatus != Aws::Braket::Model::QuantumTaskStatus::FAILED && | ||
taskStatus != Aws::Braket::Model::QuantumTaskStatus::CANCELLED); | ||
|
||
std::string outBucket = r.GetResult().GetOutputS3Bucket(); | ||
std::string outPrefix = r.GetResult().GetOutputS3Directory(); | ||
|
||
cudaq::info("results at {}/{}", outBucket, outPrefix); | ||
Aws::S3Crt::Model::GetObjectRequest resultRequest; | ||
resultRequest.SetBucket(outBucket); | ||
resultRequest.SetKey(fmt::format("{}/results.json", outPrefix)); | ||
|
||
auto results = s3Client.GetObject(resultRequest); | ||
|
||
auto parsedResult = | ||
nlohmann::json::parse(results.GetResultWithOwnership().GetBody()); | ||
|
||
auto measurements = parsedResult.at("measurements"); | ||
|
||
for (auto const &m : measurements) { | ||
std::string bitString = ""; | ||
for (int bit : m) { | ||
bitString += std::to_string(bit); | ||
} | ||
counts[bitString] += 1; | ||
} | ||
|
||
} else { | ||
std::cout << "Create error\n" << response.GetError() << "\n"; | ||
} | ||
|
||
ExecutionResult ex_r{counts}; | ||
sample_result result{ex_r}; | ||
|
||
std::promise<sample_result> p; | ||
p.set_value(result); | ||
return {p.get_future()}; | ||
}; | ||
} // namespace cudaq | ||
|
||
CUDAQ_REGISTER_TYPE(cudaq::Executor, cudaq::BraketExecutor, braket); |
106 changes: 106 additions & 0 deletions
106
runtime/cudaq/platform/default/rest/helpers/braket/BraketExecutor.h
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,106 @@ | ||
/******************************************************************************* | ||
* 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. * | ||
******************************************************************************/ | ||
|
||
#pragma once | ||
#include "common/Executor.h" | ||
#include "common/FmtCore.h" | ||
#include "common/MeasureCounts.h" | ||
#include "cudaq.h" | ||
|
||
#include <chrono> | ||
#include <iostream> | ||
|
||
#include <aws/core/Aws.h> | ||
|
||
#include <aws/braket/BraketClient.h> | ||
#include <aws/braket/model/CreateQuantumTaskRequest.h> | ||
#include <aws/braket/model/GetQuantumTaskRequest.h> | ||
#include <aws/braket/model/QuantumTaskStatus.h> | ||
|
||
#include <aws/sts/STSClient.h> | ||
|
||
#include <aws/s3-crt/S3CrtClient.h> | ||
#include <aws/s3-crt/model/GetObjectRequest.h> | ||
|
||
#include <aws/core/utils/logging/AWSLogging.h> | ||
#include <aws/core/utils/logging/ConsoleLogSystem.h> | ||
#include <aws/core/utils/logging/LogLevel.h> | ||
|
||
#include "BraketServerHelper.h" | ||
#include "common/Logger.h" | ||
|
||
#include <nlohmann/json.hpp> | ||
#include <regex> | ||
#include <string> | ||
#include <thread> | ||
|
||
namespace cudaq { | ||
/// @brief The Executor subclass for Amazon Braket | ||
class BraketExecutor : public Executor { | ||
Aws::SDKOptions options; | ||
|
||
std::string region; | ||
std::string accountId; | ||
|
||
class ScopedApi { | ||
Aws::SDKOptions &options; | ||
|
||
public: | ||
ScopedApi(Aws::SDKOptions &options) : options(options) { | ||
Aws::InitAPI(options); | ||
} | ||
~ScopedApi() { Aws::ShutdownAPI(options); } | ||
}; | ||
|
||
ScopedApi api; | ||
Aws::Braket::BraketClient braketClient; | ||
Aws::STS::STSClient stsClient; | ||
Aws::S3Crt::S3CrtClient s3Client; | ||
|
||
std::future<std::string> defaultBucketFuture; | ||
|
||
static auto getClientConfig() { | ||
Aws::Client::ClientConfiguration clientConfig; | ||
clientConfig.verifySSL = false; | ||
return clientConfig; | ||
} | ||
|
||
static auto getS3ClientConfig() { | ||
Aws::S3Crt::ClientConfiguration clientConfig; | ||
clientConfig.verifySSL = false; | ||
return clientConfig; | ||
} | ||
|
||
public: | ||
BraketExecutor() | ||
: api(options), braketClient(getClientConfig()), | ||
stsClient(getClientConfig()), s3Client(getS3ClientConfig()) { | ||
cudaq::debug("Creating BraketExecutor"); | ||
|
||
defaultBucketFuture = std::async(std::launch::async, [&] { | ||
auto response = stsClient.GetCallerIdentity(); | ||
std::string bucketName; | ||
if (response.IsSuccess()) { | ||
bucketName = | ||
fmt::format("amazon-braket-{}-{}", getClientConfig().region, | ||
response.GetResult().GetAccount()); | ||
cudaq::info("Task results will use S3 bucket \"{}\"", bucketName); | ||
return bucketName; | ||
} else { | ||
throw response.GetError(); | ||
} | ||
}); | ||
} | ||
|
||
~BraketExecutor() = default; | ||
|
||
/// @brief Execute the provided Braket task | ||
details::future | ||
execute(std::vector<KernelExecution> &codesToExecute) override; | ||
}; | ||
} // namespace cudaq |
Oops, something went wrong.