Skip to content

Commit

Permalink
[docs] [testing] Follow-up for braket target and minor clean-up (#2448
Browse files Browse the repository at this point in the history
)

* Update docs to show the `braket` examples
* Remove the unsupported tests (tracked in issue#2325)
* Emacs compatible banner
* Update the C++ tests to reflect appropriate reason for skipping.
(Note that `observe` API is not supported)

---------

Signed-off-by: Pradnya Khalate <[email protected]>
  • Loading branch information
khalatepradnya authored Dec 5, 2024
1 parent 5ccbcd2 commit 9d7e20a
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 105 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ if (CUDAQ_ENABLE_REST AND NOT DEFINED CUDAQ_ENABLE_REMOTE_SIM)
endif()
endif()

# Enable AWS backends by default.
# Enable Amazon Braket backends by default.
if (NOT DEFINED CUDAQ_ENABLE_BRAKET_BACKEND)
set(CUDAQ_ENABLE_BRAKET_BACKEND ON CACHE BOOL "Enable building AWS backends.")
set(CUDAQ_ENABLE_BRAKET_BACKEND ON CACHE BOOL "Enable building AWS SDK for Amazon Braket backends.")
endif()

# Generate a CompilationDatabase (compile_commands.json file) for our build,
Expand Down
22 changes: 19 additions & 3 deletions docs/sphinx/using/examples/hardware_providers.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
Using Quantum Hardware Providers
-----------------------------------

CUDA-Q contains support for using a set of hardware providers (IonQ, IQM, OQC, ORCA Computing and Quantinuum).
For more information about executing quantum kernels on different hardware backends, please take a look
at :doc:`hardware <../backends/hardware>`.
CUDA-Q contains support for using a set of hardware providers (Amazon Braket,
IonQ, IQM, OQC, ORCA Computing, Quantinuum and QuEra Computing).
For more information about executing quantum kernels on different hardware
backends, please take a look at :doc:`hardware <../backends/hardware>`.

Amazon Braket
==================================

The following code illustrates how to run kernels on Amazon Braket's backends.

.. tab:: Python

.. literalinclude:: ../../targets/python/braket.py
:language: python

.. tab:: C++

.. literalinclude:: ../../targets/cpp/braket.cpp
:language: cpp

IonQ
==================================
Expand Down
23 changes: 9 additions & 14 deletions runtime/common/BraketExecutor.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*******************************************************************************
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
Expand All @@ -7,27 +7,22 @@
******************************************************************************/

#pragma once

#include "common/BraketServerHelper.h"
#include "common/Executor.h"
#include "common/FmtCore.h"
#include "common/Logger.h"
#include "common/MeasureCounts.h"
#include "cudaq.h"

#include <chrono>
#include <iostream>

#include <aws/core/Aws.h>

#include <aws/braket/BraketClient.h>
#include <aws/s3-crt/S3CrtClient.h>
#include <aws/sts/STSClient.h>

#include <aws/core/Aws.h>
#include <aws/core/utils/logging/AWSLogging.h>
#include <aws/core/utils/logging/ConsoleLogSystem.h>
#include <aws/core/utils/logging/LogLevel.h>

#include "common/BraketServerHelper.h"
#include "common/Logger.h"

#include <aws/s3-crt/S3CrtClient.h>
#include <aws/sts/STSClient.h>
#include <chrono>
#include <iostream>
#include <nlohmann/json.hpp>
#include <regex>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/BraketServerHelper.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*******************************************************************************
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
Expand Down
1 change: 0 additions & 1 deletion targettests/braket/sudoku_2x2-1.cpp

This file was deleted.

1 change: 0 additions & 1 deletion targettests/braket/sudoku_2x2-bit_names.cpp

This file was deleted.

1 change: 0 additions & 1 deletion targettests/braket/sudoku_2x2-reg_name.cpp

This file was deleted.

1 change: 0 additions & 1 deletion targettests/braket/sudoku_2x2.cpp

This file was deleted.

87 changes: 6 additions & 81 deletions unittests/backends/braket/BraketTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ bool isValidExpVal(double value) {
}

CUDAQ_TEST(BraketTester, checkSampleSync) {
GTEST_SKIP() << "Fails with: Please make sure all qubits in the qubit "
"register are used for tasks submitted to simulators";
GTEST_SKIP() << "Amazon Braket credentials required";
std::string home = std::getenv("HOME");
std::string fileName = home + "/FakeCppBraket.config";
auto backendString =
Expand Down Expand Up @@ -68,8 +67,7 @@ CUDAQ_TEST(BraketTester, checkSampleSyncEmulate) {
}

CUDAQ_TEST(BraketTester, checkSampleAsync) {
GTEST_SKIP() << "Fails with: Please make sure all qubits in the qubit "
"register are used for tasks submitted to simulators";
GTEST_SKIP() << "Amazon Braket credentials required";
std::string home = std::getenv("HOME");
std::string fileName = home + "/FakeCppBraket.config";
auto backendString = fmt::format(fmt::runtime(backendStringTemplate),
Expand Down Expand Up @@ -111,8 +109,8 @@ CUDAQ_TEST(BraketTester, checkSampleAsyncEmulate) {
}

CUDAQ_TEST(BraketTester, checkSampleAsyncLoadFromFile) {
GTEST_SKIP() << "Fails with: Please make sure all qubits in the qubit "
"register are used for tasks submitted to simulators";
GTEST_SKIP() << "Fails with: Cannot persist a cudaq::future for a local "
"kernel execution.";
std::string home = std::getenv("HOME");
std::string fileName = home + "/FakeCppBraket.config";
auto backendString = fmt::format(fmt::runtime(backendStringTemplate),
Expand Down Expand Up @@ -148,9 +146,7 @@ CUDAQ_TEST(BraketTester, checkSampleAsyncLoadFromFile) {
}

CUDAQ_TEST(BraketTester, checkObserveSync) {
GTEST_SKIP() << "Fails with: Device requires all qubits in the program to be "
"measured. This may be caused by declaring non-contiguous "
"qubits or measuring partial qubits";
GTEST_SKIP() << "Fails with: Cannot observe kernel with measures in it";
std::string home = std::getenv("HOME");
std::string fileName = home + "/FakeCppBraket.config";
auto backendString = fmt::format(fmt::runtime(backendStringTemplate),
Expand All @@ -164,6 +160,7 @@ CUDAQ_TEST(BraketTester, checkObserveSync) {
kernel.x(qubit[0]);
kernel.ry(theta, qubit[1]);
kernel.x<cudaq::ctrl>(qubit[1], qubit[0]);
kernel.mz(qubit);

using namespace cudaq::spin;
cudaq::spin_op h = 5.907 - 2.1433 * x(0) * x(1) - 2.1433 * y(0) * y(1) +
Expand Down Expand Up @@ -232,78 +229,6 @@ CUDAQ_TEST(BraketTester, checkObserveAsync) {
EXPECT_TRUE(isValidExpVal(result.expectation()));
}

CUDAQ_TEST(BraketTester, checkObserveAsyncEmulate) {
std::string home = std::getenv("HOME");
std::string fileName = home + "/FakeCppBraket.config";
auto backendString = fmt::format(fmt::runtime(backendStringTemplate),
mockPort, fileName, machine);
backendString =
std::regex_replace(backendString, std::regex("false"), "true");

auto &platform = cudaq::get_platform();
platform.setTargetBackend(backendString);

auto [kernel, theta] = cudaq::make_kernel<double>();
auto qubit = kernel.qalloc(2);
kernel.x(qubit[0]);
kernel.ry(theta, qubit[1]);
kernel.x<cudaq::ctrl>(qubit[1], qubit[0]);

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);
auto future = cudaq::observe_async(100000, 0, kernel, h, .59);

auto result = future.get();
result.dump();

printf("ENERGY: %lf\n", result.expectation());
EXPECT_TRUE(isValidExpVal(result.expectation()));
}

CUDAQ_TEST(BraketTester, checkObserveAsyncLoadFromFile) {
GTEST_SKIP() << "Fails with: Device requires all qubits in the program to be "
"measured. This may be caused by declaring non-contiguous "
"qubits or measuring partial qubits";
std::string home = std::getenv("HOME");
std::string fileName = home + "/FakeCppBraket.config";
auto backendString = fmt::format(fmt::runtime(backendStringTemplate),
mockPort, fileName, machine);

auto &platform = cudaq::get_platform();
platform.setTargetBackend(backendString);

auto [kernel, theta] = cudaq::make_kernel<double>();
auto qubit = kernel.qalloc(2);
kernel.x(qubit[0]);
kernel.ry(theta, qubit[1]);
kernel.x<cudaq::ctrl>(qubit[1], qubit[0]);

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);
auto future = cudaq::observe_async(kernel, h, .59);

{
std::ofstream out("saveMeObserve.json");
out << future;
}

// Later you can come back and read it in
cudaq::async_result<cudaq::observe_result> readIn(&h);
std::ifstream in("saveMeObserve.json");
in >> readIn;

// Get the results of the read in future.
auto result = readIn.get();

std::remove("saveMeObserve.json");
result.dump();

printf("ENERGY: %lf\n", result.expectation());
EXPECT_TRUE(isValidExpVal(result.expectation()));
}

int main(int argc, char **argv) {
std::string home = std::getenv("HOME");
std::string fileName = home + "/FakeCppBraket.config";
Expand Down

0 comments on commit 9d7e20a

Please sign in to comment.