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

Added execution test for gradients



Compile and run ObjectiveFunction with gradients

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent ad3396c6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ add_test(NAME qrt_simple-demo COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURREN
add_test(NAME qrt_deuteron_exp_inst COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/deuteron/deuteron_exp_inst.cpp)
add_test(NAME qrt_deuteron_task_initiate COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/deuteron/deuteron_task_initiate.cpp)
add_test(NAME qrt_qaoa_example COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/qaoa/qaoa_example.cpp)
add_test(NAME qrt_simple-objective-function COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/simple/simple-objective-function.cpp)
add_test(NAME qrt_qpe_example COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/qpe/qpe_example_qrt.cpp)
add_test(NAME qrt_kernel_include COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/simple/multiple_kernels.cpp)
add_test(NAME qrt_grover COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/grover/grover.cpp)
@@ -40,6 +39,7 @@ add_test(NAME quasimo_verified_qpe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURR
add_test(NAME hadamard_ctrl_test COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/ctrl-gates/simple_hadamard_test.cpp)
add_test(NAME multi_ctrl_test COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/ctrl-gates/multiple_controls.cpp)

add_qcor_compile_and_exe_test(qrt_obj_func_simple simple/simple-objective-function.cpp)
add_qcor_compile_and_exe_test(qrt_bell_ctrl bell/bell_control.cpp)

# Lambda tests
+2 −3
Original line number Diff line number Diff line
@@ -25,9 +25,8 @@ int main(int argc, char **argv) {

  // Create the ObjectiveFunction, here we want to run VQE
  // need to provide ansatz, Operator, and qreg
  auto objective = createObjectiveFunction(
      ansatz, H, q, n_variational_params,
      {{"gradient-strategy", "parameter-shift"}});
  auto objective = createObjectiveFunction(ansatz, H, q, n_variational_params,
                                           {{"gradient-strategy", "central"}});

  // Create the Optimizer.
  auto optimizer = createOptimizer("nlopt", {{"nlopt-optimizer", "l-bfgs"}});
+1 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ file(GLOB HEADERS qcor.hpp
                  optimizer/qcor_optimizer.hpp 
                  kernel/quantum_kernel.hpp 
                  objectives/objective_function.hpp 
                  objectives/gradient_function.hpp
                  execution/taskInitiate.hpp
                  utils/qcor_utils.hpp)
                  
+1 −0
Original line number Diff line number Diff line
@@ -124,4 +124,5 @@ public:
  }
  void Stop(BundleContext /*context*/) {}
};
CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(FiniteDiffActivator)
} // namespace
+8 −9
Original line number Diff line number Diff line
@@ -230,6 +230,13 @@ public:

  // Construct the kernel evaluator of this ObjectiveFunctionImpl
  std::function<std::shared_ptr<CompositeInstruction>(std::vector<double>)> get_kernel_evaluator() override {
    // Turn kernel evaluation into a functor that we can use here
    // and share with the helper ObjectiveFunction for gradient evaluation
    std::function<std::shared_ptr<CompositeInstruction>(std::vector<double>)>
        kernel_evaluator = lambda_kernel_evaluator.has_value()
                               ? lambda_kernel_evaluator.value()
                               : [&](std::vector<double> x)
        -> std::shared_ptr<CompositeInstruction> {
      // Define a function pointer type for the quantum kernel
      void (*kernel_functor)(std::shared_ptr<CompositeInstruction>,
                             KernelArgs...);
@@ -238,14 +245,6 @@ public:
        kernel_functor = reinterpret_cast<void (*)(
            std::shared_ptr<CompositeInstruction>, KernelArgs...)>(kernel_ptr);
      }

    // Turn kernel evaluation into a functor that we can use here
    // and share with the helper ObjectiveFunction for gradient evaluation
    std::function<std::shared_ptr<CompositeInstruction>(std::vector<double>)>
        kernel_evaluator = lambda_kernel_evaluator.has_value()
                               ? lambda_kernel_evaluator.value()
                               : [&](std::vector<double> x)
        -> std::shared_ptr<CompositeInstruction> {
      // Create a new CompositeInstruction, and create a tuple
      // from it so we can concatenate with the tuple args
      auto m_kernel = create_new_composite();