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

Restructure the qsim implementation:



- Has a base *lib* which contains base interface for direct linkage.

- Other concrete impls (plugins) are put to a separate library -> install to plugins folder.

This will make sure that we can compile executable code using the qcor compiler.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent d692ea78
Loading
Loading
Loading
Loading
+2 −43
Original line number Diff line number Diff line
set(LIBRARY_NAME qcor-qsim)
file(GLOB SRC *.cpp)
file(GLOB HEADERS *.hpp)
add_subdirectory(base)
add_subdirectory(impls)
usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)

set(_bundle_name qcor_qsim)
add_library(${LIBRARY_NAME} SHARED ${SRC})

target_link_libraries(${LIBRARY_NAME} PUBLIC qcor)

xacc_configure_library_rpath(${LIBRARY_NAME})

set_target_properties(${LIBRARY_NAME}
                      PROPERTIES COMPILE_DEFINITIONS
                                 US_BUNDLE_NAME=${_bundle_name}
                                 US_BUNDLE_NAME
                                 ${_bundle_name})

usfunctionembedresources(TARGET
                         ${LIBRARY_NAME}
                         WORKING_DIRECTORY
                         ${CMAKE_CURRENT_SOURCE_DIR}
                         FILES
												 manifest.json)
												 
if(APPLE)
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "@loader_path/../lib;${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "$ORIGIN/../lib:${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()

install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
install(FILES ${HEADERS} DESTINATION include/qcor)

if (QCOR_BUILD_TESTS)
	add_subdirectory(tests)
endif()
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
set(LIBRARY_NAME qcor-qsim)

file(GLOB SRC *.cpp)
file(GLOB HEADERS *.hpp)

add_library(${LIBRARY_NAME} SHARED ${SRC})

target_link_libraries(${LIBRARY_NAME} PUBLIC qcor)

xacc_configure_library_rpath(${LIBRARY_NAME})

install(FILES ${HEADERS} DESTINATION include/qcor)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
+71 −0
Original line number Diff line number Diff line
#include "qcor_qsim.hpp"
#include "xacc_service.hpp"

namespace qcor {
bool CostFunctionEvaluator::initialize(Observable *observable,
                                       const HeterogeneousMap &params) {
  target_operator = observable;
  // TODO: use qcor data
  quantum_backend = nullptr;
  if (params.pointerLikeExists<Accelerator>("accelerator")) {
    quantum_backend = params.getPointerLike<Accelerator>("accelerator");
  }

  hyperParams = params;
  return target_operator && quantum_backend;
}

CostFunctionEvaluator *CostFunctionEvaluator::getInstance() {
  if (!instance) {
    instance = new CostFunctionEvaluator();
  }
  return instance;
}

double CostFunctionEvaluator::evaluate(
    std::shared_ptr<CompositeInstruction> state_prep) {

  // Measure the observables:
  // TODO: Port the existing VQE impl. as the default.

  // TODO:
  return 0.0;
}

QuatumSimulationModel
ModelBuilder::createModel(Observable *obs, TdObservable td_ham,
                          const HeterogeneousMap &params) {
  QuatumSimulationModel model;
  model.observable = obs;
  model.hamiltonian = td_ham;
  return model;
}

QuatumSimulationModel
ModelBuilder::createModel(Observable *obs, const HeterogeneousMap &params) {
  QuatumSimulationModel model;
  model.observable = obs;
  model.hamiltonian = [&](double t) {
    return *(static_cast<PauliOperator *>(obs));
  };
  return model;
}

QuatumSimulationModel
ModelBuilder::createModel(const std::string &format, const std::string &data,
                          const HeterogeneousMap &params) {
  QuatumSimulationModel model;
  // TODO:
  return model;
}

std::shared_ptr<QuatumSimulationWorkflow>
getWorkflow(const std::string &name, const HeterogeneousMap &init_params) {
  auto qsim_workflow = xacc::getService<QuatumSimulationWorkflow>(name);
  if (qsim_workflow && qsim_workflow->initialize(init_params)) {
    return qsim_workflow;
  }
  // ERROR: unknown workflow or invalid initialization options.
  return nullptr;
}
} // namespace qcor
 No newline at end of file
+29 −11
Original line number Diff line number Diff line
@@ -7,10 +7,11 @@
#include "qrt.hpp"
#include <memory>
#include <xacc_internal_compiler.hpp>
using namespace xacc;
using CompositeInstruction = xacc::CompositeInstruction;
using Accelerator = xacc::Accelerator;
using Identifiable = xacc::Identifiable;

namespace qcor {

// Struct captures a state-preparation circuit.
struct Ansatz {
  std::shared_ptr<CompositeInstruction> circuit;
@@ -27,7 +28,7 @@ struct Ansatz {
//  - Thermo-field doubles state preparation
//  - Two-point correlation function measurements
//  - QFT
class AnsatzGenerator {
class AnsatzGenerator : public Identifiable {
public:
  virtual Ansatz create_ansatz(Observable *obs = nullptr,
                               const HeterogeneousMap &params = {}) = 0;
@@ -60,7 +61,7 @@ using TdObservable = std::function<PauliOperator(double)>;

// Capture a quantum chemistry problem.
// TODO: generalize this to capture all potential use cases.
struct QsimModel {
struct QuatumSimulationModel {
  // Model name.
  std::string name;

@@ -70,6 +71,9 @@ struct QsimModel {
  // The system Hamiltonian which can be static or dynamic (time-dependent).
  // This can be the same or different from the observable operator.
  TdObservable hamiltonian;
  
  // QuatumSimulationModel also support a user-defined (fixed) ansatz.
  std::shared_ptr<CompositeInstruction> user_defined_ansatz;
};

// Generic model builder (factory)
@@ -80,13 +84,13 @@ public:
  // Strongly-typed parameters/argument.
  // Build a simple Hamiltonian-based model: static Hamiltonian which is also
  // the observable of interest.
  static QsimModel createModel(Observable *obs,
  static QuatumSimulationModel createModel(Observable *obs,
                               const HeterogeneousMap &params = {});
  // Build a time-dependent problem model:
  //  -  obs: observable operator to measure.
  //  -  td_ham: time-dependent Hamiltonian to evolve the system.
  //     e.g. a function to map from time to Hamiltonian operator.
  static QsimModel createModel(Observable *obs, TdObservable td_ham,
  static QuatumSimulationModel createModel(Observable *obs, TdObservable td_ham,
                               const HeterogeneousMap &params = {});

  // ========  High-level model builder ==============
@@ -95,27 +99,41 @@ public:
  // QCOR's Observable type. Inputs:
  //  - format: key to look up module/plugin to digest the input data.
  //  - data: model descriptions in plain-text format, e.g. load from file.
  static QsimModel createModel(const std::string &format,
  //  - params: extra parameters to pass to the parser/generator,
  //            e.g. any transformations required in order to generate the Observable.
  static QuatumSimulationModel createModel(const std::string &format,
                               const std::string &data,
                               const HeterogeneousMap &params = {});

  // ========== QuatumSimulationModel with a fixed (pre-defined) ansatz ========
  // The ansatz is provided as a QCOR kernel.
  template <typename... Args>
  static inline QuatumSimulationModel createModel(
      void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
                                     Args...),
      Observable *obs) {
    QuatumSimulationModel model;
    std::cout << "HOWDY\n";
    return model;
  }
};

// Quantum Simulation Workflow (Protocol)
// This can handle both variational workflow (optimization loop)
// as well as simple Trotter evolution workflow.
// Result is stored in a HetMap
using QsimResult = HeterogeneousMap;
using QuatumSimulationResult = HeterogeneousMap;
// Abstract workflow:
class QsimWorkflow : public Identifiable {
class QuatumSimulationWorkflow : public Identifiable {
public:
  virtual bool initialize(const HeterogeneousMap &params) = 0;
  virtual QsimResult execute(const QsimModel &model) = 0;
  virtual QuatumSimulationResult execute(const QuatumSimulationModel &model) = 0;

protected:
  CostFunctionEvaluator *evaluator;
};

// Get workflow by name:
std::shared_ptr<QsimWorkflow> getWorkflow(const std::string &name,
std::shared_ptr<QuatumSimulationWorkflow> getWorkflow(const std::string &name,
                                          const HeterogeneousMap &init_params);
} // namespace qcor
 No newline at end of file
+44 −0
Original line number Diff line number Diff line
set(LIBRARY_NAME qcor-quantum-simulation)
file(GLOB SRC *.cpp)
file(GLOB HEADERS *.hpp)

usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)

set(_bundle_name qcor_qsim)
add_library(${LIBRARY_NAME} SHARED ${SRC})

target_link_libraries(${LIBRARY_NAME} PUBLIC qcor qcor-qsim)
target_include_directories(${LIBRARY_NAME} PUBLIC ../base)
xacc_configure_library_rpath(${LIBRARY_NAME})

set_target_properties(${LIBRARY_NAME}
                      PROPERTIES COMPILE_DEFINITIONS
                                 US_BUNDLE_NAME=${_bundle_name}
                                 US_BUNDLE_NAME
                                 ${_bundle_name})

usfunctionembedresources(TARGET
                         ${LIBRARY_NAME}
                         WORKING_DIRECTORY
                         ${CMAKE_CURRENT_SOURCE_DIR}
                         FILES
												 manifest.json)
												 
if(APPLE)
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "@loader_path/../lib;${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "$ORIGIN/../lib:${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()

install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
install(FILES ${HEADERS} DESTINATION include/qcor)

# if (QCOR_BUILD_TESTS)
# 	add_subdirectory(tests)
# endif()
 No newline at end of file
Loading