Commit 125fe1e6 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

cleanup, fixed a few bugs, made base class for qcor ir functionpasses, tested qrt with taskinitiate

parent 34f82ade
Loading
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
#include "qcor.hpp"

__qpu__ void ansatz(qreg q, double theta, std::shared_ptr<xacc::Observable> H) {
  X(q[0]);
  exp_i_theta(q, theta, H);
}

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

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

  // Create the Ansatz exponent Operator
  auto ansatz_exponent = qcor::createObservable("X0 Y1 - Y0 X1");

  // Create an objective function to optimize
  // Each call evaluates E(theta) = <ansatz(theta) | H | ansatz(theta)>
  qcor::OptFunction opt_func(
      [&](const std::vector<double> &x, std::vector<double> &grad) -> double {
        
        // Affect Observable observation and evaluate at given ansatz parameters
        auto e = qcor::observe(ansatz, H, q, x[0], ansatz_exponent);

        // Need to clean the current qubit register
        q.reset();
        return e;
      },
      1);

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

  // Optimize the above function
  auto result = optimizer->optimize(opt_func);

  // Print the result
  printf("energy = %f\n", result.first);

  return 0;
}
+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);
}
+24 −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);

  // Evaluate the ObjectiveFunction at a specified set of parameters
  auto energy = (*objective)(q, .59);
  printf("vqe-energy = %f\n", energy);
}
+15 −9
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
#include "token_collector.hpp"
#include "xacc.hpp"
#include "xacc_service.hpp"
#include <limits>
#include <qalloc>

#include "AllGateVisitor.hpp"
@@ -55,7 +56,8 @@ run_token_collector(clang::Preprocessor &PP, clang::CachedTokens &Toks,

using namespace xacc::quantum;

class qrt_mapper : public AllGateVisitor, public xacc::InstructionVisitor<Circuit> {
class qrt_mapper : public AllGateVisitor,
                   public xacc::InstructionVisitor<Circuit> {
protected:
  std::stringstream ss;

@@ -108,7 +110,9 @@ public:
  void visit(U &u) override { addOneQubitGate("u", u); }
  void visit(Circuit &circ) override {
    if (circ.name() == "exp_i_theta") {
        ss << "quantum::exp(q"  << ", " << circ.getArguments()[0]->name << ", " << circ.getArguments()[1]->name << ");\n";
      ss << "quantum::exp(" << circ.getBufferNames()[0] << ", "
         << circ.getArguments()[0]->name << ", " << circ.getArguments()[1]->name
         << ");\n";
    }
  }
  void visit(IfStmt &ifStmt) override {}
@@ -153,9 +157,9 @@ void run_token_collector_llvm_rt(clang::Preprocessor &PP,
    }
    ss << terminating_char;

    std::cout << "COMPILING\n"
              << function_prototype + "{" + ss.str() + "}"
              << "\n";
    // std::cout << "COMPILING\n"
    //           << function_prototype + "{" + ss.str() + "}"
    //           << "\n";

    // FIXME, check canParse, and if not, then just write ss.str() to qrt_code
    // somehow
@@ -187,7 +191,10 @@ void run_token_collector_llvm_rt(clang::Preprocessor &PP,
        std::stringstream sss;
        for (auto &b : bufferNames) {
          // sss << "qreg " << b << "[100];\n";
          auto q = qalloc(100);
          // Note - we don't know the size of the buffer 
          // at this point, so just create one with max size 
          // and we can provide an IR Pass later that updates it
          auto q = qalloc(std::numeric_limits<int>::max());
          q.setNameAndStore(b.c_str());
        }
        extra_preamble += sss.str();
@@ -341,7 +348,6 @@ void run_token_collector_llvm_rt(clang::Preprocessor &PP,
                                  extra_preamble);
    {
      auto visitor = std::make_shared<qrt_mapper>();
      std::cout << "HELLO WORLD ACCEPTING: " << inst->name() << "\n";
      inst->accept(visitor);
      qrt_code << visitor->get_new_src();
    }
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ set_target_properties(print_llvm_qir PROPERTIES
    COMPILE_FLAGS "-fno-rtti"
)

target_include_directories(print_llvm_qir PRIVATE ..)
target_link_libraries(print_llvm_qir
                      PRIVATE ${LLVM_LIBS})

Loading