Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
multiphaseCFD committed Apr 26, 2024
1 parent 534a222 commit f18a66e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 125 deletions.
41 changes: 6 additions & 35 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
8 changes: 7 additions & 1 deletion pennylane_lightning/core/src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
71 changes: 29 additions & 42 deletions pennylane_lightning/core/src/utils/UtilLinearAlg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
#include <cstdlib>
#include <filesystem>
#include <memory>
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <string>
#include <vector>

#include <iostream>

#include "SharedLibLoader.hpp"

#include "config.h"

/// @cond DEV
namespace {
// Declare heev function pointers to access corresponding functions in
Expand All @@ -49,6 +49,29 @@ using cheevPtr = void (*)(const char *, const char *, const int *,
std::array<std::string, 5> 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

Expand Down Expand Up @@ -111,53 +134,17 @@ 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<SharedLibLoader> blasLib =
std::make_shared<SharedLibLoader>(libName);
// LCOV_EXCL_STOP
#else
std::shared_ptr<SharedLibLoader> blasLib;
std::vector<std::shared_ptr<SharedLibLoader>> 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);

Expand Down
23 changes: 0 additions & 23 deletions pennylane_lightning/core/src/utils/config.h

This file was deleted.

24 changes: 0 additions & 24 deletions pennylane_lightning/core/src/utils/config.h.in

This file was deleted.

0 comments on commit f18a66e

Please sign in to comment.