Skip to content

Commit

Permalink
setup nanobind: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
KowerKoint committed Jan 9, 2024
1 parent c44330f commit f368795
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
30 changes: 19 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ if(NOT eigen_fetch_POPULATED)
add_subdirectory(${eigen_fetch_SOURCE_DIR})
endif(NOT eigen_fetch_POPULATED)

# pybind11
# nanobind
if(QULACS_USE_PYTHON)
find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)
FetchContent_Declare(
pybind11_fetch
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.10.0
nanobind_fetch
GIT_REPOSITORY https://github.com/wjakob/nanobind
GIT_TAG v1.8.0
)
FetchContent_GetProperties(pybind11_fetch)
if(NOT pybind11_fetch_POPULATED)
message(STATUS "Fetch pybind11 for python-binding")
FetchContent_Populate(pybind11_fetch)
add_subdirectory(${pybind11_fetch_SOURCE_DIR})
endif(NOT pybind11_fetch_POPULATED)
FetchContent_GetProperties(nanobind_fetch)
if(NOT nanobind_fetch_POPULATED)
message(STATUS "Fetch nanobind for python-binding")
FetchContent_Populate(nanobind_fetch)
add_subdirectory(${nanobind_fetch_SOURCE_DIR})
endif(NOT nanobind_fetch_POPULATED)
endif(QULACS_USE_PYTHON)

# Google test
Expand Down Expand Up @@ -138,7 +139,14 @@ if(QULACS_USE_EXE)
add_subdirectory(exe)
endif(QULACS_USE_EXE)

### Custom target ###
# python
if(QULACS_USE_PYTHON)
add_custom_target(
python
DEPENDS qulacs_core
)
endif(QULACS_USE_PYTHON)

# test
if(QULACS_USE_TEST)
add_custom_target(
Expand Down
4 changes: 2 additions & 2 deletions pysrc/qulacs/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
__version_tuple__: VERSION_TUPLE
version_tuple: VERSION_TUPLE

__version__ = version = '0.1.dev36+geedaa0e.d20231019'
__version_tuple__ = version_tuple = (0, 1, 'dev36', 'geedaa0e.d20231019')
__version__ = version = '0.1.dev122+gc44330f.d20240104'
__version_tuple__ = version_tuple = (0, 1, 'dev122', 'gc44330f.d20240104')
10 changes: 6 additions & 4 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.21)

pybind11_add_module(qulacs_core SHARED EXCLUDE_FROM_ALL binding.cpp)
target_link_libraries(qulacs_core PRIVATE Kokkos::kokkos)
target_link_libraries(qulacs_core PUBLIC qulacs)
target_include_directories(qulacs_core PRIVATE ${PROJECT_SOURCE_DIR}/qulacs)
nanobind_add_module(qulacs_core SHARED EXCLUDE_FROM_ALL binding.cpp)
target_include_directories(qulacs_core PUBLIC ${PYTHON3_INCLUDE_DIRS})
target_link_directories(qulacs_core PUBLIC ${PYTHON3_LIBRARIES})
# target_link_libraries(qulacs_core PRIVATE Kokkos::kokkos)
# target_link_libraries(qulacs_core PUBLIC qulacs)
# target_include_directories(qulacs_core PRIVATE ${PROJECT_SOURCE_DIR}/qulacs)
4 changes: 2 additions & 2 deletions python/binding.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <pybind11/pybind11.h>
#include <nanobind/nanobind.h>

#include <state/state_vector.hpp>

PYBIND11_MODULE(qulacs_core, m) { pybind11::class_<StateVector>(m, "StateVector"); }
NB_MODULE(qulacs_core, m) { nb::class_<StateVector>(m, "StateVector"); }
2 changes: 2 additions & 0 deletions script/build_gcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GXX_COMMAND=${CXX_COMPILER:-"g++"}

QULACS_USE_OMP=${QULACS_USE_OMP:-"ON"}
QULACS_USE_CUDA=${QULACS_USE_CUDA:-"OFF"}
QULACS_USE_PYTHON=${QULACS_USE_PYTHON:-"OFF"}
QULACS_USE_TEST=${QULACS_USE_TEST:-"ON"}
QULACS_USE_EXE=${QULACS_USE_EXE:-"ON"}

Expand All @@ -15,6 +16,7 @@ CMAKE_OPS="-D CMAKE_CXX_COMPILER=${GXX_COMMAND} \
-D CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
-D QULACS_USE_OMP=${QULACS_USE_OMP} \
-D QULACS_USE_CUDA=${QULACS_USE_CUDA} \
-D QULACS_USE_PYTHON=${QULACS_USE_PYTHON} \
-D QULACS_USE_TEST=${QULACS_USE_TEST}"

mkdir -p ./build
Expand Down
21 changes: 6 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,8 @@ def run(self):
def build_extension(self, ext):
build_args, cmake_args = self._generate_args(ext)

if self.opt_flags is not None:
opt_flags = self.opt_flags
elif os.getenv("QULACS_OPT_FLAGS"):
opt_flags = os.getenv("QULACS_OPT_FLAGS")
else:
opt_flags = None
if opt_flags:
cmake_args += ["-DOPT_FLAGS=" + opt_flags]

if os.getenv("QULACS_USE_GPU"):
cmake_args += ["-DQULACS_USE_GPU:STR=" + os.getenv("QULACS_USE_GPU")]
if os.getenv("QULACS_USE_CUDA"):
cmake_args += ["-DQULACS_USE_CUDA:STR=" + os.getenv("QULACS_USE_CUDA")]

if os.getenv("QULACS_USE_OMP"):
cmake_args += ["-DQULACS_USE_OMP:STR=" + os.getenv("QULACS_USE_OMP")]
Expand Down Expand Up @@ -85,11 +76,11 @@ def _generate_args(self, ext):
"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=" + archive_dir,
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=" + bindir,
"-DCMAKE_POSITION_INDEPENDENT_CODE=Yes",
"-DCMAKE_POSITION_INDEPENDENT_CODE=ON",
"-DPYTHON_EXECUTABLE=" + sys.executable,
"-DPYTHON_SETUP_FLAG:STR=Yes",
"-DQULACS_USE_GPU:STR=No",
"-DQULACS_USE_PYTHON=Yes",
"-DPYTHON_SETUP_FLAG:STR=ON",
"-DQULACS_USE_CUDA:STR=OFF",
"-DQULACS_USE_PYTHON=ON",
]

cfg = "Debug" if self.debug else "Release"
Expand Down

0 comments on commit f368795

Please sign in to comment.