Commit 19e132dd authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding qite to qcor_hybrid

parent e81fa520
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
#include "qcor_hybrid.hpp"
#include <iomanip>

// Define the state preparation kernel
__qpu__ void state_prep(qreg q) { X(q[0]); }

int main() {

  // Define the Hamiltonian using the QCOR API
  auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) -
           6.125 * Z(1);

  // Create a QITE instance, give it
  // the parameterized state_prep functor,
  // Observable, and n_steps and step_size
  QITE qite(state_prep, H, 5, .1);

  // Execute!
  auto energy = qite.execute();

  // Print the energy
  std::cout << std::setprecision(12) << energy << "\n";
}
+7 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
#include "Compiler.hpp"
#include "xacc.hpp"
#include "xacc_service.hpp"
#include <Utils.hpp>

namespace qcor {
namespace __internal__ {
@@ -28,4 +29,10 @@ void QAOA::initial_compile_qaoa_code() {
}
void QAOA::error(const std::string &message) { xacc::error(message); }


void execute_qite(qreg q, const HeterogeneousMap &&m) {
  auto qite = xacc::getAlgorithm("qite", m);
  qite->execute(xacc::as_shared_ptr(q.results()));
}

} // namespace qcor
 No newline at end of file
+41 −1
Original line number Diff line number Diff line
@@ -2,8 +2,10 @@

#include "AcceleratorBuffer.hpp"
#include "qcor.hpp"
#include "qcor_utils.hpp"
#include "qrt.hpp"
#include <memory>
#include <xacc_internal_compiler.hpp>

namespace qcor {
static constexpr double pi = 3.141592653589793238;
@@ -468,4 +470,42 @@ public:

// Next, add Adapt

void execute_qite(qreg q, const HeterogeneousMap &&m);
// Next, QITE
template <typename... KernelArgs> class QITE {
protected:
  Observable &observable;
  void *state_prep_ptr;
  const int n_steps;
  const double step_size;
  // Register of qubits to operate on
  qreg q;

public:
  QITE(void (*state_prep_kernel)(std::shared_ptr<CompositeInstruction>, qreg,
                                 KernelArgs...),
       Observable &obs, const int _n_steps, const double _step_size)
      : state_prep_ptr(reinterpret_cast<void *>(state_prep_kernel)),
        observable(obs), n_steps(_n_steps), step_size(_step_size) {
    q = qalloc(obs.nBits());
  }

  double execute(KernelArgs... initial_args) {
    auto state_prep_casted =
        reinterpret_cast<void (*)(std::shared_ptr<CompositeInstruction>, qreg,
                                  KernelArgs...)>(state_prep_ptr);
    auto parent_composite =
        qcor::__internal__::create_composite("qite_composite");
    state_prep_casted(parent_composite, q, initial_args...);
    // parent_composite now has the circuit in it

    auto accelerator = xacc::internal_compiler::get_qpu();
    execute_qite(q, {{"steps", n_steps},
                     {"step-size", step_size},
                     {"ansatz", parent_composite},
                     {"accelerator", accelerator},
                     {"observable", &observable}});
    return q.results()->getInformation("opt-val").template as<double>();
  }
};
} // namespace qcor