Commit 4dcfa3f0 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

initial rewrite of runtime library to move xacc deps from public qcor.hpp API using pimpl idiom

parent e401f95e
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -9,18 +9,21 @@ int main(int argc, char **argv) {
  // Allocate 2 qubits
  auto q = qalloc(2);

  ansatz::print_kernel(q, 2.2);
  // Create the Deuteron Hamiltonian
  auto H = createObservable(
      "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1");

  qcor::set_verbose(true);
  // Create the objective function
  auto objective = createObjectiveFunction(ansatz, H, q, 1);

  print(objective->operator()(std::vector<double>{2.2}));
  // Create a qcor Optimizer
  auto optimizer = createOptimizer("nlopt");

  // Optimize the above function
  auto [optval, opt_params] = optimizer->optimize(*objective.get());
  auto [optval, opt_params] = optimizer->optimize(objective);

  // Print the result
  printf("energy = %f\n", optval);
+5 −6
Original line number Diff line number Diff line
@@ -19,18 +19,17 @@ int main() {
    CX(q[1], q[0]);
  });

  OptFunction opt_function(
      [&](std::vector<double> x) { return ansatz.observe(H, qalloc(2), x[0]); },
      1);

  OptFunction opt_function_vec(
   ObjectiveFunction opt_function_vec(
      [&](std::vector<double> x) {
        return ansatz_take_vec.observe(H, qalloc(2), x);
      },
      1);

  // Show off optimize from ObjectiveFunction rvalue
  auto optimizer = createOptimizer("nlopt");
  auto [ground_energy, opt_params] = optimizer->optimize(opt_function);
  auto [ground_energy, opt_params] = optimizer->optimize(ObjectiveFunction(
      [&](std::vector<double> x) { return ansatz.observe(H, qalloc(2), x[0]); },
      1));
  print("Energy: ", ground_energy);
  qcor_expect(std::abs(ground_energy + 1.74886) < 0.1);

+2 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ int main() {
  auto optimizer = createOptimizer("nlopt");

  // Optimize the above function
  auto [optval, opt_params] = optimizer->optimize(*objective.get());
  auto [optval, opt_params] = optimizer->optimize(objective);
  std::cout << "Energy: " << optval << "\n";
  qcor_expect(std::abs(optval + 1.74886) < 0.1);

@@ -35,7 +35,7 @@ int main() {
  auto objective_vec = createObjectiveFunction(ansatz_vec_param, H, q1, 1);

  // Optimize the above function
  auto [optval_vec, opt_params_vec] = optimizer->optimize(*objective_vec.get());
  auto [optval_vec, opt_params_vec] = optimizer->optimize(objective_vec);
  std::cout << "Energy: " << optval_vec << "\n";
  qcor_expect(std::abs(optval_vec + 1.74886) < 0.1);
}
 No newline at end of file
+11 −6
Original line number Diff line number Diff line
@@ -8,13 +8,16 @@ set(LIBRARY_NAME qcor)
file(GLOB SRC observable/qcor_observable.cpp 
              optimizer/qcor_optimizer.cpp 
              objectives/objective_function.cpp
              objectives/gradient_function.cpp
              execution/taskInitiate.cpp
              utils/qcor_utils.cpp)
              kernel/quantum_kernel.cpp
              utils/qcor_utils.cpp
              objectives/gradient_function.cpp)

              #execution/taskInitiate.cpp
              #utils/qcor_types.cpp)

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

target_include_directories(${LIBRARY_NAME} PUBLIC . qrt qrt/internal_compiler
target_include_directories(${LIBRARY_NAME} PUBLIC . objectives qrt qrt/internal_compiler qrt/ir ${XACC_ROOT}/include/spdlog
                            observable 
                            optimizer 
                            jit
@@ -41,15 +44,17 @@ file(GLOB HEADERS qcor.hpp
                  optimizer/qcor_optimizer.hpp 
                  kernel/quantum_kernel.hpp 
                  objectives/objective_function.hpp 
                  objectives/gradient_function.hpp
                  ojectives/gradient_function.hpp
                  execution/taskInitiate.hpp
                  utils/qcor_utils.hpp)
                  utils/qcor_utils.hpp
                  utils/qcor_pimpl.hpp)
                  
install(FILES ${HEADERS} DESTINATION include/qcor)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)

if (QCOR_BUILD_TESTS)
  add_subdirectory(tests)
  add_subdirectory(observable/tests)
endif()

add_subdirectory(objectives)
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ add_library(${LIBRARY_NAME}
            ${CMAKE_BINARY_DIR}/runtime/jit/qcor_jit.cpp)

target_include_directories(${LIBRARY_NAME}
                           PUBLIC . ../qrt ${CMAKE_SOURCE_DIR}/runtime/qrt/internal_compiler ${XACC_ROOT}/include/xacc ${CMAKE_SOURCE_DIR}/handlers
                           PUBLIC . ../qrt ../utils ../qrt/ir ${CMAKE_SOURCE_DIR}/runtime/qrt/internal_compiler ${XACC_ROOT}/include/xacc ${CMAKE_SOURCE_DIR}/handlers
                                  ${CLANG_INCLUDE_DIRS}
                                  ${LLVM_INCLUDE_DIRS})

Loading