diff --git a/CMakeLists.txt b/CMakeLists.txt index fd3a08a51b..0d8b3822f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,41 +83,12 @@ set(CMAKE_POLICY_DEFAULT_CMP0127 NEW) # To suppress pybind11 CMP0127 warning # Add pybind11 include(FetchContent) -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() - 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) - - 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}") +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) set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/pennylane_lightning/core/src/utils/CMakeLists.txt b/pennylane_lightning/core/src/utils/CMakeLists.txt index 33a7327a88..ab91c4aba1 100644 --- a/pennylane_lightning/core/src/utils/CMakeLists.txt +++ b/pennylane_lightning/core/src/utils/CMakeLists.txt @@ -23,12 +23,18 @@ foreach(BACKEND ${PL_BACKEND}) endforeach() -target_include_directories(lightning_utils INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(lightning_utils INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} ${Python_INCLUDE_DIRS}) target_link_libraries(lightning_utils INTERFACE lightning_compile_options lightning_external_libs + pybind11::headers + ${Python_LIBRARIES} ) +if(ENABLE_PYTHON) +target_compile_options(lightning_compile_options INTERFACE "-D_ENABLE_PYTHON=1") +endif() + set_property(TARGET lightning_utils PROPERTY POSITION_INDEPENDENT_CODE ON) if (BUILD_TESTS) diff --git a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp index 8d29d2abce..4c1825b2f5 100644 --- a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp +++ b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -32,8 +34,6 @@ #include "SharedLibLoader.hpp" -#include "config.h" - /// @cond DEV namespace { // Declare heev function pointers to access corresponding functions in @@ -49,6 +49,29 @@ using cheevPtr = void (*)(const char *, const char *, const int *, std::array priority_lib{"stdc", "gcc.", "quadmath", "gfortran", "openblas"}; +std::string get_scipylibs_path() { +#ifndef _ENABLE_PYTHON + pybind11::scoped_interpreter scope_guard{}; +#endif + + pybind11::object avail_site_packages = + pybind11::module::import("site").attr("getsitepackages")(); + + std::string scipy_lib_path; + + for (auto item : avail_site_packages) { + std::string tmp_path = pybind11::str(item); + tmp_path += "/scipy.libs"; + if (std::filesystem::exists(tmp_path)) { + return tmp_path; + } + } + + PL_ABORT_IF(scipy_lib_path.empty(), "Can't find scipy.libs"); + + return scipy_lib_path; +} + } // namespace /// @endcond @@ -111,7 +134,9 @@ void compute_diagonalizing_gates(int n, int lda, } #ifdef __APPLE__ // LCOV_EXCL_START - const std::string libName(SCIPY_LIBS_PATH); + const std::string libName = + "/System/Library/Frameworks/Accelerate.framework/Versions/Current/" + "Frameworks/vecLib.framework/libLAPACK.dylib"; std::shared_ptr blasLib = std::make_shared(libName); // LCOV_EXCL_STOP @@ -119,45 +144,7 @@ void compute_diagonalizing_gates(int n, int lda, std::shared_ptr blasLib; std::vector> blasLibs; // For C++ usage - std::string scipyPathStr(SCIPY_LIBS_PATH); - - // Exclusively for python calls - // LCOV_EXCL_START - if (!std::filesystem::exists(scipyPathStr)) { - std::string currentPathStr(getPath()); - std::string site_packages_str("site-packages/"); - - std::size_t str_pos = currentPathStr.find(site_packages_str); - if (str_pos != std::string::npos) { - scipyPathStr = - currentPathStr.substr(0, str_pos + site_packages_str.size()); - scipyPathStr += "scipy.libs"; - } - - if (std::filesystem::exists(scipyPathStr)) { - try { - // convert the relative path to absolute path - scipyPathStr = - std::filesystem::canonical(scipyPathStr).string(); - } catch (const std::exception &err) { - std::cerr << "Canonical path for scipy.libs" - << " threw exception:\n" - << err.what() << '\n'; - } - } else { - try { - scipyPathStr = currentPathStr + "../../scipy.libs/"; - // convert the relative path to absolute path - scipyPathStr = - std::filesystem::canonical(scipyPathStr).string(); - } catch (const std::exception &err) { - std::cerr << "Canonical path for scipy.libs" - << " threw exception:\n" - << err.what() << '\n'; - } - } - } - // LCOV_EXCL_STOP + std::string scipyPathStr = get_scipylibs_path(); std::filesystem::path scipyLibsPath(scipyPathStr); diff --git a/pennylane_lightning/core/src/utils/config.h b/pennylane_lightning/core/src/utils/config.h deleted file mode 100644 index d9c12fd532..0000000000 --- a/pennylane_lightning/core/src/utils/config.h +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2018-2024 Xanadu Quantum Technologies Inc. - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 - -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/** - * @file - * Config file for the path to scipy.libs at compile time. - */ - -#ifndef CONFIG_H -#define CONFIG_H -#define SCIPY_LIBS_PATH "" -#endif diff --git a/pennylane_lightning/core/src/utils/config.h.in b/pennylane_lightning/core/src/utils/config.h.in deleted file mode 100644 index 5664e754f7..0000000000 --- a/pennylane_lightning/core/src/utils/config.h.in +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018-2024 Xanadu Quantum Technologies Inc. - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 - -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/** - * @file - * Config file for the path to scipy.libs at compile time. - */ - - -#ifndef CONFIG_H -#define CONFIG_H -#define SCIPY_LIBS_PATH "${SCIPYLIBS}" -#endif