dwave.optimization.model.Model.to_networkx#

Model.to_networkx()#

Convert the model to a NetworkX graph.

Returns:

A NetworkX graph.

Examples

This example converts a model to a graph.

>>> from dwave.optimization.model import Model
>>> model = Model()
>>> one = model.constant(1)
>>> two = model.constant(2)
>>> i = model.integer()
>>> model.minimize(two * i - one)
>>> G = model.to_networkx()  

One advantage of converting to NetworkX is the wide availability of drawing tools. See NetworkX’s drawing documentation.

This example uses DAGVIZ to draw the NetworkX graph created in the example above.

>>> import dagviz                      
>>> r = dagviz.render_svg(G)           
>>> with open("model.svg", "w") as f:  
...     f.write(r)

This creates the following image:

Image of NetworkX Directed Graph