Commit 6207f596 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

removing ir generators

parent ea991de0
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
add_subdirectory(digital)
add_subdirectory(observable)

ir/digital/CMakeLists.txt

deleted100644 → 0
+0 −37
Original line number Diff line number Diff line
set(LIBRARY_NAME qcor-digital)

file(GLOB SRC generators/hwe/hwe.cpp *.cpp generators/exp/exp.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 . generators/hwe generators/exp)

target_link_libraries(${LIBRARY_NAME} PUBLIC xacc::xacc PRIVATE xacc::pauli xacc::fermion)

set(_bundle_name qcor_digital)
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)

xacc_configure_plugin_rpath(${LIBRARY_NAME})

if(QCOR_BUILD_TESTS)
  add_subdirectory(generators/hwe/tests)
  add_subdirectory(generators/exp/tests)
endif()

install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
+0 −25
Original line number Diff line number Diff line
#include "hwe.hpp"
#include "exp.hpp"

#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h"
#include "cppmicroservices/ServiceProperties.h"

using namespace cppmicroservices;

class US_ABI_LOCAL QCORDigitalActivator : public BundleActivator {
public:
  QCORDigitalActivator() {}

  void Start(BundleContext context) {
    auto hwe = std::make_shared<qcor::instructions::HWE>();
    auto expit = std::make_shared<qcor::instructions::Exp>();
    context.RegisterService<xacc::IRGenerator>(hwe);
    context.RegisterService<xacc::IRGenerator>(expit);

  }

  void Stop(BundleContext context) {}
};

CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(QCORDigitalActivator)

ir/digital/generators/exp/exp.cpp

deleted100644 → 0
+0 −158
Original line number Diff line number Diff line
#include "exp.hpp"
#include "FermionOperator.hpp"
#include "IRProvider.hpp"
#include "ObservableTransform.hpp"
#include "PauliOperator.hpp"

#include "XACC.hpp"
#include "xacc_service.hpp"
#include <memory>
#include <regex>

using namespace xacc;
using namespace xacc::quantum;

namespace qcor {
namespace instructions {
bool Exp::validateOptions() {
  if (options.count("pauli")) {
    return true;
  }
  if (options.count("fermion")) {
    return true;
  }
  return false;
}

std::shared_ptr<Function>
Exp::generate(std::shared_ptr<AcceleratorBuffer> buffer,
              std::vector<InstructionParameter> parameters) {
  xacc::error("qcor::Exp::generate(buffer,params) not implemented.");
  return nullptr;
}

std::shared_ptr<xacc::Function>
Exp::generate(std::map<std::string, xacc::InstructionParameter> &parameters) {
  if (!parameters.empty()) {
    options = parameters;
  }
  return generate(std::map<std::string, InstructionParameter>{});
}

std::shared_ptr<Function>
Exp::generate(std::map<std::string, InstructionParameter> &&parameters) {

  if (!parameters.empty()) {
    options = parameters;
  }

  std::string paramLetter = "t";
  if (options.count("param-id")) {
    paramLetter = options["param-id"].toString();
  }

  std::unordered_map<std::string, xacc::quantum::Term> terms;
  if (options.count("fermion")) {
    auto fermionStr = options["fermion"].toString();
    auto op = std::make_shared<FermionOperator>(fermionStr);
    terms = std::dynamic_pointer_cast<PauliOperator>(
                xacc::getService<ObservableTransform>("jw")->transform(op))
                ->getTerms();
  } else {
    auto pauliStr = options["pauli"].toString();
    PauliOperator op(pauliStr);
    terms = op.getTerms();
  }

  double pi = 3.1415926;
  auto gateRegistry = xacc::getService<IRProvider>("gate");
  auto function = gateRegistry->createFunction("temp", {}, {});

  for (auto spinInst : terms) {
    // Get the individual pauli terms
    auto termsMap = std::get<2>(spinInst.second);

    std::vector<std::pair<int, std::string>> terms;
    for (auto &kv : termsMap) {
      if (kv.second != "I" && !kv.second.empty()) {
        terms.push_back({kv.first, kv.second});
      }
    }

    if (!terms.empty()) {
    // The largest qubit index is on the last term
    int largestQbitIdx = terms[terms.size() - 1].first;

    for (int i = 0; i < terms.size(); i++) {

      auto qbitIdx = terms[i].first;
      auto gateName = terms[i].second;

      if (i < terms.size() - 1) {
        auto cnot = gateRegistry->createInstruction(
            "CNOT", std::vector<int>{qbitIdx, terms[i + 1].first});
        function->addInstruction(cnot);
      }

      if (gateName == "X") {
        auto hadamard =
            gateRegistry->createInstruction("H", std::vector<int>{qbitIdx});
        function->insertInstruction(0, hadamard);
      } else if (gateName == "Y") {
        auto rx =
            gateRegistry->createInstruction("Rx", std::vector<int>{qbitIdx});
        InstructionParameter p(pi / 2.0);
        rx->setParameter(0, p);
        function->insertInstruction(0, rx);
      }

      // Add the Rotation for the last term
      if (i == terms.size() - 1) {
        // FIXME DONT FORGET DIVIDE BY 2
        // std::stringstream ss;
        // ss << 2 * std::imag(std::get<0>(spinInst.second)) << " * "
        //    << std::get<1>(spinInst.second);

        auto rz = gateRegistry->createInstruction(
            "Rz", std::vector<int>{qbitIdx}, {std::to_string(std::real(spinInst.second.coeff()))+" * " + paramLetter});

        // InstructionParameter p(ss.str());
        // rz->setParameter(0, p);
        function->addInstruction(rz);
      }
    }
  }

    int counter = function->nInstructions();
    // Add the instruction on the backend of the circuit
    for (int i = terms.size() - 1; i >= 0; i--) {

      auto qbitIdx = terms[i].first;
      auto gateName = terms[i].second;

      if (i < terms.size() - 1) {
        auto cnot = gateRegistry->createInstruction(
            "CNOT", std::vector<int>{qbitIdx, terms[i + 1].first});
        function->insertInstruction(counter, cnot);
        counter++;
      }

      if (gateName == "X") {
        auto hadamard =
            gateRegistry->createInstruction("H", std::vector<int>{qbitIdx});
        function->addInstruction(hadamard);
      } else if (gateName == "Y") {
        auto rx =
            gateRegistry->createInstruction("Rx", std::vector<int>{qbitIdx});
        InstructionParameter p(-1.0 * (pi / 2.0));
        rx->setParameter(0, p);
        function->addInstruction(rx);
      }
    }
  }

  return function;
} // namespace instructions

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

ir/digital/generators/exp/exp.hpp

deleted100644 → 0
+0 −38
Original line number Diff line number Diff line
#ifndef QCOR_INSTRUCTIONS_EXP_HPP_
#define QCOR_INSTRUCTIONS_EXP_HPP_

#include "IRGenerator.hpp"

namespace xacc {
    class AcceleratorBuffer;
    class Function;
}

namespace qcor {
namespace instructions {
class Exp: public xacc::IRGenerator {
public:
	std::shared_ptr<xacc::Function> generate(
			std::shared_ptr<xacc::AcceleratorBuffer> buffer,
			std::vector<xacc::InstructionParameter> parameters = std::vector<
					xacc::InstructionParameter> { }) override;

	std::shared_ptr<xacc::Function> generate(
			std::map<std::string, xacc::InstructionParameter>& parameters) override;

	std::shared_ptr<xacc::Function> generate(
			std::map<std::string, xacc::InstructionParameter>&& parameters) override;

    bool validateOptions() override;

	const std::string name() const override {
		return "exp_i_theta";
	}

	const std::string description() const override {
		return "";
	}
};
}
}
#endif
 No newline at end of file
Loading