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

updates to simple obj func example

parent e480c8da
Loading
Loading
Loading
Loading
Loading
+32 −5
Original line number Diff line number Diff line
// Note no includes here, we are just 
// using the language extension
//
// run this with 
// qcor -qpu tnqvm simple-objective-function.cpp
// ./a.out

__qpu__ void ansatz(qreg q, double theta) {
  X(q[0]);
@@ -10,14 +16,35 @@ int main(int argc, char **argv) {
  auto q = qalloc(2);

  // Create the Deuteron Hamiltonian (Observable)
  auto H = qcor::createObservable(
  auto H = 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);
  auto objective = createObjectiveFunction("vqe", ansatz, H);

  // Create the Optimizer. This will give us COBYLA from nlopt
  auto optimizer = createOptimizer("nlopt");

  // Launch the Optimization Task with taskInitiate
  auto handle = taskInitiate(
      objective, optimizer, // Provide the ObjectiveFunction and Optimizer
      // Need to provide a way to map vector<double> x to ansatz(qreg, double)
      // QCOR provides the TranslationFunctor for this
      TranslationFunctor<qreg, double>([&](const std::vector<double> x) {
        return std::make_tuple(q, x[0]);
      }),
      // Need to specify number of parameters in x
      // because qcor has to instantiate that vector
      1);

  // Go do other work...

  // Query results when ready.
  auto results = qcor::sync(handle);
  printf("vqe-energy from taskInitiate = %f\n", results.opt_val);

  // Evaluate the ObjectiveFunction at a specified set of parameters
  auto energy = (*objective)(q, .59);
  printf("vqe-energy = %f\n", energy);
  auto energy = (*objective)(q, results.opt_params[0]);
  printf("vqe-energy just evaluating objective function = %f\n", energy);
}
+11 −6
Original line number Diff line number Diff line
@@ -3,9 +3,9 @@
#include "CompositeInstruction.hpp"
#include "IRProvider.hpp"
#include "IRTransformation.hpp"
#include "Optimizer.hpp"
#include "qrt.hpp"
#include "xacc_internal_compiler.hpp"
#include "Optimizer.hpp"

#include <Eigen/Dense>
#include <functional>
@@ -100,13 +100,18 @@ get_transformation(const std::string &transform_type);
// return the IR Provider
std::shared_ptr<qcor::IRProvider> get_provider();

// Decompose the given unitary matrix with the specified decomposition algorithm.
// Decompose the given unitary matrix with the specified decomposition
// algorithm.
std::shared_ptr<qcor::CompositeInstruction>
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat, const std::string buffer_name);
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat,
                  const std::string buffer_name);

// Decompose the given unitary matrix with the specified decomposition algorithm and optimizer
// Decompose the given unitary matrix with the specified decomposition algorithm
// and optimizer
std::shared_ptr<qcor::CompositeInstruction>
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat, const std::string buffer_name, std::shared_ptr<xacc::Optimizer> optimizer);
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat,
                  const std::string buffer_name,
                  std::shared_ptr<xacc::Optimizer> optimizer);

// Utility for calling a Functor via mapping a tuple of Args to
// a sequence of Args...