Commit 6eecbec4 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Fixing bugs in DWQMICompiler and adding new test

parent 36f39f16
......@@ -82,8 +82,15 @@ std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src,
}
}
// Now we have a qubits set. If we used 0-N qubits,
// then this set size will be N, but if we skipped some
// bits, then it will be < N. Get the maximum int in this set
// so we can see if any were skipped.
int maxBitIdx = *qubits.rbegin();
maxBitIdx++;
// Create a graph representation of the problem
auto problemGraph = std::make_shared<DWGraph>(qubits.size());
auto problemGraph = std::make_shared<DWGraph>(maxBitIdx);
for (auto inst : instructions) {
auto qbit1 = inst->bits()[0];
auto qbit2 = inst->bits()[1];
......@@ -93,6 +100,7 @@ std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src,
} else {
problemGraph->addEdge(qbit1, qbit2,
weightOrBias);
}
}
......@@ -109,14 +117,14 @@ std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src,
auto countEdgesBetweenSubTrees = [&](std::list<int> Ti, std::list<int> Tj) -> int {
int nEdges = 0;
for (auto i : Ti) {
for (auto j : Tj) {
if (hardwareGraph->edgeExists(i, j)) {
nEdges++;
}
for (auto i : Ti) {
for (auto j : Tj) {
if (hardwareGraph->edgeExists(i, j)) {
nEdges++;
}
}
return nEdges;
}
return nEdges;
};
auto subTreeContains = [](std::list<int> tree, int i) -> bool {
......@@ -136,8 +144,7 @@ std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src,
for (int i = 0; i < nHardwareVerts; i++) {
for (int j = 0; j < nHardwareVerts; j++) {
if (hardwareGraph->edgeExists(i,j) && i < j && i != j) {
if (hardwareGraph->edgeExists(i, j) && i < j && i != j) {
for (int pi = 0; pi < problemGraph->order(); pi++) {
for (int pj = 0; pj < problemGraph->order(); pj++) {
......@@ -147,21 +154,30 @@ std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src,
if (subTreeContains(Ti, i) && subTreeContains(Tj, j)) {
double newWeight = 0.0;
if (pi != pj) {
newWeight = problemGraph->getEdgeWeight(pi, pj)
// If problem edge does not exist,
// Graph.getEdgeWeight retusn 0.0;
newWeight = problemGraph->getEdgeWeight(pi,
pj)
/ countEdgesBetweenSubTrees(Ti, Tj);
} else {
// ferro-magnetic coupling parameter that ensures that physical
// qubits representing one logical qubit remain highly correlated.
for (auto neighbor : problemGraph->getNeighborList(pi)) {
newWeight += std::fabs(problemGraph->getEdgeWeight(pi, neighbor));
for (auto neighbor : problemGraph->getNeighborList(
pi)) {
newWeight += std::fabs(
problemGraph->getEdgeWeight(pi,
neighbor));
}
newWeight = std::get<0>(problemGraph->getVertexProperties(pi)) + newWeight - 1.0;
newWeight = std::get<0>(
problemGraph->getVertexProperties(pi))
+ newWeight - 1.0;
}
auto embeddedInst = std::make_shared<DWQMI>(i,
j, newWeight);
dwKernel->addInstruction(embeddedInst);
if (std::fabs(newWeight) > 1e-4) {
auto embeddedInst = std::make_shared<DWQMI>(i,
j, newWeight);
dwKernel->addInstruction(embeddedInst);
}
}
}
......
......@@ -38,19 +38,6 @@
using namespace xacc::quantum;
struct F {
F() :
compiler(std::make_shared<DWQMICompiler>()) {
BOOST_TEST_MESSAGE("setup fixture");
BOOST_VERIFY(compiler);
}
~F() {
BOOST_TEST_MESSAGE("teardown fixture");
}
std::shared_ptr<xacc::Compiler> compiler;
};
class FakeDWAcc : public xacc::Accelerator {
public:
......@@ -107,7 +94,6 @@ public:
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}));
return embedding;
}
......@@ -120,13 +106,41 @@ public:
}
};
//____________________________________________________________________________//
BOOST_FIXTURE_TEST_SUITE( s, F )
class Shor15FakeEmbedding : public EmbeddingAlgorithm {
public:
virtual std::map<int, std::list<int>> 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}));
return embedding;
}
/**
* Return the name of this Embedding Algorithm
* @return
*/
virtual std::string name() {
return "fake-shor15-embedding";
}
};
BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
EmbeddingAlgorithmRegistry::instance()->add(FakeEmbedding().name(), []() { return std::make_shared<FakeEmbedding>(); });
EmbeddingAlgorithmRegistry::instance()->add(FakeEmbedding().name(),
[]() {return std::make_shared<FakeEmbedding>();});
auto compiler = std::make_shared<DWQMICompiler>();
const std::string simpleQMI =
"__qpu__ dwaveKernel() {\n"
......@@ -145,9 +159,78 @@ BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
auto ir = compiler->compile(simpleQMI, acc);
// std::cout << "STR:\n" << ir->getKernel("dw-kernel")->toString("") << "\n";
auto qmi = ir->getKernel("dw-kernel")->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 0.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_SUITE_END()
BOOST_AUTO_TEST_CASE(checkShor15OneToOneMapping) {
EmbeddingAlgorithmRegistry::instance()->add(Shor15FakeEmbedding().name(),
[]() {return std::make_shared<Shor15FakeEmbedding>();});
auto compiler = std::make_shared<DWQMICompiler>();
const std::string shor15QMI =
"__qpu__ shor15() {\n"
" 0 0 20\n"
" 1 1 50\n"
" 2 2 60\n"
" 4 4 50\n"
" 5 5 60\n"
" 6 6 -160\n"
" 1 4 -1000\n"
" 2 5 -1000\n"
" 0 4 -14\n"
" 0 5 -12\n"
" 0 6 32\n"
" 1 5 68\n"
" 1 6 -128\n"
" 2 6 -128\n"
"}";
auto options = xacc::RuntimeOptions::instance();
if (options->exists("dwave-embedding")) {
(*options)["dwave-embedding"] = "fake-shor15-embedding";
} else {
options->insert(std::make_pair("dwave-embedding", "fake-shor15-embedding"));
}
auto acc = std::make_shared<FakeDWAcc>();
auto ir = compiler->compile(shor15QMI, acc);
auto qmi = ir->getKernel("dw-kernel")->toString("");
std::cout << qmi << "\n";
const std::string expected =
"0 0 20\n"
"1 1 50\n"
"2 2 60\n"
"4 4 50\n"
"5 5 60\n"
"6 6 -160\n"
"0 4 -14\n"
"0 5 -12\n"
"0 6 32\n"
"1 4 -1000\n"
"1 5 68\n"
"1 6 -128\n"
"2 5 -1000\n"
"2 6 -128\n";
BOOST_VERIFY(expected == qmi);
}
......@@ -39,7 +39,7 @@ public:
auto retGraph = std::make_shared<AcceleratorGraph>(order());
for (int i = 0; i < order(); i++) {
for (int j = 0; j < order(); j++) {
if (i < j) {
if (i < j && edgeExists(i,j)) {
retGraph->addEdge(i, j);
}
}
......
......@@ -341,7 +341,11 @@ public:
*/
double getEdgeWeight(const int srcIndex, const int tgtIndex) {
auto e = edge(vertex(srcIndex, *_graph.get()), vertex(tgtIndex, *_graph.get()), *_graph.get());
return (*_graph.get())[e.first].weight;
if (e.second) {
return (*_graph.get())[e.first].weight;
} else {
return 0.0;
}
}
/**
......
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