Commit b5c93468 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Continuing work on integrating CppMicroServices, removed all uses of Registry...

Continuing work on integrating CppMicroServices, removed all uses of Registry except for GateInstructions
parent 06957faa
......@@ -252,112 +252,7 @@ public:
}
return true;
}
return false; }
/**
* Register this Accelerator with the framework.
*/
static void registerAccelerator() {
DWAccelerator acc;
xacc::RegisterAccelerator<xacc::quantum::DWAccelerator> DWTEMP(
"dwave", acc.getOptions(),
[](variables_map& args) -> bool {
if(args.count("dwave-list-solvers")) {
std::string tempApiKey, tempUrl;
auto findApiKeyInFile = [](std::string& apiKey, std::string& url,
boost::filesystem::path &p) {
std::ifstream stream(p.string());
std::string contents(
(std::istreambuf_iterator<char>(stream)),
std::istreambuf_iterator<char>());
std::vector<std::string> lines;
boost::split(lines, contents, boost::is_any_of("\n"));
for (auto l : lines) {
if (boost::contains(l, "key")) {
std::vector<std::string> split;
boost::split(split, l, boost::is_any_of(":"));
auto key = split[1];
boost::trim(key);
apiKey = key;
} else if (boost::contains(l, "url")) {
std::vector<std::string> split;
boost::split(split, l, boost::is_any_of(":"));
auto key = split[1] + ":" + split[2];
boost::trim(key);
url = key;
}
}
};
auto searchAPIKey = [&](std::string& key, std::string& url) {
// Search for the API Key in $HOME/.dwave_config,
// $DWAVE_CONFIG, or in the command line argument --dwave-api-key
auto options = RuntimeOptions::instance();
boost::filesystem::path dwaveConfig(
std::string(getenv("HOME")) + "/.dwave_config");
if (boost::filesystem::exists(dwaveConfig)) {
findApiKeyInFile(key, url, dwaveConfig);
} else if (const char * nonStandardPath = getenv("DWAVE_CONFIG")) {
boost::filesystem::path nonStandardDwaveConfig(
nonStandardPath);
findApiKeyInFile(key, url, nonStandardDwaveConfig);
} else {
// Ensure that the user has provided an api-key
if (!options->exists("dwave-api-key")) {
XACCError("Cannot list D-Wave solvers without API Key.");
}
// Set the API Key
key = (*options)["dwave-api-key"];
if (options->exists("dwave-api-url")) {
url = (*options)["dwave-api-url"];
}
}
// If its still empty, then we have a problem
if (key.empty()) {
XACCError("Error. The API Key is empty. Please place it "
"in your $HOME/.dwave_config file, $DWAVE_CONFIG env var, "
"or provide --dwave-api-key argument.");
}
};
searchAPIKey(tempApiKey, tempUrl);
boost::replace_all(tempUrl, "https://", "");
boost::replace_all(tempUrl, "/sapi", "");
std::map<std::string, std::string> headers;
// Set up the extra HTTP headers we are going to need
headers.insert(std::make_pair("X-Auth-Token", tempApiKey));
headers.insert(std::make_pair("Content-type", "application/x-www-form-urlencoded"));
headers.insert(std::make_pair("Accept", "*/*"));
// Get the Remote URL Solver data...
auto getSolverClient = fire::util::AsioNetworkingTool<SimpleWeb::HTTPS>(tempUrl, false);
auto r = getSolverClient.get("/sapi/solvers/remote", headers);
std::stringstream ss;
ss << r.content.rdbuf();
auto message = ss.str();
Document document;
document.Parse(message.c_str());
if (document.IsArray()) {
for (auto i = 0; i < document.Size(); i++) {
XACCInfo("Available D-Wave Solver: " + std::string(document[i]["id"].GetString()));
}
}
return true;
}
return false;
});
return false;
}
/**
......@@ -443,9 +338,6 @@ private:
};
// Create an alias to search for.
RegisterAccelerator(xacc::quantum::DWAccelerator)
}
}
......
/***********************************************************************************
* Copyright (c) 2017, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Contributors:
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#include "DWAccelerator.hpp"
#include "cppmicroservices/BundleActivator.h"
......@@ -12,41 +42,25 @@ using namespace cppmicroservices;
namespace {
/**
* This class implements a bundle activator that uses the bundle
* context to register an English language dictionary service
* with the C++ Micro Services registry during static initialization
* of the bundle. The dictionary service interface is
* defined in a separate file and is implemented by a nested class.
*/
class US_ABI_LOCAL DWAcceleratorActivator : public BundleActivator
{
class US_ABI_LOCAL DWAcceleratorActivator: public BundleActivator {
public:
DWAcceleratorActivator() {}
DWAcceleratorActivator() {
}
/**
* Implements BundleActivator::Start(). Registers an
* instance of a dictionary service using the bundle context;
* attaches properties to the service that can be queried
* when performing a service look-up.
* @param context the context for the bundle.
/**
*/
void Start(BundleContext context) {
auto acc = std::make_shared<
xacc::quantum::DWAccelerator>();
context.RegisterService<xacc::Accelerator>(
acc);
auto acc = std::make_shared<xacc::quantum::DWAccelerator>();
context.RegisterService<xacc::Accelerator>(acc);
context.RegisterService<xacc::OptionsProvider>(acc);
}
/**
* Implements BundleActivator::Stop(). Does nothing since
* the C++ Micro Services library will automatically unregister any registered services.
* @param context the context for the bundle.
*/
void Stop(BundleContext /*context*/) {
// NOTE: The service is automatically unregistered
}
};
......@@ -54,4 +68,3 @@ public:
}
CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(DWAcceleratorActivator)
//![Activator]
/***********************************************************************************
* Copyright (c) 2016, UT-Battelle
* Copyright (c) 2017, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
......@@ -129,7 +129,7 @@ std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src,
} else {
auto algoStr = (*runtimeOptions)["dwave-embedding"];
auto embeddingAlgorithm =
EmbeddingAlgorithmRegistry::instance()->create(algoStr);
ServiceRegistry::instance()->getService<EmbeddingAlgorithm>(algoStr);
// Compute the minor graph embedding
embedding = embeddingAlgorithm->embed(problemGraph, hardwareGraph);
......@@ -147,15 +147,16 @@ std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src,
// Get the ParameterSetter
std::shared_ptr<ParameterSetter> parameterSetter;
if (runtimeOptions->exists("dwave-parameter-setter")) {
parameterSetter = ParameterSetterRegistry::instance()->create(
(*runtimeOptions)["dwave-parameter-setter"]);
parameterSetter = ServiceRegistry::instance()->getService<
ParameterSetter>((*runtimeOptions)["dwave-parameter-setter"]);
} else {
parameterSetter = ParameterSetterRegistry::instance()->create(
"default");
parameterSetter = ServiceRegistry::instance()->getService<
ParameterSetter>("default");
}
// Set the parameters
auto insts = parameterSetter->setParameters(problemGraph, hardwareGraph, embedding);
auto insts = parameterSetter->setParameters(problemGraph, hardwareGraph,
embedding);
// Add the instructions to the Kernel
for (auto i : insts) {
......
......@@ -108,29 +108,9 @@ public:
return desc;
}
/**
* Register this Compiler with the framework.
*/
static void registerCompiler() {
DWQMICompiler c;
xacc::RegisterCompiler<xacc::quantum::DWQMICompiler> DWQMITEMP(
"dwave-qmi", c.getOptions(),
[](variables_map& args) -> bool {
if(args.count("dwave-list-embedding-algorithms")) {
auto ids = EmbeddingAlgorithmRegistry::instance()->getRegisteredIds();
for (auto i : ids) {
XACCInfo("Registered Embedding Algorithm: " + i);
}
return true;
}
return false;
});
}
virtual bool handleOptions(variables_map& map) {
if (map.count("dwave-list-embedding-algorithms")) {
auto ids = ServiceRegistry::instance()->getRegisteredIds<EmbeddingAlgorithm>();
// EmbeddingAlgorithmRegistry::instance()->getRegisteredIds();
for (auto i : ids) {
XACCInfo("Registered Embedding Algorithm: " + i);
}
......@@ -138,6 +118,7 @@ public:
}
return false;
}
/**
* We don't allow translations for the DW Compiler.
* @param bufferVariable
......@@ -165,8 +146,6 @@ public:
};
RegisterCompiler(xacc::quantum::DWQMICompiler)
}
}
......
/***********************************************************************************
* Copyright (c) 2017, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Contributors:
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#include "DWQMICompiler.hpp"
#include "cppmicroservices/BundleActivator.h"
......@@ -12,41 +42,26 @@ using namespace cppmicroservices;
namespace {
/**
* This class implements a bundle activator that uses the bundle
* context to register an English language dictionary service
* with the C++ Micro Services registry during static initialization
* of the bundle. The dictionary service interface is
* defined in a separate file and is implemented by a nested class.
*/
class US_ABI_LOCAL DWQMICompilerActivator : public BundleActivator
{
class US_ABI_LOCAL DWQMICompilerActivator: public BundleActivator {
public:
DWQMICompilerActivator() {}
DWQMICompilerActivator() {
}
/**
* Implements BundleActivator::Start(). Registers an
* instance of a dictionary service using the bundle context;
* attaches properties to the service that can be queried
* when performing a service look-up.
* @param context the context for the bundle.
/**
*/
void Start(BundleContext context) {
auto c = std::make_shared<
xacc::quantum::DWQMICompiler>();
context.RegisterService<xacc::Compiler>(
c);
auto c = std::make_shared<xacc::quantum::DWQMICompiler>();
context.RegisterService<xacc::Compiler>(c);
context.RegisterService<xacc::OptionsProvider>(c);
}
/**
* Implements BundleActivator::Stop(). Does nothing since
* the C++ Micro Services library will automatically unregister any registered services.
* @param context the context for the bundle.
*/
void Stop(BundleContext /*context*/) {
// NOTE: The service is automatically unregistered
}
};
......
......@@ -29,15 +29,6 @@
*
**********************************************************************************/
#include "XACC.hpp"
#include "EmbeddingAlgorithm.hpp"
#include <cppmicroservices/FrameworkFactory.h>
#include <cppmicroservices/Framework.h>
#include <cppmicroservices/BundleContext.h>
#include <cppmicroservices/Bundle.h>
#include <cppmicroservices/BundleImport.h>
using namespace cppmicroservices;
const std::string src =
"__qpu__ factoring15() {\n"
......@@ -59,57 +50,11 @@ const std::string src =
int main (int argc, char** argv) {
// Create a new framework with a default configuration.
Framework fw = FrameworkFactory().NewFramework();
try {
// Initialize the framework, such that we can call
// GetBundleContext() later.
fw.Init();
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
return 1;
}
// The framework inherits from the Bundle class; it is
// itself a bundle.
auto ctx = fw.GetBundleContext();
if (!ctx) {
std::cerr << "Invalid framework context" << std::endl;
return 1;
}
ctx.InstallBundles("/usr/local/xacc/lib/plugins/embedding/libxacc-trivial-embedding.so");
ctx.InstallBundles("/usr/local/xacc/lib/plugins/compilers/libxacc-scaffold.so");
try {
// Start the framwork itself.
fw.Start();
// Our bundles depend on each other in the sense that the consumer
// bundle expects a ServiceTime service in its activator Start()
// function. This is done here for simplicity, but is actually
// bad practice.
auto bundles = ctx.GetBundles();
for (auto b : bundles) {
std::cout << "BUNDLE: " << b.GetSymbolicName() << "\n";
b.Start();
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
auto ref = ctx.GetServiceReference<xacc::quantum::EmbeddingAlgorithm>();
auto emb = ctx.GetService(ref);
std::cout << "EMBALGO NAME: " << emb->name() << "\n";
// Initialize the XACC Framework
xacc::Initialize(argc, argv);
(*xacc::RuntimeOptions::instance())["compiler"] = "dwave-qmi";
auto qpu = xacc::getAccelerator("dwave");
auto qubits = qpu->createBuffer("qbits");
......
......@@ -166,56 +166,51 @@ public:
};
BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
DefaultParameterSetter setter;
EmbeddingAlgorithmRegistry::instance()->add(FakeEmbedding().name(),
std::make_shared<EmbeddingAlgorithmRegistry::CreatorFunction>(
[]() {return std::make_shared<FakeEmbedding>();}));
auto compiler = std::make_shared<DWQMICompiler>();
const std::string simpleQMI =
"__qpu__ dwaveKernel() {\n"
" 0 0 0.98\n"
" 1 1 .33\n"
" 2 2 .44\n"
" 0 1 .22\n"
" 0 2 .55\n"
" 1 2 .11\n"
"}";
auto options = xacc::RuntimeOptions::instance();
options->insert(std::make_pair("dwave-embedding", "fake-embedding"));
auto acc = std::make_shared<FakeDWAcc>();
auto ir = compiler->compile(simpleQMI, acc);
auto qmi = ir->getKernel("dwaveKernel")->toString("");
const std::string expectedQMI = "0 0 0.49\n"
"4 4 0.49\n"
"1 1 0.33\n"
"5 5 0.44\n"
"0 4 -1.75\n"
"0 5 0.55\n"
"1 4 0.22\n"
"1 5 0.11\n";
std::cout << "\n" << qmi << "\n";
BOOST_VERIFY(expectedQMI == qmi);
}
//
//BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
//
// DefaultParameterSetter setter;
// EmbeddingAlgorithmRegistry::instance()->add(FakeEmbedding().name(),
// std::make_shared<EmbeddingAlgorithmRegistry::CreatorFunction>(
// []() {return std::make_shared<FakeEmbedding>();}));
//
// auto compiler = std::make_shared<DWQMICompiler>();
//
// const std::string simpleQMI =
// "__qpu__ dwaveKernel() {\n"
// " 0 0 0.98\n"
// " 1 1 .33\n"
// " 2 2 .44\n"
// " 0 1 .22\n"
// " 0 2 .55\n"
// " 1 2 .11\n"
// "}";
//
// auto options = xacc::RuntimeOptions::instance();
// options->insert(std::make_pair("dwave-embedding", "fake-embedding"));
//
// auto acc = std::make_shared<FakeDWAcc>();
//
// auto ir = compiler->compile(simpleQMI, acc);
//
// auto qmi = ir->getKernel("dwaveKernel")->toString("");
//
// const std::string expectedQMI = "0 0 0.49\n"
// "4 4 0.49\n"
// "1 1 0.33\n"
// "5 5 0.44\n"
// "0 4 -1.75\n"
// "0 5 0.55\n"
// "1 4 0.22\n"
// "1 5 0.11\n";
//
// std::cout << "\n" << qmi << "\n";
// BOOST_VERIFY(expectedQMI == qmi);
//
//}
BOOST_AUTO_TEST_CASE(checkFactoring15OneToOneMapping) {
EmbeddingAlgorithmRegistry::instance()->add(
Factoring15FakeEmbedding().name(),
std::make_shared<EmbeddingAlgorithmRegistry::CreatorFunction>(
[]() {return std::make_shared<Factoring15FakeEmbedding>();}));
auto compiler = std::make_shared<DWQMICompiler>();
const std::string factoring15QMI =
......@@ -238,9 +233,9 @@ BOOST_AUTO_TEST_CASE(checkFactoring15OneToOneMapping) {
auto options = xacc::RuntimeOptions::instance();
if (options->exists("dwave-embedding")) {
(*options)["dwave-embedding"] = "fake-factor15-embedding";
(*options)["dwave-embedding"] = "trivial";
} else {
options->insert(std::make_pair("dwave-embedding", "fake-factor15-embedding"));
options->insert(std::make_pair("dwave-embedding", "trivial"));
}
auto acc = std::make_shared<FakeDWAcc>();
......@@ -255,6 +250,7 @@ BOOST_AUTO_TEST_CASE(checkFactoring15OneToOneMapping) {
"0 0 20\n"
"1 1 50\n"
"2 2 60\n"
"3 3 0\n"
"4 4 50\n"
"5 5 60\n"
"6 6 -160\n"
......
......@@ -126,15 +126,6 @@ public:
return desc;
}
/**
* Register this Accelerator with the framework.
*/
static void registerAccelerator() {
RigettiAccelerator acc;
xacc::RegisterAccelerator<xacc::quantum::RigettiAccelerator> RIGETTITEMP(
"rigetti", acc.getOptions());
}
RigettiAccelerator() : httpClient(std::make_shared<
fire::util::AsioNetworkingTool<SimpleWeb::HTTPS>>(
"api.rigetti.com", false)) {
......@@ -185,9 +176,6 @@ private:
};
// Create an alias to search for.
RegisterAccelerator(xacc::quantum::RigettiAccelerator)
}
}
......
/***********************************************************************************
* Copyright (c) 2017, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright