Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to pass custom passes from nvq++ #1034

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions test/NVQPP/custom_pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2022 - 2023 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. *
******************************************************************************/
// clang-format off
// RUN: nvq++ --enable-mlir --opt-plugin %cudaq_lib_dir/CustomPassPlugin.so --opt-pass 'func.func(cudaq-custom-pass)' %s -o %t && %t | FileCheck %s
1tnguyen marked this conversation as resolved.
Show resolved Hide resolved
// RUN: nvq++ -std=c++17 --enable-mlir --opt-plugin %cudaq_lib_dir/CustomPassPlugin.so --opt-pass 'func.func(cudaq-custom-pass)' %s -o %t && %t | FileCheck %s
// clang-format on

#include <cudaq.h>
#include <iostream>

void kernel() __qpu__ {
cudaq::qarray<2> q;
h(q[0]);
x<cudaq::ctrl>(q[0], q[1]);
mz(q);
}

int main() {
auto result = cudaq::sample(1000, kernel);
for (auto &&[bits, counts] : result) {
std::cout << bits << '\n';
}
return 0;
}

// The custom pass replace H with S, hence not a Bell state anymore.
// CHECK: 00
22 changes: 21 additions & 1 deletion tools/nvqpp/nvq++.in
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ function show_help {
--[no-]lambda-lifting
Enable/disable lambda lifting pass.

--opt-plugin <dynamic library file>
Load pass plugin by specifying its dynamic library.

--opt-pass <pass name>
Append a pass to the pass pipeline by specifying its name in the pass pipeline syntax, e.g. <dialect>.<opname>(passname).

khalatepradnya marked this conversation as resolved.
Show resolved Hide resolved
--save-temps
Save temporary files.

Expand Down Expand Up @@ -269,6 +275,8 @@ LIST_TARGETS=false
DISABLE_QUBIT_MAPPING=false
NVQIR_LIBS="-lnvqir -lnvqir-"
CPPSTD=-std=c++20
CUDAQ_OPT_PLUGIN_ARGS=
CUDAQ_OPT_EXTRA_PASSES=

# Provide a default backend, user can override
NVQIR_SIMULATION_BACKEND="qpp"
Expand Down Expand Up @@ -409,6 +417,14 @@ while [ $# -ne 0 ]; do
--lambda-lifting)
ENABLE_LAMBDA_LIFTING=true
;;
--opt-plugin)
CUDAQ_OPT_PLUGIN_ARGS="${CUDAQ_OPT_PLUGIN_ARGS} --load-cudaq-plugin $2"
shift
;;
--opt-pass)
CUDAQ_OPT_EXTRA_PASSES=$(add_pass_to_pipeline "${CUDAQ_OPT_EXTRA_PASSES}" "$2")
shift
;;
--save-temps)
DELETE_TEMPS=false
;;
Expand Down Expand Up @@ -589,6 +605,10 @@ if ${RUN_OPT}; then
OPT_PASSES=$(add_pass_to_pipeline "${OPT_PASSES}" "canonicalize,cse")
fi

if [ ! -z "$CUDAQ_OPT_EXTRA_PASSES" ]; then
OPT_PASSES=$(add_pass_to_pipeline "${OPT_PASSES}" "$CUDAQ_OPT_EXTRA_PASSES")
fi

OPT_PASSES="builtin.module(${OPT_PASSES})"

if ${SHOW_VERSION} && [ -z "$SRCS" ] && [ -z "$OBJS" ]; then
Expand Down Expand Up @@ -622,7 +642,7 @@ for i in ${SRCS}; do
if ${RUN_OPT}; then
DCL_FILE=$(mktemp ${file}.qke.XXXXXX)
TMPFILES="${TMPFILES} ${DCL_FILE} ${DCL_FILE}.o"
run ${TOOLBIN}cudaq-opt --pass-pipeline="${OPT_PASSES}" ${QUAKE_IN} -o ${DCL_FILE}
run ${TOOLBIN}cudaq-opt ${CUDAQ_OPT_PLUGIN_ARGS} --pass-pipeline="${OPT_PASSES}" ${QUAKE_IN} -o ${DCL_FILE}
QUAKE_IN=${DCL_FILE}
fi
QUAKELL_FILE=$(mktemp ${file}.ll.XXXXXX)
Expand Down
Loading