Skip to content

Commit 2a6382c

Browse files
authored
Test examples in workflow (#112)
Test the examples whenever the library itself (either solvers or qec) is updated, or the examples are updated. The expectation is that the examples should run. However the correctness of the examples' results are not checked in this step. --------- Signed-off-by: Melody Ren <[email protected]>
1 parent e77c964 commit 2a6382c

File tree

8 files changed

+143
-6
lines changed

8 files changed

+143
-6
lines changed

.github/actions/build-lib/build_all.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ cmake -S . -B "$1" \
99
-DCUDAQ_DIR=/cudaq-install/lib/cmake/cudaq/ \
1010
-DCUDAQX_ENABLE_LIBS="all" \
1111
-DCUDAQX_INCLUDE_TESTS=ON \
12-
-DCUDAQX_BINDINGS_PYTHON=ON
12+
-DCUDAQX_BINDINGS_PYTHON=ON \
13+
-DCMAKE_INSTALL_PREFIX=$HOME/.cudaqx
1314

1415
cmake --build "$1" --target install
1516

.github/actions/build-lib/build_qec.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ cmake -S libs/qec -B "$1" \
88
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
99
-DCUDAQ_DIR=/cudaq-install/lib/cmake/cudaq/ \
1010
-DCUDAQX_INCLUDE_TESTS=ON \
11-
-DCUDAQX_BINDINGS_PYTHON=ON
11+
-DCUDAQX_BINDINGS_PYTHON=ON \
12+
-DCMAKE_INSTALL_PREFIX=$HOME/.cudaqx
1213

1314
cmake --build "$1" --target install
1415

.github/actions/build-lib/build_solvers.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ cmake -S libs/solvers -B "$1" \
88
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
99
-DCUDAQ_DIR=/cudaq-install/lib/cmake/cudaq/ \
1010
-DCUDAQX_INCLUDE_TESTS=ON \
11-
-DCUDAQX_BINDINGS_PYTHON=ON
11+
-DCUDAQX_BINDINGS_PYTHON=ON \
12+
-DCMAKE_INSTALL_PREFIX=$HOME/.cudaqx
1213

1314
cmake --build "$1" --target install
1415

.github/workflows/all_libs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,10 @@ jobs:
8585
- name: Run Python tests
8686
run: cmake --build ${{ steps.build.outputs.build-dir }} --target run_python_tests
8787

88+
# ========================================================================
89+
# Run example tests
90+
# ========================================================================
91+
92+
- name: Run example tests
93+
run: bash scripts/ci/test_examples.sh all
94+

.github/workflows/lib_qec.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
# ========================================================================
6868
# Run tests
6969
# ========================================================================
70-
#
70+
7171
- name: Run tests
7272
run: cmake --build ${{ steps.build.outputs.build-dir }} --target run_tests
7373

@@ -81,3 +81,10 @@ jobs:
8181
- name: Run Python tests
8282
run: cmake --build ${{ steps.build.outputs.build-dir }} --target run_python_tests
8383

84+
# ========================================================================
85+
# Run example tests
86+
# ========================================================================
87+
88+
- name: Run example tests
89+
run: bash scripts/ci/test_examples.sh qec
90+

.github/workflows/lib_solvers.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ jobs:
8585
- name: Run Python tests
8686
run: cmake --build ${{ steps.build.outputs.build-dir }} --target run_python_tests
8787

88+
# ========================================================================
89+
# Run example tests
90+
# ========================================================================
91+
92+
- name: Run example tests
93+
run: bash scripts/ci/test_examples.sh solvers

.github/workflows/pr_workflow.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ jobs:
8080
- 'libs/solvers/**/*.in'
8181
- 'libs/solvers/**/*.py'
8282
- 'libs/solvers/**/CMakeLists.txt'
83+
build-examples-qec:
84+
- 'docs/sphinx/examples/qec/**'
85+
build-examples-solvers:
86+
- 'docs/sphinx/examples/solvers/**'
8387
8488
build-cudaq:
8589
name: Build CUDAQ
@@ -143,15 +147,23 @@ jobs:
143147
needs: [check-changes, build-cudaq]
144148
if: |
145149
!failure() && !cancelled() &&
146-
(needs.check-changes.outputs.build-qec == 'true' || needs.check-changes.outputs.build-cudaq == 'true')
150+
(
151+
needs.check-changes.outputs.build-qec == 'true' ||
152+
needs.check-changes.outputs.build-cudaq == 'true' ||
153+
needs.check-changes.outputs.build-examples-qec == 'true'
154+
)
147155
uses: ./.github/workflows/lib_qec.yaml
148156

149157
build-solvers:
150158
name: Solvers
151159
needs: [check-changes, build-cudaq]
152160
if: |
153161
!failure() && !cancelled() &&
154-
(needs.check-changes.outputs.build-solvers == 'true' || needs.check-changes.outputs.build-cudaq == 'true')
162+
(
163+
needs.check-changes.outputs.build-solvers == 'true' ||
164+
needs.check-changes.outputs.build-cudaq == 'true' ||
165+
needs.check-changes.outputs.build-examples-solvers == 'true'
166+
)
155167
uses: ./.github/workflows/lib_solvers.yaml
156168

157169
# This job is used for branch protection checks.

scripts/ci/test_examples.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/sh
2+
3+
# ============================================================================ #
4+
# Copyright (c) 2025 NVIDIA Corporation & Affiliates. #
5+
# All rights reserved. #
6+
# #
7+
# This source code and the accompanying materials are made available under #
8+
# the terms of the Apache License 2.0 which accompanies this distribution. #
9+
# ============================================================================ #
10+
11+
# Set PATH
12+
export PATH="/cudaq-install/bin:$PATH"
13+
export PYTHONPATH="/cudaq-install:$HOME/.cudaqx"
14+
echo "Setting PYTHONPATH=$PYTHONPATH"
15+
16+
# Set CUDA-QX paths for nvq++
17+
CUDAQX_INCLUDE="$HOME/.cudaqx/include"
18+
CUDAQX_LIB="$HOME/.cudaqx/lib"
19+
20+
LIB=$1 # Accepts "qec", "solvers", or "all"
21+
22+
echo "Running example tests for $LIB..."
23+
echo "----------------------------------"
24+
25+
# List to track failed tests
26+
FAILED_TESTS=()
27+
28+
run_python_test() {
29+
local file=$1
30+
echo "Running Python example: $file"
31+
echo "------------------------------"
32+
python3 "$file"
33+
if [ $? -ne 0 ]; then
34+
echo "Python test failed: $file"
35+
FAILED_TESTS+=("$file")
36+
fi
37+
echo ""
38+
}
39+
40+
run_cpp_test() {
41+
local file=$1
42+
local lib_flag=$2
43+
echo "Compiling and running C++ example: $file"
44+
echo "-----------------------------------------"
45+
46+
nvq++ --enable-mlir $lib_flag \
47+
-I"$CUDAQX_INCLUDE" -L"$CUDAQX_LIB" -Wl,-rpath,"$CUDAQX_LIB" \
48+
"$file"
49+
50+
if [ $? -ne 0 ]; then
51+
echo "Compilation failed: $file"
52+
FAILED_TESTS+=("$file")
53+
return
54+
fi
55+
56+
./a.out
57+
if [ $? -ne 0 ]; then
58+
echo "Execution failed: $file"
59+
FAILED_TESTS+=("$file")
60+
fi
61+
echo ""
62+
}
63+
64+
if [[ "$LIB" == "qec" || "$LIB" == "all" ]]; then
65+
echo "Running QEC examples..."
66+
echo "------------------------"
67+
68+
for file in examples/qec/python/*.py; do
69+
run_python_test "$file"
70+
done
71+
72+
for file in examples/qec/cpp/*.cpp; do
73+
run_cpp_test "$file" "--target=stim -lcudaq-qec"
74+
done
75+
fi
76+
77+
if [[ "$LIB" == "solvers" || "$LIB" == "all" ]]; then
78+
echo "Running Solvers examples..."
79+
echo "---------------------------"
80+
81+
for file in examples/solvers/python/*.py; do
82+
run_python_test "$file"
83+
done
84+
85+
for file in examples/solvers/cpp/*.cpp; do
86+
run_cpp_test "$file" "-lcudaq-solvers"
87+
done
88+
fi
89+
90+
# Final summary
91+
if [ ${#FAILED_TESTS[@]} -ne 0 ]; then
92+
echo "========================================"
93+
echo "Some tests failed:"
94+
for test in "${FAILED_TESTS[@]}"; do
95+
echo "- $test"
96+
done
97+
echo "========================================"
98+
exit 1
99+
else
100+
echo "All tests passed successfully!"
101+
exit 0
102+
fi

0 commit comments

Comments
 (0)