Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional keywords to GraphOptimizationApplication.draw #52

Open
t-imamichi opened this issue Mar 19, 2021 · 2 comments
Open

Add optional keywords to GraphOptimizationApplication.draw #52

t-imamichi opened this issue Mar 19, 2021 · 2 comments
Labels
on hold Can not fix yet

Comments

@t-imamichi
Copy link
Collaborator

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

@a-matsuo
Copy link
Contributor

a-matsuo commented Mar 24, 2021

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.

@mtreinish
Copy link
Collaborator

mtreinish commented Apr 7, 2021

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: Qiskit/rustworkx#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on hold Can not fix yet
Projects
None yet
Development

No branches or pull requests

4 participants