qiskit-optimization icon indicating copy to clipboard operation
qiskit-optimization copied to clipboard

Add optional keywords to `GraphOptimizationApplication.draw`

Open t-imamichi opened this issue 4 years ago • 2 comments

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

t-imamichi avatar Mar 19 '21 14:03 t-imamichi

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.

a-matsuo avatar Mar 24 '21 14:03 a-matsuo

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:

graph

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.

mtreinish avatar Apr 07 '21 11:04 mtreinish