Commit 330299fb authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Start to work on QCOR python bindings



Initial work to set up the build configs

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 21eabdd0
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -35,6 +35,18 @@ if (QCOR_BUILD_TESTS)
  add_subdirectory(examples)
endif()

# Compile and install Python binding if Python found:
find_package(Python COMPONENTS Interpreter Development)
if(Python_FOUND)
 if(${Python_VERSION} VERSION_GREATER_EQUAL 3.0.0)
   message(STATUS "Found Python version ${Python_VERSION}. Building QCOR Python API with ${Python_INCLUDE_DIRS}")
   add_subdirectory(python)
 else()
  message(STATUS "Found Python version ${Python_VERSION}. Version must be greater than 3.0.0, skipping Python API build.")
 endif()
else()
  message(STATUS "Python interpreter or development headers not found. Skipping Python API build.")
endif()

if (QCOR_CPACK_DEB_PLATFORM)
message(STATUS "CPack DEB Build Enabled.") 

python/CMakeLists.txt

0 → 100644
+26 −0
Original line number Diff line number Diff line
set(LIBRARY_NAME _pyqcor)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -O2 -g -pipe -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wformat -fexceptions --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=native -D_GNU_SOURCE -fPIC -fwrapv")
if(APPLE)
   set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
endif(APPLE)
file(GLOB SRC *.cpp)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(${LIBRARY_NAME} PUBLIC .
                                          ${Python_INCLUDE_DIRS}
                                          ${XACC_ROOT}/include/pybind11/include)
set_target_properties(${LIBRARY_NAME} PROPERTIES PREFIX "")
target_link_libraries(${LIBRARY_NAME} PUBLIC qcor qcor-qsim)

if(APPLE)
   set_target_properties(${LIBRARY_NAME} PROPERTIES INSTALL_RPATH "@loader_path/lib")
   set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
   set_target_properties(${LIBRARY_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/lib")
   set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()

install(FILES qcor.py DESTINATION ${CMAKE_INSTALL_PREFIX})
install(TARGETS _pyqcor DESTINATION ${CMAKE_INSTALL_PREFIX})
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
import sys
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.xacc')

from qcor import *
# Get the NLOpt Optimzier
observable = X(0)*X(1) + Z(0) 
print("Hamiltonian = ", observable.toString())
optimizer = createOptimizer('nlopt')

python/py-qcor.cpp

0 → 100644
+26 −0
Original line number Diff line number Diff line
#include "qcor/qcor_optimizer.hpp"
#include <pybind11/pybind11.h>

namespace py = pybind11;

PYBIND11_MODULE(_pyqcor, m) {
  m.doc() = "Python bindings for QCOR.";
  // Expose QCOR API functions
  m.def(
      "createOptimizer",
      [](const std::string &name, py::dict p = {}) {
        xacc::HeterogeneousMap params;
        std::cout << "HOWDY: " << name << "\n";
        /// TODO: reuse XACC PyHetMap here
        for (auto item : p) {
          std::cout << "key=" << std::string(py::str(item.first)) << ", "
                    << "value=" << std::string(py::str(item.second)) << "\n";
        }

        return qcor::createOptimizer(name, std::move(params));
      },
      py::arg("name"), py::arg("p") = py::dict(),
      py::return_value_policy::reference,
      "Return the Optimizer with given name.");
  { py::module qsim = m.def_submodule("qsim", "QCOR's python qsim submodule"); }
}

python/qcor.py

0 → 100644
+13 −0
Original line number Diff line number Diff line
from _pyqcor import *
# TODO: will need to selectively import XACC
import xacc

def X(idx):
  return xacc.quantum.PauliOperator({idx: 'X'}, 1.0) 

def Y(idx):
  return xacc.quantum.PauliOperator({idx: 'Y'}, 1.0) 

def Z(idx):
  return xacc.quantum.PauliOperator({idx: 'Z'}, 1.0)