Commit 10282c12 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding simple quil token collector and example cpp file

parent 60af5b4f
Loading
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 0
  RY(theta) 1
  CNOT 1 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> = %.12f\n", results.opt_val);
}
+1 −0
Original line number Diff line number Diff line
@@ -27,3 +27,4 @@ install(TARGETS ${LIBRARY_NAME} DESTINATION lib)

add_subdirectory(xasm)
add_subdirectory(staq)
add_subdirectory(quil)
 No newline at end of file
+54 −0
Original line number Diff line number Diff line
# *******************************************************************************
# Copyright (c) 2019 UT-Battelle, LLC.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v.10 which accompany this distribution.
# The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# https://eclipse.org/org/documents/edl-v10.php
#
# Contributors:
#   Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
set(LIBRARY_NAME qcor-quil-token)

file(GLOB SRC *.cpp)

usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)

add_library(${LIBRARY_NAME} SHARED ${SRC})

target_include_directories(
  ${LIBRARY_NAME}
  PUBLIC . .. ${CLANG_INCLUDE_DIRS})

target_link_libraries(${LIBRARY_NAME} PUBLIC ${CLANG_LIBS} ${LLVM_LIBS} xacc::xacc)

set(_bundle_name qcor_quil_token)
set_target_properties(${LIBRARY_NAME}
                      PROPERTIES COMPILE_DEFINITIONS
                                 US_BUNDLE_NAME=${_bundle_name}
                                 US_BUNDLE_NAME
                                 ${_bundle_name})

usfunctionembedresources(TARGET
                         ${LIBRARY_NAME}
                         WORKING_DIRECTORY
                         ${CMAKE_CURRENT_SOURCE_DIR}
                         FILES
                         manifest.json)


if(APPLE)
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "@loader_path/../lib;${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "$ORIGIN/../lib:${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()

install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
+6 −0
Original line number Diff line number Diff line
{
  "bundle.symbolic_name" : "qcor_quil_token",
  "bundle.activator" : true,
  "bundle.name" : "QUIL Token Collector",
  "bundle.description" : ""
}
+75 −0
Original line number Diff line number Diff line
#include "quil_token_collector.hpp"

#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h"
#include "cppmicroservices/ServiceProperties.h"
#include "clang/Basic/TokenKinds.h"

#include "xacc.hpp"
#include <iostream>
#include <memory>
#include <set>
#include <xacc_service.hpp>

using namespace cppmicroservices;

namespace {

/**
 */
class US_ABI_LOCAL QuilTokenCollectorActivator : public BundleActivator {

public:
  QuilTokenCollectorActivator() {}

  /**
   */
  void Start(BundleContext context) {
    auto xt = std::make_shared<qcor::QuilTokenCollector>();
    context.RegisterService<qcor::TokenCollector>(xt);
    // context.RegisterService<xacc::OptionsProvider>(acc);
  }

  /**
   */
  void Stop(BundleContext /*context*/) {}
};

} // namespace

CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(QuilTokenCollectorActivator)

namespace qcor {

void QuilTokenCollector::collect(clang::Preprocessor &PP,
                                 clang::CachedTokens &Toks,
                                 std::stringstream &ss) {
  bool inForLoop = false;
  for (int i = 0; i < Toks.size() - 1; i++) {

    auto token = PP.getSpelling(Toks[i]);

    ss << token << " ";

    auto nextToken = PP.getSpelling(Toks[i + 1]);

    // The following instructions in Quil
    // have different capitalization in XACC.
    if (nextToken == "RX")
      nextToken = "Rx";
    if (nextToken == "RY")
      nextToken = "Ry";
    if (nextToken == "RZ")
      nextToken = "Rz";
    if (nextToken == "CX")
      nextToken = "CNOT";

    // If the next token is an Instruction, then add newline
    if (xacc::hasService<xacc::Instruction>(nextToken)) {
      ss << "\n";
    }
  }
  ss << PP.getSpelling(Toks[Toks.size() - 1]) << "\n";
}

} // namespace qcor
 No newline at end of file
Loading