Commit 4d522e1f authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Adding Embedding object that can be loaded from file or persisted to file (#41)

parent a4e73cf5
......@@ -108,14 +108,14 @@ public:
class FakeEmbedding : public EmbeddingAlgorithm {
public:
virtual std::map<int, std::list<int>> embed(
virtual Embedding embed(
std::shared_ptr<DWGraph> problem, std::shared_ptr<xacc::AcceleratorGraph> hardware,
std::map<std::string, std::string> params = std::map<std::string,
std::string>()) override {
std::map<int, std::list<int>> embedding;
embedding.insert(std::make_pair(0, std::list<int>{0, 4}));
embedding.insert(std::make_pair(1, std::list<int>{1}));
embedding.insert(std::make_pair(2, std::list<int>{5}));
Embedding embedding;
embedding.insert(std::make_pair(0, std::vector<int>{0, 4}));
embedding.insert(std::make_pair(1, std::vector<int>{1}));
embedding.insert(std::make_pair(2, std::vector<int>{5}));
return embedding;
}
......@@ -132,17 +132,17 @@ public:
class Factoring15FakeEmbedding : public EmbeddingAlgorithm {
public:
virtual std::map<int, std::list<int>> embed(
virtual Embedding embed(
std::shared_ptr<DWGraph> problem, std::shared_ptr<xacc::AcceleratorGraph> hardware,
std::map<std::string, std::string> params = std::map<std::string,
std::string>()) override {
std::map<int, std::list<int>> embedding;
embedding.insert(std::make_pair(0, std::list<int>{0}));
embedding.insert(std::make_pair(1, std::list<int>{1}));
embedding.insert(std::make_pair(2, std::list<int>{2}));
embedding.insert(std::make_pair(4, std::list<int>{4}));
embedding.insert(std::make_pair(5, std::list<int>{5}));
embedding.insert(std::make_pair(6, std::list<int>{6}));
Embedding embedding;
embedding.insert(std::make_pair(0, std::vector<int>{0}));
embedding.insert(std::make_pair(1, std::vector<int>{1}));
embedding.insert(std::make_pair(2, std::vector<int>{2}));
embedding.insert(std::make_pair(4, std::vector<int>{4}));
embedding.insert(std::make_pair(5, std::vector<int>{5}));
embedding.insert(std::make_pair(6, std::vector<int>{6}));
return embedding;
}
......
......@@ -32,6 +32,7 @@
#define QUANTUM_AQC_ACCELERATOR_AQCACCELERATORBUFFER_HPP_
#include "AcceleratorBuffer.hpp"
#include "Embedding.hpp"
namespace xacc {
namespace quantum {
......@@ -50,7 +51,7 @@ protected:
* The minor graph embedding for the problem these
* results represent.
*/
std::map<int, std::list<int>> embedding;
Embedding embedding;
/**
* The energies computed as part of this execution.
......@@ -96,7 +97,7 @@ public:
*
* @param emb The minor graph embedding
*/
void setEmbedding(std::map<int, std::list<int>> emb) {
void setEmbedding(Embedding emb) {
embedding = emb;
}
......@@ -105,7 +106,7 @@ public:
*
* @return emb The minor graph embedding
*/
std::map<int, std::list<int>> getEmbedding() {
Embedding getEmbedding() {
return embedding;
}
......
#ifndef QUANTUM_AQC_COMPILER_EMBEDDING_HPP_
#define QUANTUM_AQC_COMPILER_EMBEDDING_HPP_
#include <map>
#include <vector>
#include <boost/algorithm/string.hpp>
namespace xacc {
namespace quantum {
class Embedding : public std::map<int, std::vector<int>> {
public:
void persist(std::ostream& stream) {
for (auto& kv : *this) {
stream << kv.first << ": ";
for (int i = 0; i < kv.second.size(); i++) {
if (i == kv.second.size()-1) {
stream << kv.second[i];
} else {
stream << kv.second[i] << " ";
}
}
stream << "\n";
}
}
void load(std::istream& stream) {
std::string s(std::istreambuf_iterator<char>(stream), {});
std::vector<std::string> splitNewLine, splitColon, splitSpaces;
boost::split(splitNewLine, s, boost::is_any_of("\n"));
for (auto line : splitNewLine) {
if (!line.empty()) {
boost::split(splitColon, line, boost::is_any_of(":"));
auto probVert = std::stoi(splitColon[0]);
std::vector<int> hardwareVerts;
boost::split(splitSpaces, splitColon[1], boost::is_any_of(" "));
for (auto i : splitSpaces) {
if (!i.empty()) {
hardwareVerts.push_back(std::stoi(i));
}
}
insert(std::make_pair(probVert, hardwareVerts));
}
}
return;
}
};
}
}
#endif
......@@ -39,6 +39,7 @@
#include "Utils.hpp"
#include "Registry.hpp"
#include "DWGraph.hpp"
#include "Embedding.hpp"
namespace xacc {
namespace quantum {
......@@ -72,7 +73,7 @@ public:
* @param params Any key-value string parameters to influence the algorithm.
* @return embedding A mapping of problem vertex indices to the list of hardware vertices they map to
*/
virtual std::map<int, std::list<int>> embed(std::shared_ptr<DWGraph> problem,
virtual Embedding embed(std::shared_ptr<DWGraph> problem,
std::shared_ptr<AcceleratorGraph> hardware,
std::map<std::string, std::string> params = std::map<std::string,
std::string>()) = 0;
......
......@@ -4,6 +4,7 @@
#include "DWGraph.hpp"
#include "DWQMI.hpp"
#include "Embedding.hpp"
namespace xacc {
namespace quantum {
......@@ -15,7 +16,7 @@ public:
virtual std::list<std::shared_ptr<DWQMI>> setParameters(
std::shared_ptr<DWGraph> problemGraph,
std::shared_ptr<AcceleratorGraph> hardwareGraph,
std::map<int, std::list<int>> embedding) = 0;
Embedding embedding) = 0;
virtual ~ParameterSetter() {}
};
......
......@@ -36,14 +36,14 @@ namespace quantum {
std::list<std::shared_ptr<DWQMI>> DefaultParameterSetter::setParameters(
std::shared_ptr<DWGraph> problemGraph,
std::shared_ptr<AcceleratorGraph> hardwareGraph,
std::map<int, std::list<int>> embedding) {
Embedding embedding) {
std::list<std::shared_ptr<DWQMI>> instList;
auto nHardwareVerts = hardwareGraph->order();
auto nProblemVerts = problemGraph->order();
auto countEdgesBetweenSubTrees =
[&](std::list<int> Ti, std::list<int> Tj) -> int {
[&](std::vector<int> Ti, std::vector<int> Tj) -> int {
int nEdges = 0;
for (auto i : Ti) {
for (auto j : Tj) {
......@@ -55,7 +55,7 @@ std::list<std::shared_ptr<DWQMI>> DefaultParameterSetter::setParameters(
return nEdges;
};
auto subTreeContains = [](std::list<int> tree, int i) -> bool {
auto subTreeContains = [](std::vector<int> tree, int i) -> bool {
return std::find(tree.begin(), tree.end(), i) != tree.end();
};
......
......@@ -55,7 +55,7 @@ public:
virtual std::list<std::shared_ptr<DWQMI>> setParameters(
std::shared_ptr<DWGraph> problemGraph,
std::shared_ptr<AcceleratorGraph> hardwareGraph,
std::map<int, std::list<int>> embedding);
Embedding embedding);
};
}
......
......@@ -32,12 +32,12 @@
namespace xacc {
namespace quantum {
std::map<int, std::list<int>> TrivialEmbeddingAlgorithm::embed(
Embedding TrivialEmbeddingAlgorithm::embed(
std::shared_ptr<DWGraph> problem,
std::shared_ptr<xacc::AcceleratorGraph> hdware,
std::map<std::string, std::string> params) {
std::map<int, std::list<int>> xaccEmbedding;
Embedding xaccEmbedding;
bool failHard = true;
if (params.count("failhard")) {
failHard = params["failhard"] == "false" ? false : true;
......@@ -59,7 +59,7 @@ std::map<int, std::list<int>> TrivialEmbeddingAlgorithm::embed(
}
}
}
xaccEmbedding.insert(std::make_pair(i, std::list<int>{i}));
xaccEmbedding.insert(std::make_pair(i, std::vector<int>{i}));
}
return xaccEmbedding;
......
......@@ -65,7 +65,7 @@ public:
* @param params Any key-value string parameters to influence the algorithm.
* @return embedding A mapping of problem vertex indices to the list of hardware vertices they map to
*/
virtual std::map<int, std::list<int>> embed(std::shared_ptr<DWGraph> problem,
virtual Embedding embed(std::shared_ptr<DWGraph> problem,
std::shared_ptr<AcceleratorGraph> hardware,
std::map<std::string, std::string> params = std::map<std::string,
std::string>());
......
/***********************************************************************************
* Copyright (c) 2016, 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
*
**********************************************************************************/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE EmbeddingTester
#include <boost/test/included/unit_test.hpp>
#include "Embedding.hpp"
using namespace xacc::quantum;
BOOST_AUTO_TEST_CASE(checkLoadPersist) {
Embedding embedding;
embedding.insert(std::make_pair(0, std::vector<int>{0, 4}));
embedding.insert(std::make_pair(1, std::vector<int>{1, 5}));
embedding.insert(std::make_pair(2, std::vector<int>{2, 6}));
embedding.insert(std::make_pair(3, std::vector<int>{3}));
embedding.insert(std::make_pair(4, std::vector<int>{7}));
std::stringstream ss;
embedding.persist(ss);
std::string expected =
"0: 0 4\n"
"1: 1 5\n"
"2: 2 6\n"
"3: 3\n"
"4: 7\n";
BOOST_VERIFY(ss.str() == expected);
Embedding toBeLoaded;
std::istringstream iss(expected);
toBeLoaded.load(iss);
toBeLoaded.persist(std::cout);
BOOST_VERIFY(toBeLoaded == embedding);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment