Commit 764ba1da authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

saving work done on removing gate registry, this branch is broken


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 420a6ea4
......@@ -18,6 +18,7 @@
#include "GateQIR.hpp"
#include <boost/math/constants/constants.hpp>
#include "XACC.hpp"
#include "GateInstructionService.hpp"
using namespace xacc;
......@@ -31,6 +32,8 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
int counter = 0;
auto pi = boost::math::constants::pi<double>();
GateInstructionService gateRegistry;
// Populate GateQIR now...
for (auto& inst : terms) {
......@@ -44,7 +47,7 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
// Loop over all terms in the Spin Instruction
// and create instructions to run on the Gate QPU.
std::vector<std::shared_ptr<xacc::quantum::GateInstruction>> measurements;
std::vector<std::shared_ptr<xacc::Instruction>> measurements;
std::vector<std::pair<int, std::string>> terms;
for (auto& kv : spinInst) {
......@@ -56,20 +59,18 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
for (int i = terms.size() - 1; i >= 0; i--) {
auto qbit = terms[i].first;
auto gateName = terms[i].second;
auto gateRegistry =
xacc::quantum::GateInstructionRegistry::instance();
auto meas = gateRegistry->create("Measure",
auto meas = gateRegistry.create("Measure",
std::vector<int> { qbit });
xacc::InstructionParameter classicalIdx(qbit);
meas->setParameter(0, classicalIdx);
measurements.push_back(meas);
if (gateName == "X") {
auto hadamard = gateRegistry->create("H", std::vector<int> {
auto hadamard = gateRegistry.create("H", std::vector<int> {
qbit });
gateFunction->addInstruction(hadamard);
} else if (gateName == "Y") {
auto rx = gateRegistry->create("Rx", std::vector<int> { qbit });
auto rx = gateRegistry.create("Rx", std::vector<int> { qbit });
InstructionParameter p(pi / 2.0);
rx->setParameter(0, p);
gateFunction->addInstruction(rx);
......@@ -99,6 +100,7 @@ public:
BOOST_AUTO_TEST_CASE(checkSimple) {
xacc::Initialize();
// (-2.143303525+0j)*X0*X1 + (-3.91311896+0j)*X1*X2 +
// (-2.143303525+0j)*Y0*Y1 + (-3.91311896+0j)*Y1*Y2 + (0.218290555+0j)*Z0 + (-6.125+0j)*Z1 + (-9.625+0j)*Z2
// needs x0, x1, x2, y0, y1, y2
......@@ -135,4 +137,5 @@ BOOST_AUTO_TEST_CASE(checkSimple) {
BOOST_VERIFY(1.0 == fixedBuffers[i]->getExpectationValueZ());
}
xacc::Finalize();
}
......@@ -25,7 +25,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(rx.toString("qreg") == "Rx(3.14) qreg0");
BOOST_VERIFY(rx.bits().size() == 1);
BOOST_VERIFY(rx.bits()[0] == 0);
BOOST_VERIFY(rx.getName() == "Rx");
BOOST_VERIFY(rx.name() == "Rx");
Rx rx2(44, 1.71234);
......@@ -33,16 +33,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(rx2.toString("qreg") == "Rx(1.71234) qreg44");
BOOST_VERIFY(rx2.bits().size() == 1);
BOOST_VERIFY(rx2.bits()[0] == 44);
BOOST_VERIFY(rx2.getName() == "Rx");
BOOST_VERIFY(rx2.name() == "Rx");
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
xacc::InstructionParameter p = 3.1415;
auto rx = GateInstructionRegistry::instance()->create("Rx", std::vector<int>{0});
rx->setParameter(0, p);
BOOST_VERIFY(rx->getName() == "Rx");
BOOST_VERIFY(boost::get<double>(rx->getParameter(0)) == 3.1415);
}
......@@ -25,7 +25,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(ry.toString("qreg") == "Ry(3.14) qreg0");
BOOST_VERIFY(ry.bits().size() == 1);
BOOST_VERIFY(ry.bits()[0] == 0);
BOOST_VERIFY(ry.getName() == "Ry");
BOOST_VERIFY(ry.name() == "Ry");
Ry ry2(44, 1.71234);
......@@ -33,16 +33,8 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(ry2.toString("qreg") == "Ry(1.71234) qreg44");
BOOST_VERIFY(ry2.bits().size() == 1);
BOOST_VERIFY(ry2.bits()[0] == 44);
BOOST_VERIFY(ry2.getName() == "Ry");
BOOST_VERIFY(ry2.name() == "Ry");
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
xacc::InstructionParameter p = 3.1415;
auto ry = GateInstructionRegistry::instance()->create("Ry", std::vector<int>{0});
ry->setParameter(0, p);
BOOST_VERIFY(ry->getName() == "Ry");
BOOST_VERIFY(boost::get<double>(ry->getParameter(0)) == 3.1415);
}
......@@ -25,7 +25,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(rz.toString("qreg") == "Rz(3.14) qreg0");
BOOST_VERIFY(rz.bits().size() == 1);
BOOST_VERIFY(rz.bits()[0] == 0);
BOOST_VERIFY(rz.getName() == "Rz");
BOOST_VERIFY(rz.name() == "Rz");
Rz rz2(44, 1.71234);
......@@ -33,16 +33,8 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(rz2.toString("qreg") == "Rz(1.71234) qreg44");
BOOST_VERIFY(rz2.bits().size() == 1);
BOOST_VERIFY(rz2.bits()[0] == 44);
BOOST_VERIFY(rz2.getName() == "Rz");
BOOST_VERIFY(rz2.name() == "Rz");
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
xacc::InstructionParameter p = 3.1415;
auto rz = GateInstructionRegistry::instance()->create("Rz", std::vector<int>{0});
rz->setParameter(0, p);
BOOST_VERIFY(rz->getName() == "Rz");
BOOST_VERIFY(boost::get<double>(rz->getParameter(0)) == 3.1415);
}
......@@ -25,7 +25,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(sw.bits().size() == 2);
BOOST_VERIFY(sw.bits()[0] == 0);
BOOST_VERIFY(sw.bits()[1] == 1);
BOOST_VERIFY(sw.getName() == "Swap");
BOOST_VERIFY(sw.name() == "Swap");
Swap sw2(44, 45);
......@@ -33,12 +33,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(sw2.bits().size() == 2);
BOOST_VERIFY(sw2.bits()[0] == 44);
BOOST_VERIFY(sw2.bits()[1] == 45);
BOOST_VERIFY(sw2.getName() == "Swap");
BOOST_VERIFY(sw2.name() == "Swap");
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto sw = GateInstructionRegistry::instance()->create("Swap", std::vector<int>{0, 1});
BOOST_VERIFY(sw->getName() == "Swap");
}
......@@ -24,22 +24,12 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(x.toString("qreg") == "X qreg0");
BOOST_VERIFY(x.bits().size() == 1);
BOOST_VERIFY(x.bits()[0] == 0);
BOOST_VERIFY(x.getName() == "X");
BOOST_VERIFY(x.name() == "X");
X x2(44);
BOOST_VERIFY(x2.toString("qreg") == "X qreg44");
BOOST_VERIFY(x2.bits().size() == 1);
BOOST_VERIFY(x2.bits()[0] == 44);
BOOST_VERIFY(x2.getName() == "X");
BOOST_VERIFY(x2.name() == "X");
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto x = GateInstructionRegistry::instance()->create("X", std::vector<int> {
0 });
BOOST_VERIFY(x->toString("qreg") == "X qreg0");
BOOST_VERIFY(x->bits().size() == 1);
BOOST_VERIFY(x->bits()[0] == 0);
BOOST_VERIFY(x->getName() == "X");
}
......@@ -24,22 +24,12 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(y.toString("qreg") == "Y qreg0");
BOOST_VERIFY(y.bits().size() == 1);
BOOST_VERIFY(y.bits()[0] == 0);
BOOST_VERIFY(y.getName() == "Y");
BOOST_VERIFY(y.name() == "Y");
Y y2(44);
BOOST_VERIFY(y2.toString("qreg") == "Y qreg44");
BOOST_VERIFY(y2.bits().size() == 1);
BOOST_VERIFY(y2.bits()[0] == 44);
BOOST_VERIFY(y2.getName() == "Y");
BOOST_VERIFY(y2.name() == "Y");
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto y = GateInstructionRegistry::instance()->create("Y", std::vector<int> {
0 });
BOOST_VERIFY(y->toString("qreg") == "Y qreg0");
BOOST_VERIFY(y->bits().size() == 1);
BOOST_VERIFY(y->bits()[0] == 0);
BOOST_VERIFY(y->getName() == "Y");
}
......@@ -24,21 +24,13 @@ BOOST_AUTO_TEST_CASE(czeckCreation) {
BOOST_VERIFY(z.toString("qreg") == "Z qreg0");
BOOST_VERIFY(z.bits().size() == 1);
BOOST_VERIFY(z.bits()[0] == 0);
BOOST_VERIFY(z.getName() == "Z");
BOOST_VERIFY(z.name() == "Z");
Z z2(44);
BOOST_VERIFY(z2.toString("qreg") == "Z qreg44");
BOOST_VERIFY(z2.bits().size() == 1);
BOOST_VERIFY(z2.bits()[0] == 44);
BOOST_VERIFY(z2.getName() == "Z");
BOOST_VERIFY(z2.name() == "Z");
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto z = GateInstructionRegistry::instance()->create("Z", std::vector<int>{0});
BOOST_VERIFY(z->bits().size() == 1);
BOOST_VERIFY(z->bits()[0] == 0);
BOOST_VERIFY(z->getName() == "Z");
}
......@@ -65,7 +65,7 @@ public:
writer->StartObject();
writer->String("function");
writer->String(f->getName());
writer->String(f->name());
// All functions have instructions, start
// that array here.
......@@ -95,7 +95,7 @@ public:
// // This is a Function, start it as an Object
// writer->StartObject();
// writer->String("function");
// writer->String(function->getName());
// writer->String(function->name());
//
// // All functions have instructions, start
// // that array here.
......@@ -194,7 +194,7 @@ public:
void visit(ConditionalFunction& cn) {
writer->StartObject();
writer->String("conditional_function");
writer->String(cn.getName());
writer->String(cn.name());
writer->String("conditional_qubit");
writer->Int(cn.getConditionalQubit());
......@@ -248,7 +248,7 @@ protected:
void baseGateInst(GateInstruction& inst, bool endObject = true) {
writer->StartObject();
writer->String("gate");
writer->String(inst.getName().c_str());
writer->String(inst.name().c_str());
writer->String("enabled");
writer->Bool(inst.isEnabled());
writer->String("qubits");
......
/*******************************************************************************
* Copyright (c) 2017 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 v1.0 which accompanies 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
*******************************************************************************/
#ifndef XACC_UTILS_REGISTRY_HPP_
#define XACC_UTILS_REGISTRY_HPP_
#include "Singleton.hpp"
#include "XACC.hpp"
#include "RuntimeOptions.hpp"
#include <map>
#include <iostream>
#include <boost/program_options.hpp>
using namespace boost::program_options;
namespace xacc {
/**
* Registry is a Singleton that provides a
* mapping of string ids to creation functions that
* create and return the provided Registry template
* parameter T.
*
* Clients can add new creation functions to be placed
* in the map with a unique name key, and can request
* that the Registry return a new created instance of
* the template parameter T.
*
*/
template<typename T, typename... TArgs>
class Registry : public Singleton<Registry<T, TArgs...>> {
public:
using CreatorFunction = std::function<std::shared_ptr<T>(TArgs...)>;
using CreatorFunctionPtr = std::shared_ptr<CreatorFunction>;
/**
* Add a new creation function to the Registry, keyed
* on the provided string id.
*
* @param id The Id of the creation function
* @param f The object's creation function
* @return success Bool indicating if this creator was added successfully.
*/
bool add(const std::string& id,
CreatorFunctionPtr f) {
if (registry.find(id) != registry.end()) {
XACCLogger::instance()->info(id + " already exists in Registry. Ignoring and retaining previous Registry entry");
return true;
}
if (RuntimeOptions::instance()->exists("verbose-registry")) XACCLogger::instance()->info("Registry adding " + id);
bool s = registry.emplace(std::make_pair(id, f)).second;
if (s) {
return true;
}
xacc::error("Could not add " + id + " to the Registry.");
return false;
}
/**
* Add a new creation function to the Registry, keyed
* on the provided string id, and related
* command line options for the object to be created.
*
* @param id The Id of the creation function
* @param f The object's creation function
* @param options The objects command line options
* @return success Bool indicating if this creator was added successfully.
*/
bool add(const std::string& id,
CreatorFunctionPtr f, std::shared_ptr<options_description> options) {
if (registry.find(id) != registry.end()) {
return true;
}
if (RuntimeOptions::instance()->exists("verbose-registry")) XACCLogger::instance()->info("Registry adding " + id);
bool s = registry.emplace(std::make_pair(id, f)).second;
bool s2 = registryOptions.insert(std::make_pair(id, std::move(options))).second;
if (!s || ! s2) {
xacc::error("Could not add " + id + " to the Registry.");
return false;
} else {
return true;
}
}
bool add(const std::string& id, CreatorFunctionPtr f,
std::shared_ptr<options_description> options,
std::function<bool(variables_map&)> optionsHandler) {
if (registry.find(id) != registry.end()) {
XACCLogger::instance()->info(
id
+ " already exists in Registry. Ignoring and retaining previous Registry entry");
return true;
}
if (RuntimeOptions::instance()->exists("verbose-registry"))
XACCLogger::instance()->info("Registry adding " + id);
bool s = registry.emplace(std::make_pair(id, f)).second;
bool s2 =
registryOptions.insert(std::make_pair(id, std::move(options))).second;
bool s3 = registryOptionHandlers.insert(std::make_pair(id, std::move(optionsHandler))).second;
if (!s || !s2) {
xacc::error("Could not add " + id + " to the Registry.");
return false;
} else {
return true;
}
}
/**
* Create an instance of T by using the creation
* function found at the given key string id.
*
* @param id The Id of the creation function
* @return object Shared Pointer for the created object.
*/
std::shared_ptr<T> create(const std::string& id, TArgs... args) {
auto search = registry.find(id);
if (search != registry.end()) {
return registry[id]->operator()(args...);
} else {
xacc::error("Invalid Registry map id string - " + id);
return std::make_shared<T>(args...);
}
}
/**
* Return the keys from the registry map.
*
* @return ids The registered creator Ids
*/
std::vector<std::string> getRegisteredIds() {
std::vector<std::string> keys;
std::transform(registry.begin(), registry.end(),
std::back_inserter(keys),
std::bind(
&std::map<std::string,
CreatorFunctionPtr>::value_type::first,
std::placeholders::_1));
return keys;
}
std::vector<std::shared_ptr<options_description>> getRegisteredOptions() {
std::vector<std::shared_ptr<options_description>> values;
auto getSecond = [](const std::pair<std::string, std::shared_ptr<options_description>>& key_val) {
return key_val.second;
};
std::transform(registryOptions.begin(), registryOptions.end(), std::back_inserter(values),
getSecond);
return values;
}
bool handleOptions(variables_map& map) {
for (auto& kv : registryOptionHandlers) {
if (kv.second(map)) {
return true;
}
}
return false;
}
/**
* Return the number of creation functions
* this registry contains.
*
* @return size The number of creators in this Registry
*/
std::size_t size() {
return registry.size();
}
protected:
/**
* Reference to the database of creation functions
* for classes of superclass type T.
*/
std::map<std::string, CreatorFunctionPtr> registry;
std::map<std::string, std::shared_ptr<options_description>> registryOptions;
std::map<std::string, std::function<bool(variables_map&)>> registryOptionHandlers;
};
}
#endif
///*******************************************************************************
// * Copyright (c) 2017 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 v1.0 which accompanies 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
// *******************************************************************************/
//#ifndef XACC_UTILS_REGISTRY_HPP_
//#define XACC_UTILS_REGISTRY_HPP_
//
//#include "Singleton.hpp"
//#include "XACC.hpp"
//#include "RuntimeOptions.hpp"
//#include <map>
//#include <iostream>
//#include <boost/program_options.hpp>
//
//using namespace boost::program_options;
//
//namespace xacc {
//
///**
// * Registry is a Singleton that provides a
// * mapping of string ids to creation functions that
// * create and return the provided Registry template
// * parameter T.
// *
// * Clients can add new creation functions to be placed
// * in the map with a unique name key, and can request
// * that the Registry return a new created instance of
// * the template parameter T.
// *
// */
//template<typename T, typename... TArgs>
//class Registry : public Singleton<Registry<T, TArgs...>> {
//
//public:
//
// using CreatorFunction = std::function<std::shared_ptr<T>(TArgs...)>;
// using CreatorFunctionPtr = std::shared_ptr<CreatorFunction>;
//
// /**
// * Add a new creation function to the Registry, keyed
// * on the provided string id.
// *
// * @param id The Id of the creation function
// * @param f The object's creation function
// * @return success Bool indicating if this creator was added successfully.
// */
// bool add(const std::string& id,
// CreatorFunctionPtr f) {
// if (registry.find(id) != registry.end()) {
// XACCLogger::instance()->info(id + " already exists in Registry. Ignoring and retaining previous Registry entry");
// return true;
// }
//
// if (RuntimeOptions::instance()->exists("verbose-registry")) XACCLogger::instance()->info("Registry adding " + id);
// bool s = registry.emplace(std::make_pair(id, f)).second;
// if (s) {
// return true;
// }
// xacc::error("Could not add " + id + " to the Registry.");
// return false;
// }
//
// /**
// * Add a new creation function to the Registry, keyed
// * on the provided string id, and related
// * command line options for the object to be created.
// *
// * @param id The Id of the creation function
// * @param f The object's creation function
// * @param options The objects command line options
// * @return success Bool indicating if this creator was added successfully.
// */
// bool add(const std::string& id,
// CreatorFunctionPtr f, std::shared_ptr<options_description> options) {
// if (registry.find(id) != registry.end()) {
// return true;
// }
// if (RuntimeOptions::instance()->exists("verbose-registry")) XACCLogger::instance()->info("Registry adding " + id);
// bool s = registry.emplace(std::make_pair(id, f)).second;
// bool s2 = registryOptions.insert(std::make_pair(id, std::move(options))).second;
// if (!s || ! s2) {
// xacc::error("Could not add " + id + " to the Registry.");
// return false;
// } else {
// return true;
// }
// }
//
// bool add(const std::string& id, CreatorFunctionPtr f,
// std::shared_ptr<options_description> options,
// std::function<bool(variables_map&)> optionsHandler) {
// if (registry.find(id) != registry.end()) {
// XACCLogger::instance()->info(
// id
// + " already exists in Registry. Ignoring and retaining previous Registry entry");
// return true;
// }
// if (RuntimeOptions::instance()->exists("verbose-registry"))
// XACCLogger::instance()->info("Registry adding " + id);
// bool s = registry.emplace(std::make_pair(id, f)).second;
// bool s2 =
// registryOptions.insert(std::make_pair(id, std::move(options))).second;
// bool s3 = registryOptionHandlers.insert(std::make_pair(id, std::move(optionsHandler))).second;
// if (!s || !s2) {
// xacc::error("Could not add " + id + " to the Registry.");
// return false;
// } else {
// return true;
// }
// }
//
// /**
// * Create an instance of T by using the creation
// * function found at the given key string id.
// *
// * @param id The Id of the creation function
// * @return object Shared Pointer for the created object.
// */
// std::shared_ptr<T> create(const std::string& id, TArgs... args) {
// auto search = registry.find(id);