Open t-imamichi opened 3 years ago
Currently, matplotlib is used with networkx to draw a graph. However, we might change the library for visualization. @mtreinish is working on, so please leave this issue for now.
Right, I'm hoping to migrate qiskit-optimization to use retworkx internally instead of networkx for performance. Right now retworkx only supports generating a DOT
file for visualization with graphviz. You should be able to use graphviz in the same way you're using the networkx matplotlib drawer, but the api for accomplishing it is slightly different. For example, something like:
import retworkx
import pydot
graph = retworkx.undirected_gnm_random_graph(7, 10, seed=123)
graph.update_edge(2, 4, 'red')
graph.update_edge(1, 3, 'red')
graph.update_edge(1, 5, 'red')
def node_attrs(node):
out_dict = {'style': 'filled'}
if node % 2 == 0:
out_dict['fillcolor'] = 'red'
else:
out_dict['fillcolor'] = 'grey'
return out_dict
def edge_attrs(edge):
out_dict = {}
if edge:
out_dict['color'] = edge
out_dict['penwidth'] = '4.0'
return out_dict
dot_str = graph.to_dot(node_attrs, edge_attrs)
dot = pydot.graph_from_dot_data(dot_str)[0]
dot.write_png('graph.png')
generates a graph image like:
That being said for the next retworkx release I'm working on adding a matplotlib visualization function: https://github.com/Qiskit/retworkx/pull/304 (it's still pretty rough, but hopefully I'll be able to clean it up before too long) so we might not need to adapt things here if/when we switch to retworkx since if we wait for retworkx 0.9 we hopefully should have a matplotlib drawer available.
As an aside as we're preparing the networkx release please file issues at https://github.com/Qiskit/retworkx/issues for any gaps in retworkx for things that qiskit-optimization needs so we can make sure retworkx has all the functionality needed for qiskit-optimization.
What is the expected enhancement?
If we add optional keywords (i.e., kwargs) to
GraphOptimizationApplication.draw
, users can control all options of networkx.draw. https://networkx.org/documentation/stable/reference/generated/networkx.drawing.nx_pylab.draw.html#networkx.drawing.nx_pylab.draw