Skip to content

Commit

Permalink
Add support for building multiple backend simulators (#497)
Browse files Browse the repository at this point in the history
* Add PL_BACKEND_LIST

* Update the support

* Exclude Python bindings

* Update HermitianObs name scope conflicts

* Auto update version

* Cleanup

* Update CI to build and check C++ tests of multiple backends (Linux)

* Update changelog

* Update .github/workflows/tests_linux.yml

Co-authored-by: Vincent Michaud-Rioux <vincentm@nanoacademic.com>

* Apply code review suggestions

* Update .github/workflows/tests_linux.yml

Co-authored-by: Amintor Dusko <87949283+AmintorDusko@users.noreply.github.com>

---------

Co-authored-by: Dev version update bot <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Vincent Michaud-Rioux <vincentm@nanoacademic.com>
Co-authored-by: Amintor Dusko <87949283+AmintorDusko@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 13, 2023
1 parent 3fbd4ce commit 9426f9e
Show file tree
Hide file tree
Showing 24 changed files with 242 additions and 159 deletions.
2 changes: 2 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* Add support for dependent external packages to C++ core.
[(#482)](https://github.com/PennyLaneAI/pennylane-lightning/pull/482)

* Add support for building multiple backend simulators.
[(#497)](https://github.com/PennyLaneAI/pennylane-lightning/pull/497)

### Documentation

Expand Down
59 changes: 58 additions & 1 deletion .github/workflows/tests_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,61 @@ jobs:
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
token: ${{ secrets.CODECOV_TOKEN }}

cpptestsWithMultipleBackends:
# Device-specific tests are performed for both. Device-agnostic tests default to LightningQubit.
needs: [build_and_cache_Kokkos]
strategy:
matrix:
os: [ubuntu-22.04]
exec_model: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.exec_model) }}
kokkos_version: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.kokkos_version) }}

name: C++ tests (multiple backends)
runs-on: ${{ matrix.os }}

steps:
- uses: actions/setup-python@v4
name: Install Python
with:
python-version: '3.9'

- name: Checkout PennyLane-Lightning
uses: actions/checkout@v3

- name: Restoring cached dependencies
id: kokkos-cache
uses: actions/cache@v3
with:
path: ${{ github.workspace}}/Kokkos_install/${{ matrix.exec_model }}
key: ${{ matrix.os }}-kokkos${{ matrix.kokkos_version }}-${{ matrix.exec_model }}

- name: Copy cached libraries
run: |
mkdir Kokkos/
cp -rf ${{ github.workspace}}/Kokkos_install/${{ matrix.exec_model }}/* Kokkos/
- name: Install dependencies
run: sudo apt-get update && sudo apt-get -y -q install cmake gcc-$GCC_VERSION g++-$GCC_VERSION ninja-build gcovr lcov

- name: Build and run unit tests
run: |
cmake . -BBuild -G Ninja \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DBUILD_TESTS=ON \
-DENABLE_PYTHON=OFF \
-DCMAKE_PREFIX_PATH=${{ github.workspace }}/Kokkos \
-DPL_BACKEND="lightning_qubit;lightning_kokkos" \
-DCMAKE_CXX_COMPILER=$(which g++-$GCC_VERSION)
cmake --build ./Build
cd ./Build
mkdir -p ./tests/results_multiple_backends
for file in *runner ; do ./$file --order lex --reporter junit --out ./tests/results_multiple_backends/report_$file.xml; done;
- name: Upload test results
uses: actions/upload-artifact@v3
if: always()
with:
name: ubuntu-tests-reports-${{ github.job }}-multiple-backends
path: ./Build/tests/results_multiple_backends/
25 changes: 19 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ option(BUILD_BENCHMARKS "Enable cpp benchmarks" OFF)
# Backend
set(PL_BACKEND "lightning_qubit" CACHE STRING "PennyLane Lightning backend")

# Python bindings are not supported while building multiple backend devices
list(LENGTH PL_BACKEND PL_BACKEND_LEN)
if ((${PL_BACKEND_LEN} GREATER 1) AND ENABLE_PYTHON)
message(FATAL_ERROR "Lightning does not provide Python support for building multiple backends. Requested backends: ${PL_BACKEND}")
endif()

# Print PL_BACKEND
foreach(BACKEND ${PL_BACKEND})
message(STATUS "PL_BACKEND: ${BACKEND}")
endforeach()

# Process compile options
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/process_options.cmake")
Expand Down Expand Up @@ -94,12 +105,14 @@ target_link_libraries(pennylane_lightning INTERFACE lightning_observables
lightning_algorithms
)

target_link_libraries(pennylane_lightning INTERFACE ${PL_BACKEND} #simulator
"${PL_BACKEND}_algorithms"
"${PL_BACKEND}_observables"
"${PL_BACKEND}_bindings"
"${PL_BACKEND}_measurements"
)
foreach(BACKEND ${PL_BACKEND})
target_link_libraries(pennylane_lightning INTERFACE "${BACKEND}" #simulator
"${BACKEND}_algorithms"
"${BACKEND}_observables"
"${BACKEND}_bindings"
"${BACKEND}_measurements"
)
endforeach()

target_include_directories(pennylane_lightning INTERFACE "$<INSTALL_INTERFACE:${PROJECT_SOURCE_DIR}/pennylane_lightning/core/src;include>")

Expand Down
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ PYTHON := python3
COVERAGE := --cov=pennylane_lightning --cov-report term-missing --cov-report=html:coverage_html_report
TESTRUNNER := -m pytest tests --tb=short

PL_BACKEND?="$(if $(backend:-=),$(backend),lightning_qubit)"

ifdef verbose
VERBOSE := --verbose
else
Expand Down Expand Up @@ -68,22 +70,22 @@ coverage:
pl-device-test --device $(if $(device:-=),$(device),lightning.qubit) --shots=None --skip-ops $(COVERAGE) --cov-append

coverage-cpp:
@echo "Generating cpp coverage report in BuildCov/out for $(if $(backend:-=),$(backend),lightning_qubit) backend"
@echo "Generating cpp coverage report in BuildCov/out for $(PL_BACKEND) backend"
rm -rf ./BuildCov
cmake -BBuildCov -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON -DENABLE_COVERAGE=ON -DPL_BACKEND=$(if $(backend:-=),$(backend),lightning_qubit)
cmake -BBuildCov -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON -DENABLE_COVERAGE=ON -DPL_BACKEND=$(PL_BACKEND)
cmake --build ./BuildCov
cd ./BuildCov; for file in *runner ; do ./$file; done; \
lcov --directory . -b ../pennylane_lightning/core/src --capture --output-file coverage.info; \
genhtml coverage.info --output-directory out

build:
rm -rf ./Build
cmake -BBuild -DENABLE_BLAS=ON -DENABLE_KOKKOS=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(if $(backend:-=),$(backend),lightning_qubit)
cmake -BBuild -G Ninja -DENABLE_BLAS=ON -DENABLE_KOKKOS=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(PL_BACKEND)
cmake --build ./Build $(VERBOSE)

test-cpp:
rm -rf ./BuildTests
cmake -BBuildTests -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON -DENABLE_KOKKOS=ON -DENABLE_OPENMP=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(if $(backend:-=),$(backend),lightning_qubit)
cmake -BBuildTests -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON -DENABLE_KOKKOS=ON -DENABLE_OPENMP=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(PL_BACKEND)
ifdef target
cmake --build ./BuildTests $(VERBOSE) --target $(target)
OMP_PROC_BIND=false ./BuildTests/$(target)
Expand All @@ -94,7 +96,7 @@ endif

test-cpp-blas:
rm -rf ./BuildTests
cmake -BBuildTests -DBUILD_TESTS=ON -DENABLE_BLAS=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(if $(backend:-=),$(backend),lightning_qubit)
cmake -BBuildTests -G Ninja -DBUILD_TESTS=ON -DENABLE_BLAS=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(PL_BACKEND)
cmake --build ./BuildTests $(VERBOSE)
cmake --build ./BuildTests $(VERBOSE) --target test

Expand All @@ -110,7 +112,7 @@ format-python:
.PHONY: check-tidy
check-tidy:
rm -rf ./BuildTidy
cmake -BBuildTidy -DENABLE_CLANG_TIDY=ON -DBUILD_TESTS=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(if $(backend:-=),$(backend),lightning_qubit)
cmake -BBuildTidy -DENABLE_CLANG_TIDY=ON -DBUILD_TESTS=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(PL_BACKEND)
ifdef target
cmake --build ./BuildTidy $(VERBOSE) --target $(target)
else
Expand Down
22 changes: 12 additions & 10 deletions cmake/support_simulators.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ MACRO(FIND_AND_ADD_SIMULATOR)
# Finding the list of simulators:
FIND_SIMULATORS_LIST(SIMULATORS_LIST)

if (${PL_BACKEND} IN_LIST SIMULATORS_LIST)
add_subdirectory(${PL_BACKEND})
else()
message("Could not find the required backend. Options found are:")
FOREACH(SIMULATOR ${SIMULATORS_LIST})
message(" * " ${SIMULATOR})
ENDFOREACH()
message(FATAL_ERROR "Building process will not proceed. Failed to find backend.")
endif()
ENDMACRO()
FOREACH(BACKEND ${PL_BACKEND})
if (${BACKEND} IN_LIST SIMULATORS_LIST)
add_subdirectory(${BACKEND})
else()
message("Could not find the requested backend. Options found are:")
FOREACH(SIMULATOR ${SIMULATORS_LIST})
message(" * " ${SIMULATOR})
ENDFOREACH()
message(FATAL_ERROR "Building process will not proceed. Failed to find backend.")
endif()
ENDFOREACH()
ENDMACRO()
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.33.0-dev7"
__version__ = "0.33.0-dev8"
10 changes: 6 additions & 4 deletions pennylane_lightning/core/src/algorithms/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ target_link_libraries(algorithms_tests INTERFACE Catch2::Catch2
)

# Create dependencies on the dynamically defined simulator/backend targets.
target_link_libraries(algorithms_tests INTERFACE ${PL_BACKEND}
"${PL_BACKEND}_algorithms"
"${PL_BACKEND}_observables"
)
foreach(BACKEND ${PL_BACKEND})
target_link_libraries(algorithms_tests INTERFACE ${BACKEND}
"${BACKEND}_algorithms"
"${BACKEND}_observables"
)
endforeach()

ProcessTestOptions(algorithms_tests)

Expand Down
10 changes: 6 additions & 4 deletions pennylane_lightning/core/src/measurements/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ add_library(measurements_tests INTERFACE)
target_link_libraries(measurements_tests INTERFACE Catch2::Catch2)

# Create dependencies on the dynamically defined simulator/backend targets.
target_link_libraries(measurements_tests INTERFACE ${PL_BACKEND}
"${PL_BACKEND}_measurements"
"${PL_BACKEND}_observables"
)
foreach(BACKEND ${PL_BACKEND})
target_link_libraries(measurements_tests INTERFACE ${BACKEND}
"${BACKEND}_measurements"
"${BACKEND}_observables"
)
endforeach()

ProcessTestOptions(measurements_tests)

Expand Down
5 changes: 3 additions & 2 deletions pennylane_lightning/core/src/observables/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ add_executable(observables_test_runner ${TEST_SOURCES})
target_link_libraries(observables_test_runner PRIVATE observables_tests)

# Create dependency on the dynamically defined simulator/backend target.
target_link_libraries(observables_test_runner PRIVATE ${PL_BACKEND}
)
foreach(BACKEND ${PL_BACKEND})
target_link_libraries(observables_test_runner PRIVATE ${BACKEND})
endforeach()

catch_discover_tests(observables_test_runner)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ target_link_libraries(base_tests INTERFACE Catch2::Catch2)
ProcessTestOptions(base_tests)

# Create dependency on the dynamically defined simulator/backend target.
target_link_libraries(base_tests INTERFACE ${PL_BACKEND})
foreach(BACKEND ${PL_BACKEND})
target_link_libraries(base_tests INTERFACE ${BACKEND})
endforeach()

target_sources(base_tests INTERFACE runner_base.cpp)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ set(LOGO [=[
]=])
message(${LOGO})

project(${PL_BACKEND}
project(lightning_kokkos
DESCRIPTION "PennyLane Lightning Kokkos C++ Backend."
LANGUAGES CXX C
)

set(LKOKKOS_FILES StateVectorKokkos.cpp
CACHE INTERNAL "" FORCE)

add_library(${PL_BACKEND} STATIC ${LKOKKOS_FILES})
add_library(lightning_kokkos STATIC ${LKOKKOS_FILES})
target_compile_options(lightning_compile_options INTERFACE "-D_ENABLE_PLKOKKOS=1")

##########################
Expand Down Expand Up @@ -51,18 +51,18 @@ if(PLKOKKOS_ENABLE_SANITIZER)
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
ENDIF()

target_link_libraries(${PL_BACKEND} PUBLIC lightning_compile_options
target_link_libraries(lightning_kokkos PUBLIC lightning_compile_options
lightning_external_libs
lightning_base
lightning_gates
lightning_utils
${PL_BACKEND}_utils
${PL_BACKEND}_gates
lightning_kokkos_utils
lightning_kokkos_gates
)
target_include_directories(${PL_BACKEND} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TARGET ${PL_BACKEND} PROPERTY POSITION_INDEPENDENT_CODE ON)
target_include_directories(lightning_kokkos PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TARGET lightning_kokkos PROPERTY POSITION_INDEPENDENT_CODE ON)
if(PLKOKKOS_ENABLE_NATIVE)
target_compile_options(${PL_BACKEND} PRIVATE -march=native)
target_compile_options(lightning_kokkos PRIVATE -march=native)
endif()

###############################################################################
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
cmake_minimum_required(VERSION 3.20)

project(${PL_BACKEND}_algorithms LANGUAGES CXX)
project(lightning_kokkos_algorithms LANGUAGES CXX)

set(ALGORITHMS_FILES AlgorithmsKokkos.cpp CACHE INTERNAL "" FORCE)
add_library(${PL_BACKEND}_algorithms STATIC ${ALGORITHMS_FILES})
add_library(lightning_kokkos_algorithms STATIC ${ALGORITHMS_FILES})

target_include_directories(${PL_BACKEND}_algorithms INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PL_BACKEND}_algorithms PRIVATE lightning_compile_options
target_include_directories(lightning_kokkos_algorithms INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(lightning_kokkos_algorithms PRIVATE lightning_compile_options
lightning_external_libs
)

target_link_libraries(${PL_BACKEND}_algorithms PUBLIC ${PL_BACKEND}_utils
target_link_libraries(lightning_kokkos_algorithms PUBLIC lightning_kokkos_utils
lightning_algorithms
${PL_BACKEND}
${PL_BACKEND}_observables
lightning_kokkos
lightning_kokkos_observables
)

if (BUILD_TESTS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(${PL_BACKEND}_algorithms_tests)
project(lightning_kokkos_algorithms_tests)

# Default build type for test code is Debug
if(NOT CMAKE_BUILD_TYPE)
Expand All @@ -14,26 +14,25 @@ FetchAndIncludeCatch()
# Define library
################################################################################

add_library(${PL_BACKEND}_algorithms_tests INTERFACE)
target_link_libraries(${PL_BACKEND}_algorithms_tests INTERFACE Catch2::Catch2
${PL_BACKEND}
${PL_BACKEND}_algorithms
${PL_BACKEND}_observables
add_library(lightning_kokkos_algorithms_tests INTERFACE)
target_link_libraries(lightning_kokkos_algorithms_tests INTERFACE Catch2::Catch2
lightning_kokkos
lightning_kokkos_algorithms
lightning_kokkos_observables
)

ProcessTestOptions(${PL_BACKEND}_algorithms_tests)
ProcessTestOptions(lightning_kokkos_algorithms_tests)

target_sources(${PL_BACKEND}_algorithms_tests INTERFACE runner_${PL_BACKEND}_algorithms.cpp)
target_sources(lightning_kokkos_algorithms_tests INTERFACE runner_lightning_kokkos_algorithms.cpp)

################################################################################
# Define targets
################################################################################
set(TEST_SOURCES Test_AdjointJacobianKokkos.cpp
)
set(TEST_SOURCES Test_AdjointJacobianKokkos.cpp)

add_executable(${PL_BACKEND}_algorithms_test_runner ${TEST_SOURCES})
target_link_libraries(${PL_BACKEND}_algorithms_test_runner PRIVATE ${PL_BACKEND}_algorithms_tests)
add_executable(lightning_kokkos_algorithms_test_runner ${TEST_SOURCES})
target_link_libraries(lightning_kokkos_algorithms_test_runner PRIVATE lightning_kokkos_algorithms_tests)

catch_discover_tests(${PL_BACKEND}_algorithms_test_runner)
catch_discover_tests(lightning_kokkos_algorithms_test_runner)

install(TARGETS ${PL_BACKEND}_algorithms_test_runner DESTINATION bin)
install(TARGETS lightning_kokkos_algorithms_test_runner DESTINATION bin)
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
cmake_minimum_required(VERSION 3.20)

project(${PL_BACKEND}_bindings LANGUAGES CXX)
project(lightning_kokkos_bindings LANGUAGES CXX)

add_library(${PL_BACKEND}_bindings INTERFACE)
add_library(lightning_kokkos_bindings INTERFACE)

target_include_directories(${PL_BACKEND}_bindings INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(lightning_kokkos_bindings INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(${PL_BACKEND}_bindings INTERFACE lightning_bindings
target_link_libraries(lightning_kokkos_bindings INTERFACE lightning_bindings
lightning_utils
${PL_BACKEND}
${PL_BACKEND}_gates
${PL_BACKEND}_utils
lightning_kokkos
lightning_kokkos_gates
lightning_kokkos_utils
)

set_property(TARGET ${PL_BACKEND}_bindings PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET lightning_kokkos_bindings PROPERTY POSITION_INDEPENDENT_CODE ON)
Loading

0 comments on commit 9426f9e

Please sign in to comment.