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

Add ENABLE_LAPACK #678

Merged
merged 20 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
* Add `workflow_dispatch` to wheels recipes; allowing developers to build wheels manually on a branch instead of temporarily changing the headers.
[(#679)](https://github.com/PennyLaneAI/pennylane-lightning/pull/679)

* Add the `ENABLE_LAPACK` compilation flag to toggle dynamic linking to LAPACK library.
[(#678)](https://github.com/PennyLaneAI/pennylane-lightning/pull/678)

### Documentation

### Bug fixes
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/tests_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ jobs:
-DBUILD_TESTS=ON `
-DENABLE_OPENMP=OFF `
-DENABLE_PYTHON=OFF `
-DENABLE_LAPACK=OFF `
-DENABLE_GATE_DISPATCHER=OFF `
-DPL_BACKEND=${{ matrix.pl_backend }} `
-DENABLE_WARNINGS=OFF
Expand Down Expand Up @@ -187,7 +188,7 @@ jobs:

- name: Install dependencies
run: |
python -m pip install cmake build ninja scipy
python -m pip install cmake build ninja

- name: Checkout PennyLane-Lightning
uses: actions/checkout@v4
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/wheel_linux_aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ jobs:
# Python build settings
CIBW_BEFORE_BUILD: |
cat /etc/yum.conf | sed "s/\[main\]/\[main\]\ntimeout=5/g" > /etc/yum.conf
python -m pip install ninja cmake~=3.24.0 scipy
python -m pip install ninja cmake~=3.24.0

CIBW_ENVIRONMENT: |
PL_BACKEND="${{ matrix.pl_backend }}"
CIBW_ENVIRONMENT: PL_BACKEND="${{ matrix.pl_backend }}" CMAKE_ARGS="-DENABLE_LAPACK=OFF"

CIBW_MANYLINUX_AARCH64_IMAGE: manylinux2014

Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/wheel_linux_ppc64le.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ jobs:
# Python build settings
CIBW_BEFORE_BUILD: |
cat /etc/yum.conf | sed "s/\[main\]/\[main\]\ntimeout=5/g" > /etc/yum.conf
python -m pip install ninja cmake~=3.24.0 scipy
python -m pip install ninja cmake~=3.24.0

CIBW_ENVIRONMENT: |
PL_BACKEND="${{ matrix.pl_backend }}"
CIBW_ENVIRONMENT: PL_BACKEND="${{ matrix.pl_backend }}" CMAKE_ARGS="-DENABLE_LAPACK=OFF"

CIBW_MANYLINUX_PPC64LE_IMAGE: manylinux2014

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/wheel_win_x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ jobs:
python -m pip install pybind11 cmake~=3.24.0 build

CIBW_ENVIRONMENT: |
CMAKE_ARGS="-DENABLE_LAPACK=OFF"
PL_BACKEND="${{ matrix.pl_backend }}"

CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
Expand Down
47 changes: 26 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ option(ENABLE_WARNINGS "Enable warnings" ON)
option(ENABLE_NATIVE "Enable native CPU build tuning" OFF)
option(ENABLE_PYTHON "Enable compilation of the Python module" ON)

option(ENABLE_LAPACK "Enable compilation with scipy/LAPACK" ON)

# OpenMP
find_package(OpenMP)
if (OpenMP_CXX_FOUND)
Expand Down Expand Up @@ -81,39 +83,42 @@ set(CMAKE_POLICY_DEFAULT_CMP0127 NEW) # To suppress pybind11 CMP0127 warning
# Add pybind11
include(FetchContent)

# TODO: Add a flag to toggle the Python requirement.
find_package(Python COMPONENTS Interpreter Development)
message("Python site-packages directory: ${Python_SITELIB}")
set(SCIPYLIBS ${Python_SITELIB})

if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
set(SCIPYLIBS "/System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/libLAPACK.dylib")
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
if(EXISTS ${SCIPYLIBS}/scipy.libs)
set(SCIPYLIBS ${SCIPYLIBS}/scipy.libs)
if(ENABLE_LAPACK)
find_package(Python COMPONENTS Interpreter Development)
set(SCIPYLIBS ${Python_SITELIB})

if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
set(SCIPYLIBS "/System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/libLAPACK.dylib")
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
if(EXISTS ${SCIPYLIBS}/scipy.libs)
set(SCIPYLIBS ${SCIPYLIBS}/scipy.libs)
else()
# Fallback to the lib path of Python for `conda` support
set(SCIPYLIBS ${SCIPYLIBS}/../..)
endif()
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
else()
# Fallback to the lib path of Python for `conda` support
set(SCIPYLIBS ${SCIPYLIBS}/../..)
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif()
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
# TODO: add windows support
# string(REPLACE "\\" "/" SCIPYLIBS "${SCIPYLIBS}")
# message("Python site-packages directory (Windows): ${SCIPYLIBS}")
else()
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/pennylane_lightning/core/src/utils/config.h.in
${CMAKE_CURRENT_SOURCE_DIR}/pennylane_lightning/core/src/utils/config.h)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/pennylane_lightning/core/src/utils/config.h.in
${CMAKE_CURRENT_SOURCE_DIR}/pennylane_lightning/core/src/utils/config.h)

message(STATUS "Python scipy-lib path: ${SCIPYLIBS}")
endif()

if(ENABLE_PYTHON)
find_package(Python COMPONENTS Interpreter Development)
FetchContent_Declare(pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.11.1
)
FetchContent_MakeAvailable(pybind11)
endif()

# Print Python site-packages directory for reference
message("Python site-packages directory: ${Python_SITELIB}")

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

# All CMakeLists.txt in subdirectories use pennylane_lightning_compile_options and pennylane_lightning_external_libs
Expand Down
3 changes: 3 additions & 0 deletions cmake/process_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ if (UNIX AND (${CMAKE_SYSTEM_PROCESSOR} MATCHES "(AMD64)|(X64)|(x64)|(x86_64)"))
target_compile_options(lightning_compile_options INTERFACE -mavx)
endif()

if(ENABLE_LAPACK)
target_compile_options(lightning_compile_options INTERFACE "-DPL_USE_LAPACK=1")
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ TEST_CASE("Expval Shot- NamedObs", "[MeasurementsBase][Observables]") {
}
}

#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
template <typename TypeList> void testHermitianObsExpvalShot() {
if constexpr (!std::is_same_v<TypeList, void>) {
using StateVectorT = typename TypeList::Type;
Expand Down Expand Up @@ -839,7 +839,7 @@ template <typename TypeList> void testTensorProdObsExpvalShot() {
expected, static_cast<PrecisionT>(0.20)));
}

#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
DYNAMIC_SECTION(" With Identity and shots_range"
<< StateVectorToName<StateVectorT>::name) {
size_t num_shots = 80000;
Expand Down Expand Up @@ -1011,7 +1011,7 @@ TEST_CASE("Var - HermitianObs", "[MeasurementsBase][Observables]") {
}
}

#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
template <typename TypeList> void testHermitianObsShotVar() {
if constexpr (!std::is_same_v<TypeList, void>) {
using StateVectorT = typename TypeList::Type;
Expand Down Expand Up @@ -1147,7 +1147,7 @@ template <typename TypeList> void testTensorProdObsVarShot() {
expected, static_cast<PrecisionT>(0.20)));
}

#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
DYNAMIC_SECTION("With Hermitian and NameObs"
<< StateVectorToName<StateVectorT>::name) {
using MatrixT = std::vector<ComplexT>;
Expand Down Expand Up @@ -1555,7 +1555,7 @@ template <typename TypeList> void testHamiltonianObsExpvalShot() {
REQUIRE_THAT(res, Catch::Matchers::WithinRel(
expected, static_cast<PrecisionT>(0.20)));
}
#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
DYNAMIC_SECTION("YHer" << StateVectorToName<StateVectorT>::name) {
auto Y0 = std::make_shared<NamedObs<StateVectorT>>(
"PauliY", std::vector<size_t>{0});
Expand Down Expand Up @@ -1636,6 +1636,7 @@ template <typename TypeList> void testHamiltonianObsVarShot() {
expected, static_cast<PrecisionT>(0.20)));
}

#ifdef PL_USE_LAPACK
DYNAMIC_SECTION("YHer" << StateVectorToName<StateVectorT>::name) {
using ComplexT = typename StateVectorT::ComplexT;
auto Y0 = std::make_shared<NamedObs<StateVectorT>>(
Expand Down Expand Up @@ -1664,6 +1665,7 @@ template <typename TypeList> void testHamiltonianObsVarShot() {
REQUIRE_THAT(res, Catch::Matchers::WithinRel(
expected, static_cast<PrecisionT>(0.20)));
}
#endif

testHamiltonianObsVarShot<typename TypeList::Next>();
}
Expand Down
14 changes: 8 additions & 6 deletions pennylane_lightning/core/src/observables/Observables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "Error.hpp"
#include "Util.hpp"

#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
#include "UtilLinearAlg.hpp"
#endif

Expand Down Expand Up @@ -220,7 +220,7 @@ class HermitianObsBase : public Observable<StateVectorT> {
MatrixT matrix_;
std::vector<size_t> wires_;

#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
std::vector<PrecisionT> eigenVals_;
MatrixT unitary_;
#endif
Expand All @@ -245,7 +245,7 @@ class HermitianObsBase : public Observable<StateVectorT> {
: matrix_{std::move(matrix)}, wires_{std::move(wires)} {
PL_ASSERT(matrix_.size() == Util::exp2(2 * wires_.size()));

#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
std::vector<std::complex<PrecisionT>> mat(matrix_.size());

std::transform(matrix_.begin(), matrix_.end(), mat.begin(),
Expand Down Expand Up @@ -284,7 +284,7 @@ class HermitianObsBase : public Observable<StateVectorT> {
[[maybe_unused]] StateVectorT &sv,
[[maybe_unused]] std::vector<std::vector<PrecisionT>> &eigenValues,
[[maybe_unused]] std::vector<size_t> &ob_wires) const override {
#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
std::vector<std::complex<PrecisionT>> mat(matrix_.size());

std::transform(matrix_.begin(), matrix_.end(), mat.begin(),
Expand All @@ -303,8 +303,10 @@ class HermitianObsBase : public Observable<StateVectorT> {
sv.applyMatrix(unitary_, wires_);
eigenValues.push_back(eigenVals_);
#else
PL_ABORT("Hermitian observables do not support shot measurement for "
"Windows.");
PL_ABORT(
"Hermitian observables with shot measurement are not supported; "
"consider building the device with `ENABLE_LAPACK=ON` compilation "
"flag.");
#endif
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ template <typename TypeList> void testHermitianObsBase() {
REQUIRE(ob2 != ob3);
}

#ifndef _MSC_VER
#ifdef PL_USE_LAPACK
DYNAMIC_SECTION("Failed to create a HermitianObs- "
<< StateVectorToName<StateVectorT>::name) {
std::mt19937_64 re{1337};
Expand Down Expand Up @@ -244,9 +244,8 @@ template <typename TypeList> void testHermitianObsBase() {

REQUIRE_THROWS_WITH(
obs.applyInPlaceShots(state_vector, eigenValues, ob_wires),
Catch::Matchers::Contains(
"Hermitian observables do not support shot measurement for "
"Windows."));
Catch::Matchers::Contains("Hermitian observables with shot "
"measurement are not supported"));
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(LQUBIT_UTILS_FILES RuntimeInfo.cpp CACHE INTERNAL "" FORCE)

add_library(lightning_utils STATIC ${LQUBIT_UTILS_FILES})

if(NOT WIN32)
if(ENABLE_LAPACK)
list(APPEND LQUBIT_UTILS_FILES UtilLinearAlg.cpp)
endif()

Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/src/utils/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ set(TEST_SOURCES Test_BitUtil.cpp
Test_Util.cpp
)

if(NOT WIN32)
if(ENABLE_LAPACK)
list(APPEND TEST_SOURCES Test_UtilLinearAlg.cpp)
endif()

Expand Down
Loading