Commit d7572914 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

started work on adding taskInitiate back to runtime

parent 3d56a88c
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
#include "qcor.hpp"

__qpu__ void ansatz(qreg q, double theta) {
  X(q[0]);
  Ry(q[1], theta);
  CX(q[1], q[0]);
}


int main(int argc, char **argv) {
  // Allocate 2 qubits
  auto q = qalloc(2);

  // Create the Deuteron Hamiltonian (Observable)
  auto H = qcor::createObservable(
      "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1");

  // Create the ObjectiveFunction, here we want to run VQE
  // need to provide ansatz and the Observable
  auto objective = qcor::createObjectiveFunction("vqe", ansatz, H);

  // Create the Optimizer
  auto optimizer = qcor::createOptimizer("nlopt");

  // Call taskInitiate, kick off optimization of the give 
  // functor dependent on the ObjectiveFunction, async call
  auto handle = qcor::taskInitiate(
      objective, optimizer,
      [&](const std::vector<double> x, std::vector<double> &dx) {
        return (*objective)(q, x[0]);
      },
      1);

  // Go do other work...

  // Query results when ready.
  auto results = qcor::sync(handle);

  // Print the optimal value.
  printf("<H> = %f\n", results.opt_val);
}
+0 −2
Original line number Diff line number Diff line
@@ -23,8 +23,6 @@ int main(int argc, char **argv) {

  // Create the ObjectiveFunction, here we want to run VQE
  // need to provide ansatz and the Observable
  // Must also provide initial params for ansatz (under the hood, uses
  // variadic template)
  auto objective = qcor::createObjectiveFunction("vqe", ansatz, H);


+0 −2
Original line number Diff line number Diff line
@@ -35,8 +35,6 @@ int main(int argc, char **argv) {

  // Create the ObjectiveFunction, here we want to run VQE
  // need to provide ansatz and the Observable
  // Must also provide initial params for ansatz (under the hood, uses 
  // variadic template)
  auto objective = qcor::createObjectiveFunction("vqe", ansatz, H);

  // Evaluate the ObjectiveFunction at a specified set of parameters
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ TEST(VQETester, checkSimple) {

  auto vqe = xacc::getService<qcor::ObjectiveFunction>("vqe");
  vqe->initialize(observable, ruccsd.get());
  vqe->set_results_buffer(buffer);
  vqe->set_qreg(buffer);

  qcor::OptFunction f(
      [&](const std::vector<double> x, std::vector<double> &grad) {
+36 −8
Original line number Diff line number Diff line
#ifndef RUNTIME_QCOR_HPP_
#define RUNTIME_QCOR_HPP_

#include <CompositeInstruction.hpp>
#include <future>
#include <memory>
#include <qalloc>

#include "CompositeInstruction.hpp"
#include "Observable.hpp"
#include "Optimizer.hpp"

#include "qalloc"
#include "xacc_internal_compiler.hpp"

namespace qcor {

using OptFunction = xacc::OptFunction;
using HeterogeneousMap = xacc::HeterogeneousMap;
using ResultsBuffer = xacc::internal_compiler::qreg;
using Observable = xacc::Observable;
using Optimizer = xacc::Optimizer;

class ResultsBuffer {
public:
  xacc::internal_compiler::qreg q_buffer;
  double opt_val;
  std::vector<double> opt_params;
};

using Handle = std::future<ResultsBuffer>;
ResultsBuffer sync(Handle& handle) {
    return handle.get();
}

void set_verbose(bool verbose);

@@ -79,7 +92,7 @@ protected:
  xacc::CompositeInstruction *kernel;

  // The buffer containing all execution results
  ResultsBuffer qreg;
  xacc::internal_compiler::qreg qreg;

  HeterogeneousMap options;

@@ -116,7 +129,8 @@ public:
  void set_options(HeterogeneousMap &opts) { options = opts; }

  // Set the results buffer
  void set_results_buffer(ResultsBuffer q) { qreg = q; }
  void set_qreg(xacc::internal_compiler::qreg q) { qreg = q; }
  xacc::internal_compiler::qreg get_qreg() { return qreg; }

  // Evaluate this Objective function at the give parameters.
  // These variadic parameters must mirror the provided
@@ -182,9 +196,6 @@ createOptimizer(const char *type, HeterogeneousMap &&options = {});
// Create an observable from a string representation
std::shared_ptr<Observable> createObservable(const char *repr);

// template <typename T> struct is_shared_ptr : std::false_type {};
// template <typename T>
// struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
std::shared_ptr<ObjectiveFunction> createObjectiveFunction(
    const char *obj_name, std::shared_ptr<xacc::CompositeInstruction> kernel,
    std::shared_ptr<Observable> observable, HeterogeneousMap &&options = {}) {
@@ -212,6 +223,23 @@ createObjectiveFunction(const char *obj_name, QuantumKernel &kernel,
  return obj_func;
}

Handle taskInitiate(
    std::shared_ptr<ObjectiveFunction> objective,
    std::shared_ptr<Optimizer> optimizer,
    std::function<double(const std::vector<double>, std::vector<double> &)>
        &&opt_function,
    const int nParameters) {
  return std::async(std::launch::async, [=]() -> ResultsBuffer {
    qcor::OptFunction f(opt_function, nParameters);
    auto results = optimizer->optimize(f);
    ResultsBuffer rb;
    rb.q_buffer = objective->get_qreg();
    rb.opt_params = results.second;
    rb.opt_val = results.first;
    return rb;
  });
}

} // namespace qcor

#endif
Loading