CUDA-Q and CMake

CUDA-Q provides a full compiler toolchain for building out quantum-classical applications. Since most C++ projects provide some sort of integration with CMake, it becomes imperative for our toolchain to integrate well with the CMake build system.

CUDA-Q provides CMake configuration files that allow downstream application developers to create hybrid quantum-classical source code and build it with the typical CMake workflow, which will natively leverage nvq++ for compilation of CUDA-Q source files.

To see how this works, let’s create a trivial example. Suppose we have a folder named myCudaQApp and within that folder we have a simple CUDA-Q source file, like ghz.cpp. With a local CUDA-Q installation present on the system, we can create the following top-level CMakeLists.txt file to build this file with nvq++:

cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
project(myCudaQApp LANGUAGES CXX)
find_package(CUDAQ REQUIRED)
add_executable(ghz_exe ghz.cpp)

To configure and build this file, all we need to do is execute the following from the top level of the myCudaQApp folder:

mkdir build && cd build
cmake .. -G Ninja -DCUDAQ_DIR="$CUDA_QUANTUM_PATH/lib/cmake/cudaq"
ninja
./ghz_exe

Notice that by running the ninja command with --verbose, you’ll see that nvq++ was used to compile and link the executable.