DWGraph.hpp 3.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * DWGraph.hpp
 *
 *  Created on: Jul 11, 2017
 *      Author: aqw
 */

#ifndef QUANTUM_AQC_IR_DWGRAPH_HPP_
#define QUANTUM_AQC_IR_DWGRAPH_HPP_

#include "Graph.hpp"
12
#include "Accelerator.hpp"
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

namespace xacc {

namespace quantum {

/**
 * The DWVertex is a subclass of the XACCVertex that
 * keeps track of one vertex parameter - the qubit
 * bias parameter. XACCVertex already keeps track
 * of edge weights.
 */
class DWVertex: public XACCVertex<double> {
public:
	DWVertex() :
			XACCVertex() {
		propertyNames[0] = "bias";
	}
};

// Alias for Graphs of DWave Vertices
class DWGraph : public Graph<DWVertex> {
public:

	DWGraph(const int nVertices) : Graph(nVertices) {
	}

	std::shared_ptr<AcceleratorGraph> getAcceleratorGraph() {
		auto retGraph = std::make_shared<AcceleratorGraph>(order());
		for (int i = 0; i < order(); i++) {
			for (int j = 0; j < order(); j++) {
43
				if (i < j && edgeExists(i,j)) {
44
					retGraph->addEdge(i, j);
45
46
47
48
49
50
51
				}
			}
		}

		return retGraph;
	}

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	std::string toKernelSource() {
		std::string src;
		for (int i = 0; i < order(); i++) {
			src += std::to_string(i) + " " + std::to_string(i) + " "
					+ std::to_string(getVertexProperty<0>(i)) + "\n";
		}
		for (int i = 0; i < order(); i++) {
			for (int j = 0; j < order(); j++) {
				if (i < j && edgeExists(i, j)) {
					src += std::to_string(i) + " " + std::to_string(j) + " "
							+ std::to_string(getEdgeWeight(i, j)) + "\n";
				}
			}
		}

		return src;
	}

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	virtual ~DWGraph() {}
};

class K44Bipartite : public DWGraph {
public:

	K44Bipartite() :
			DWGraph(8) {
		addEdge(0, 4);
		addEdge(0, 5);
		addEdge(0, 6);
		addEdge(0, 7);
		addEdge(1, 4);
		addEdge(1, 5);
		addEdge(1, 6);
		addEdge(1, 7);
		addEdge(2, 4);
		addEdge(2, 5);
		addEdge(2, 6);
		addEdge(2, 7);
		addEdge(3, 4);
		addEdge(3, 5);
		addEdge(3, 6);
		addEdge(3, 7);
	}

	virtual ~K44Bipartite() {}
};

class CompleteGraph : public DWGraph {
public:

	CompleteGraph(int nVertices) :
			DWGraph(nVertices) {
		for (int i = 0; i < nVertices; i++) {
			for (int j = 0; j < nVertices; j++) {
				if (i < j) {
					addEdge(i, j);
				}
			}
		}
	}

	virtual ~CompleteGraph() {}
};

116
class ChimeraGraph : public DWGraph {
117
118
public:

119
	ChimeraGraph(int gridSize) : DWGraph(8 * gridSize * gridSize) {
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
		// We are going to assign a tuple of ints to each vertex
		std::map<std::tuple<int, int, int, int>, int> vertexTuples;
		int i, j, k, l, v = 0;

		// Loop through the list of vertices and give
		// them the correct 4-tuple: (i,j) is unit cell in
		// the chimera grid, (k,l) is vertex in unit cell, k = 0-3,
		// l = 0,1 (left, right of bipartite graph)
		for (i = 0; i < gridSize; i++) {
			for (j = 0; j < gridSize; j++) {
				for (l = 0; l < 2; l++) {
					for (k = 0; k < 4; k++) {
						vertexTuples.insert(
								std::make_pair(std::make_tuple(i, j, k, l), v));
						v++;
					}
				}
			}
		}

		// Setup edges within each cell
		for (i = 0; i < gridSize; i++) {
			for (j = 0; j < gridSize; j++) {
				for (k = 0; k < 4; k++) {
					for (int k2 = 0; k2 < 4; k2++) {
						int v1 = vertexTuples[std::make_tuple(i, j, k, 0)];
						int v2 = vertexTuples[std::make_tuple(i, j, k2, 1)];
						addEdge(v1, v2);
					}

				}
			}
		}

		// Setup edges between cells
		for (i = 0; i < gridSize - 1; i++) {
			for (j = 0; j < gridSize; j++) {
				for (k = 0; k < 4; k++) {
					int v1 = vertexTuples[std::make_tuple(i, j, k, 0)];
					int v2 = vertexTuples[std::make_tuple(i + 1, j, k, 0)];
					addEdge(v1, v2);

				}
			}
		}

		for (i = 0; i < gridSize; i++) {
			for (j = 0; j < gridSize - 1; j++) {
				for (k = 0; k < 4; k++) {
					int v1 = vertexTuples[std::make_tuple(i, j, k, 1)];
					int v2 = vertexTuples[std::make_tuple(i, j + 1, k, 1)];
					addEdge(v1, v2);

				}
			}
		}
	}

178
	virtual ~ChimeraGraph() {}
179
180
181
182
183
184
};
}

}

#endif