Graph.hpp 13.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
116
117
118
119
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/***********************************************************************************
 * 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 <organization> 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
 *
 **********************************************************************************/
#ifndef XACC_UTILS_IGRAPH_HPP_
#define XACC_UTILS_IGRAPH_HPP_

#include <boost/graph/adjacency_list.hpp>
#include <type_traits>
#include <boost/graph/exterior_property.hpp>
#include <boost/graph/floyd_warshall_shortest.hpp>
#include <boost/graph/eccentricity.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <numeric>
#include "XACCError.hpp"
#include <utility>

using namespace boost;

namespace xacc {

/**
 * Utility structs to help determine if
 * we have been given valid Vertices.
 */
template<typename T, typename = void>
struct is_valid_vertex: std::false_type {
};
template<typename T>
struct is_valid_vertex<T, decltype(std::declval<T>().properties, void())> : std::true_type {
};

/**
 * The base class of all QCI Vertices for the
 * QCI Common Graph class. All Vertices must keep
 * track of a set of properties, stored as a tuple.
 */
template<typename ... Properties>
class XACCVertex {
public:
	std::tuple<Properties...> properties;
	std::vector<std::string> propertyNames;
	XACCVertex() : propertyNames(sizeof...(Properties)) {}
	std::string getPropertyName(const int index) {
		return propertyNames[index];
	}
};

/**
 * For now, we only allow Edges with weight property.
 */
struct DefaultEdge {
	double weight = 0.0;
};

enum GraphType {
	Undirected, Directed
};

// Overload stream operator for vector
template<class T>
std::ostream& operator << (std::ostream& os, const std::vector<T>& v) {
	os << "[";
	for (std::size_t i = 0; i < v.size(); i++) {
		os << v[i] << (i == v.size() - 1 ? "" : " ");
	}
	os << "]";
	return os;
}

template<std::size_t> struct int_{};

template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos> ) {
  out << std::get< std::tuple_size<Tuple>::value-Pos >(t) << ',';
  return print_tuple(out, t, int_<Pos-1>());
}

template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1> ) {
  return out << std::get<std::tuple_size<Tuple>::value-1>(t);
}

template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
  out << std::string(" [label=\"");
  print_tuple(out, t, int_<sizeof...(Args)>());
  return out << "\"]";
}

/**
 * The Graph class provides a generic data structure modeling
 * mathematical graph structures. It is templated on the vertex
 * type, allowing for graphs with a wide variety of graph nodes
 * (for example, in quantum computing - graph of tensors, graph of
 * Ising parameters, etc.)
 *
 * All provided Vertex types must be a subclass of the
 * QCIVertex in order to properly provide a tuple of vertex
 * properties.
 * s
 */
template<typename Vertex, GraphType type = Undirected>
class Graph {

	// Make sure we've been given a valid Vertex
	static_assert(is_valid_vertex<Vertex>::value, "XACC Graph can only take Vertices that are derived from XACCVertex, or have a tuple properties member.");

	using graph_type = typename std::conditional<(type == GraphType::Undirected), undirectedS, directedS>::type;

	// Setup some easy to use aliases
	using adj_list = adjacency_list<vecS, vecS, graph_type, Vertex, DefaultEdge>;
	using BoostGraph = std::shared_ptr<adj_list>;
	using vertex_type = typename boost::graph_traits<adjacency_list<vecS, vecS, undirectedS, Vertex, DefaultEdge>>::vertex_descriptor;
	using edge_type = typename boost::graph_traits<adjacency_list<vecS, vecS, undirectedS, Vertex, DefaultEdge>>::edge_descriptor;

protected:

	/**
	 * This is a custom utility class for writing
	 * XACCVertices with user-defined properties.
	 */
	class XACCVertexPropertiesWriter {
	protected:
		adj_list graph;
	public:
		XACCVertexPropertiesWriter(adj_list& list) :
				graph(list) {
		}
		template<class BoostVertex>
		void operator()(std::ostream& out, const BoostVertex& v) const {
			auto node = vertex(v, graph);
			std::stringstream ss;
			ss << graph[node].properties;

			// Now we have a string of comma separated parameters
			std::string result;
			auto vertexString = ss.str();
			boost::trim(vertexString);
			std::vector<std::string> splitVec;
			boost::split(splitVec, vertexString, boost::is_any_of(","));

			int counter = 0;
			for (std::size_t i = 0; i < splitVec.size(); i++) {
				if (!graph[node].propertyNames[counter].empty()) {
					auto s = splitVec[i];
					if (i == 0) {
						s.insert(8, graph[node].propertyNames[counter] + "=");
					} else {
						s.insert(0, graph[node].propertyNames[counter] + "=");
					}
					counter++;
					result += s + ",";
				}
			}

			result = result.substr(0,result.size() - 1);
			if (result.substr(result.size() - 2, result.size()) != "\"]") {
				result += "\"]";
			}
			out << " " << result;
		}
	};

	/**
	 * The actual graph data structure we are
	 * delegating to.
	 */
	BoostGraph _graph;

public:

	/**
	 * The nullary constructor
	 */
	Graph() {
		_graph = std::make_shared<adj_list>();
	}

	/**
	 * The constructor, constructs a graph with
	 * specified number of vertices.
	 *
	 * @param numberOfVertices The number of vertices
	 */
	Graph(const int numberOfVertices) {
		_graph = std::make_shared<adj_list>(
				numberOfVertices);
	}

	/**
	 * Add an edge between the vertices with given provided
	 * indices and edge weight.
	 *
	 * @param srcIndex Index of the starting vertex
	 * @param tgtIndex Index of the ending vertex
	 * @param edgeWeight The edge weight
	 */
	void addEdge(const int srcIndex, const int tgtIndex, const double edgeWeight) {
		auto edgeBoolPair = add_edge(vertex(srcIndex, *_graph.get()),
				vertex(tgtIndex, *_graph.get()), *_graph.get());
		if (!edgeBoolPair.second) {

		}
		(*_graph.get())[edgeBoolPair.first].weight = edgeWeight;
	}

	/**
	 * Add an edge with default edge weight between the
	 * vertices at the provided indices.
	 *
	 * @param srcIndex Index of the starting vertex
	 * @param tgtIndex Index of the ending vertex
	 */
	void addEdge(const int srcIndex, const int tgtIndex) {
		add_edge(vertex(srcIndex, *_graph.get()),
				vertex(tgtIndex, *_graph.get()), *_graph.get());
	}

	/**
	 * Add a vertex to this Graph.
	 */
	void addVertex() {
		add_vertex(*_graph.get());
	}

	/**
	 * Add a vertex to this graph with the provided properties.
	 * s
	 * @param properties
	 */
	template<typename ... Properties>
	void addVertex(Properties ... properties) {
		auto v = add_vertex(*_graph.get());
		(*_graph.get())[v].properties = std::make_tuple(properties...);
	}

	void addVertex(Vertex& vertex) {
		auto v = add_vertex(*_graph.get());
		(*_graph.get())[v].properties = vertex.properties;
	}

	/**
	 * Set an existing vertices properties.
	 *
	 * @param index The index of the vertex
	 * @param properties The new properties for the vertex
	 */
	template<typename... Properties>
	void setVertexProperties(const int index, Properties... properties) {
		auto v = vertex(index, *_graph.get());
		(*_graph.get())[v].properties = std::make_tuple(properties...);
	}

	/**
	 * Set a specific vertex property for the vertex at given index.
	 *
	 * @param index The index of the vertex
	 * @param prop The property to set.
	 */
	template<const int PropertyIndex>
	void setVertexProperty(const int index,
			decltype(std::get<PropertyIndex>(std::declval<Vertex>().properties)) prop) {
		auto v = vertex(index, *_graph.get());
		std::get<PropertyIndex>((*_graph.get())[v].properties) = prop;
		return;
	}

	Vertex& getVertex(const int index) {
		auto v = vertex(index, *_graph.get());
		return (*_graph.get())[v];
	}

	auto getVertexProperties(const int index) -> decltype((*_graph.get())[index].properties) {
		return (*_graph.get())[index].properties;
	}

	/**
	 * Return the vertex property of the vertex
	 * at the given index and at the provided
	 * valid vertex property template index.
	 *
	 * @param index The index of the vertex
	 * @return property The property value.
	 */
	template<const int PropertyIndex>
	decltype(std::get<PropertyIndex>(std::declval<Vertex>().properties))& getVertexProperty(
			const int index) {
		auto v = vertex(index, *_graph.get());
		return std::get<PropertyIndex>((*_graph.get())[v].properties);
	}

	/**
	 * Set the weight on the edge between the vertices at the
	 * provided indices.
	 *
	 * @param srcIndex The starting vertex index
	 * @param tgtIndex The ending vertex index
	 * @param weight The weight to set.
	 */
	void setEdgeWeight(const int srcIndex, const int tgtIndex, const double weight) {
		auto e = edge(vertex(srcIndex, *_graph.get()), vertex(tgtIndex, *_graph.get()), *_graph.get());
		(*_graph.get())[e.first].weight = weight;
	}

	/**
	 * Return the edge weight at the edge between
	 * the provided vertices.
	 *
	 * @param srcIndex The starting vertex index
	 * @param tgtIndex The ending vertex index
	 * @return The edge weight
	 */
	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;
	}

	/**
	 * Return true if there is an edge between the
	 * two vertices at the given vertex indices.
	 *
	 * @param srcIndex The starting vertex index
	 * @param tgtIndex The ending vertex index
	 * @return exists Boolean indicating if edge exists or not
	 */
	bool edgeExists(const int srcIndex, const int tgtIndex) {
		return edge(vertex(srcIndex, *_graph.get()),
				vertex(tgtIndex, *_graph.get()), *_graph.get()).second;
	}

	/**
	 * Return the vertex degree at the given vertex index.
	 *
	 * @param index The index of the vertex
	 * @return degree The degree of the vertex
	 */
	int degree(const int index) {
		return boost::degree(vertex(index, *_graph.get()), *_graph.get());
	}

	/**
	 * Return the diameter of this Graph.
	 *
	 * @return diameter The graph diameter
	 */
	int diameter() {
		// Set some aliases we'll need
		using DistanceProperty = boost::exterior_vertex_property<adj_list, int>;
		using DistanceMatrix = typename DistanceProperty::matrix_type;
		using DistanceMatrixMap = typename DistanceProperty::matrix_map_type;
		using EccentricityProperty = boost::exterior_vertex_property<adj_list, int>;
		using EccentricityContainer = typename EccentricityProperty::container_type;
		using EccentricityMap = typename EccentricityProperty::map_type;

		// Construct the distance mapping
		DistanceMatrix distances(order());
		DistanceMatrixMap dm(distances, *_graph.get());
		constant_property_map<edge_type, int> wm(1);

		// Compute the shortest paths
		floyd_warshall_all_pairs_shortest_paths(*_graph.get(), dm, weight_map(wm));

		// Now get diameter information
		EccentricityContainer eccs(order());
		EccentricityMap em(eccs, *_graph.get());

		// Return the diameter
		return all_eccentricities(*_graph.get(), dm, em).second;
	}

	/**
	 * Return the number of edges.
	 * @return nEdges The number of edges.
	 */
	int size() {
		return num_edges(*_graph.get());
	}

	/**
	 * Return the number of vertices in this graph
	 *
	 * @return nVerts The number of vertices.
	 */
	int order() {
		return num_vertices(*_graph.get());
	}

	/**
	 * Write this graph in a graphviz dot format to the
	 * provided ostream.
	 *
	 * @param stream
	 */
	void write(std::ostream& stream) {
		std::stringstream ss;
		XACCVertexPropertiesWriter writer(*_graph.get());
		boost::write_graphviz(ss, *_graph.get(), writer);
		auto str = ss.str();
		// Modify the style...
		str = str.insert(9, "\n{\nnode [shape=box style=filled]");

		std::vector<std::string> splitVec;
		boost::split(splitVec, str, boost::is_any_of("\n"));
		splitVec.insert(splitVec.begin() + 3 + order(), "}");

		std::stringstream combine;
		std::for_each(splitVec.begin(), splitVec.end(), [&](const std::string& elem) { combine << elem << "\n"; });
		stream << combine.str().substr(0, combine.str().size() - 2);
	}

	/**
	 * Read in a graphviz dot graph from the given input
	 * stream. This is left for subclasses.
	 *
	 * @param stream
	 */
	virtual void read(std::istream& stream) {
		stream.fail();
		XACCError("Reading must be implemented by subclasses.");
	}
};

}

#endif