From 92d2d6fb9fbca5b12d304c3ec8856059699db731 Mon Sep 17 00:00:00 2001 From: Pradnya Khalate Date: Tue, 7 May 2024 09:53:12 -0700 Subject: [PATCH] * Python bindings for ORCA backend - Expose the Orca-specific APIs to Python front-end - Added example to showcase usage - Documentation to be added ([Documentation TODO]) --- docs/sphinx/examples/cpp/providers/orca.cpp | 6 ++--- docs/sphinx/examples/python/providers/orca.py | 25 +++++++++++++++++++ python/CMakeLists.txt | 1 + python/CUDAQuantumExtension.cpp | 9 +++++-- python/cudaq/__init__.py | 3 +++ runtime/cudaq/platform/orca/OrcaQPU.cpp | 18 +++++++++++++ runtime/cudaq/platform/orca/orca_qpu.h | 12 +-------- 7 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 docs/sphinx/examples/python/providers/orca.py diff --git a/docs/sphinx/examples/cpp/providers/orca.cpp b/docs/sphinx/examples/cpp/providers/orca.cpp index 498d8a68b9..b074c0d77a 100644 --- a/docs/sphinx/examples/cpp/providers/orca.cpp +++ b/docs/sphinx/examples/cpp/providers/orca.cpp @@ -1,7 +1,7 @@ // Compile and run with: // ``` -// nvq++ --target orca --orca-url http://localhost:8080/sample -// orca.cpp -o out.x && ./out.x +// nvq++ --target orca --orca-url http://localhost:8080/sample orca.cpp -o out.x +// && ./out.x // ``` #include "cudaq/orca.h" @@ -28,4 +28,4 @@ int main() { counts.dump(); return 0; -} +} \ No newline at end of file diff --git a/docs/sphinx/examples/python/providers/orca.py b/docs/sphinx/examples/python/providers/orca.py new file mode 100644 index 0000000000..76fe1f8de0 --- /dev/null +++ b/docs/sphinx/examples/python/providers/orca.py @@ -0,0 +1,25 @@ +# ============================================================================ # +# 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. # +# ============================================================================ # + +import cudaq +import numpy as np + +cudaq.set_target("orca", url="http://localhost:8080/sample") + +# [Documentation TODO]: Explanation of the following terms and APIs + +bs_angles = [np.pi / 3, np.pi / 6] +ps_angles = [np.pi / 4, np.pi / 5] + +input_state = [1, 1, 1] +loop_lengths = [1] + +counts = cudaq.orca.sample(bs_angles, ps_angles, input_state, loop_lengths, + 10000) + +print(counts) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 70157b66aa..07366384ed 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -84,6 +84,7 @@ declare_mlir_python_extension(CUDAQuantumPythonSources.Extension utils/LinkedLibraryHolder.cpp ../runtime/cudaq/platform/common/QuantumExecutionQueue.cpp ../runtime/cudaq/platform/default/rest_server/RemoteRuntimeClient.cpp + ../runtime/cudaq/platform/orca/OrcaQPU.cpp EMBED_CAPI_LINK_LIBS CUDAQuantumMLIRCAPI diff --git a/python/CUDAQuantumExtension.cpp b/python/CUDAQuantumExtension.cpp index ade30cb6dd..93a49598e9 100644 --- a/python/CUDAQuantumExtension.cpp +++ b/python/CUDAQuantumExtension.cpp @@ -13,6 +13,7 @@ #include #include +#include "cudaq/Optimizer/CAPI/Dialects.h" #include "runtime/common/py_ExecutionContext.h" #include "runtime/common/py_NoiseModel.h" #include "runtime/common/py_ObserveResult.h" @@ -33,10 +34,11 @@ #include "runtime/mlir/py_register_dialects.h" #include "utils/LinkedLibraryHolder.h" #include "utils/OpaqueArguments.h" -#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" -#include "cudaq/Optimizer/CAPI/Dialects.h" +#include "../runtime/cudaq/platform/orca/orca_qpu.h" + #include "mlir/Bindings/Python/PybindAdaptors.h" +#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" namespace py = pybind11; @@ -161,6 +163,9 @@ PYBIND11_MODULE(_quakeDialects, m) { mpiSubmodule.def( "finalize", []() { cudaq::mpi::finalize(); }, "Finalize MPI."); + auto orcaSubmodule = cudaqRuntime.def_submodule("orca"); + orcaSubmodule.def("sample", &cudaq::orca::sample, "[Documentation TODO]"); + cudaqRuntime.def("cloneModule", [](MlirModule mod) { return wrap(unwrap(mod).clone()); }); cudaqRuntime.def("isTerminator", [](MlirOperation op) { diff --git a/python/cudaq/__init__.py b/python/cudaq/__init__.py index 8c6bf15547..d2af1495aa 100644 --- a/python/cudaq/__init__.py +++ b/python/cudaq/__init__.py @@ -104,6 +104,9 @@ to_qir = cudaq_runtime.get_qir testing = cudaq_runtime.testing +# target-specific +orca = cudaq_runtime.orca + def synthesize(kernel, *args): # Compile if necessary, no-op if already compiled diff --git a/runtime/cudaq/platform/orca/OrcaQPU.cpp b/runtime/cudaq/platform/orca/OrcaQPU.cpp index cbc6f67597..b344fd8d85 100644 --- a/runtime/cudaq/platform/orca/OrcaQPU.cpp +++ b/runtime/cudaq/platform/orca/OrcaQPU.cpp @@ -31,6 +31,24 @@ #include #include +namespace cudaq::orca { +cudaq::sample_result sample(std::vector &bs_angles, + std::vector &ps_angles, + std::vector &input_state, + std::vector &loop_lengths, + int n_samples) { + TBIParameters parameters{bs_angles, ps_angles, input_state, loop_lengths, + n_samples}; + cudaq::ExecutionContext context("sample", n_samples); + auto &platform = get_platform(); + platform.set_exec_ctx(&context, 0); + cudaq::altLaunchKernel("orca_launch", nullptr, ¶meters, + sizeof(TBIParameters), 0); + + return context.result; +} +} // namespace cudaq::orca + namespace { /// @brief The OrcaRemoteRESTQPU is a subtype of QPU that enables the diff --git a/runtime/cudaq/platform/orca/orca_qpu.h b/runtime/cudaq/platform/orca/orca_qpu.h index 4f0fcf84a0..95d4b506d4 100644 --- a/runtime/cudaq/platform/orca/orca_qpu.h +++ b/runtime/cudaq/platform/orca/orca_qpu.h @@ -30,16 +30,6 @@ cudaq::sample_result sample(std::vector &bs_angles, std::vector &ps_angles, std::vector &input_state, std::vector &loop_lengths, - int n_samples = 1000000) { - TBIParameters parameters{bs_angles, ps_angles, input_state, loop_lengths, - n_samples}; - cudaq::ExecutionContext context("sample", n_samples); - auto &platform = get_platform(); - platform.set_exec_ctx(&context, 0); - cudaq::altLaunchKernel("orca_launch", nullptr, ¶meters, - sizeof(TBIParameters), 0); - - return context.result; -} + int n_samples = 1000000); }; // namespace cudaq::orca \ No newline at end of file