Commit 3d56a88c authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

added more documentation, made it so you can manually provide...


added more documentation, made it so you can manually provide CompositeInstruction to ObjectiveFunction

Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 97446133
Loading
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
#include "qcor.hpp"

const std::string src = R"(__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) {

  // When manually writing kernel source strings
  // you have to specify the backend you are targeting
  qcor::set_backend("tnqvm");

  // 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");

  // JIT compile the ansatz
  auto ansatz = qcor::compile(src);

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

  // Evaluate the ObjectiveFunction at a specified set of parameters
  auto energy = (*objective)(q, .59);
  printf("vqe-energy = %f\n", energy);
}
+12 −8
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@
#include "Optimizer.hpp"

#include "xacc.hpp"
#include "xacc_service.hpp"
#include "xacc_quantum_gate_api.hpp"
#include "xacc_service.hpp"

#include "qalloc.hpp"

@@ -42,7 +42,8 @@ double observe(xacc::CompositeInstruction* program,
}
} // namespace __internal__

std::shared_ptr<xacc::Optimizer> createOptimizer(const char * type, HeterogeneousMap&& options) {
std::shared_ptr<xacc::Optimizer> createOptimizer(const char *type,
                                                 HeterogeneousMap &&options) {
  if (!xacc::isInitialized())
    xacc::internal_compiler::compiler_InitializeXACC();
  return xacc::getOptimizer(type, options);
@@ -54,5 +55,8 @@ std::shared_ptr<xacc::Observable> createObservable(const char *repr) {
  return xacc::quantum::getObservable("pauli", std::string(repr));
}

std::shared_ptr<xacc::CompositeInstruction> compile(const std::string &src) {
  return xacc::getCompiler("xasm")->compile(src)->getComposites()[0];
}

} // namespace qcor
 No newline at end of file
+30 −6
Original line number Diff line number Diff line
#ifndef RUNTIME_QCOR_HPP_
#define RUNTIME_QCOR_HPP_

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

@@ -43,8 +44,7 @@ double observe(xacc::CompositeInstruction *program,

// Observe the kernel and return the measured kernels
std::vector<std::shared_ptr<xacc::CompositeInstruction>>
observe(std::shared_ptr<Observable> obs,
        xacc::CompositeInstruction *program);
observe(std::shared_ptr<Observable> obs, xacc::CompositeInstruction *program);

// Get the objective function from the service registry
std::shared_ptr<ObjectiveFunction> get_objective(const char *type);
@@ -127,13 +127,25 @@ public:
      auto functor =
          reinterpret_cast<void (*)(ArgumentTypes...)>(pointer_to_functor);
      kernel = __internal__::kernel_as_composite_instruction(functor, args...);
    }

    if (!qreg.results()) {
      // this hasn't been set, so set it
      qreg = std::get<0>(std::forward_as_tuple(args...));
    }

    kernel->updateRuntimeArguments(args...);
    return operator()();
  }
};

void set_backend(const std::string &backend) {
  xacc::internal_compiler::compiler_InitializeXACC();
  xacc::internal_compiler::setAccelerator(backend.c_str());
}

std::shared_ptr<xacc::CompositeInstruction> compile(const std::string &src);

// Public observe function, returns expected value of Observable
template <typename QuantumKernel, typename... Args>
auto observe(QuantumKernel &kernel, std::shared_ptr<Observable> obs,
@@ -170,6 +182,18 @@ 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 = {}) {
  auto obj_func = qcor::__internal__::get_objective(obj_name);
  obj_func->initialize(observable, kernel.get());
  obj_func->set_options(options);
  return obj_func;
}

// Create an Objective Function that makes calls to the
// provided Quantum Kernel, with measurements dictated by
// the provided Observable. Optionally can provide problem-specific