1414
1515"""Convenience functions for showing rich displays in Jupyter notebook."""
1616
17+ import os
1718from typing import Dict , Sequence , TYPE_CHECKING , Union
1819
1920import IPython .display
2324from .flame_graph import get_flame_graph_svg_data
2425from .graphviz import PrettyGraphDrawer , TypedGraphDrawer
2526from .musical_score import draw_musical_score , get_musical_score_data
27+ from .qpic_diagram import qpic_diagram_for_bloq
2628
2729if TYPE_CHECKING :
2830 import networkx as nx
@@ -36,17 +38,20 @@ def show_bloq(bloq: 'Bloq', type: str = 'graph'): # pylint: disable=redefined-b
3638
3739 Args:
3840 bloq: The bloq to show
39- type: Either 'graph', 'dtype', or 'musical_score'. By default, display a directed acyclic
40- graph of the bloq connectivity. If dtype then the connections are
41- labelled with their dtypes rather than bitsizes. Otherwise, draw a
42- musical score diagram.
41+ type: Either 'graph', 'dtype', 'musical_score' or 'latex'. By default, display
42+ a directed acyclic graph of the bloq connectivity. If dtype then the
43+ connections are labelled with their dtypes rather than bitsizes. If 'latex',
44+ then latex diagrams are drawn using `qpic`, which should be installed already
45+ and is invoked via a subprocess.run() call. Otherwise, draw a musical score diagram.
4346 """
4447 if type .lower () == 'graph' :
4548 IPython .display .display (PrettyGraphDrawer (bloq ).get_svg ())
4649 elif type .lower () == 'dtype' :
4750 IPython .display .display (TypedGraphDrawer (bloq ).get_svg ())
4851 elif type .lower () == 'musical_score' :
4952 draw_musical_score (get_musical_score_data (bloq ))
53+ elif type .lower () == 'latex' :
54+ show_bloq_via_qpic (bloq )
5055 else :
5156 raise ValueError (f"Unknown `show_bloq` type: { type } ." )
5257
@@ -84,3 +89,13 @@ def show_flame_graph(*bloqs: 'Bloq', **kwargs):
8489 """Display hiearchical decomposition and T-complexity costs as a Flame Graph."""
8590 svg_data = get_flame_graph_svg_data (* bloqs , ** kwargs )
8691 IPython .display .display (IPython .display .SVG (svg_data ))
92+
93+
94+ def show_bloq_via_qpic (bloq : 'Bloq' , width : int = 1000 , height : int = 400 ):
95+ """Display latex diagram for bloq by invoking `qpic`. Assumes qpic is already installed."""
96+ output_file_path = qpic_diagram_for_bloq (bloq , output_type = 'png' )
97+
98+ from IPython .display import Image
99+
100+ IPython .display .display (Image (output_file_path , width = width , height = height ))
101+ os .remove (output_file_path )
0 commit comments