Skip to content

Commit

Permalink
Change print_matrix so that it actually displays the matrix.
Browse files Browse the repository at this point in the history
(Also: update "AKLT hexagonal lattice.ipynb" so it no longer uses the hack of wrapping print_matrix with a call to display.)

Fixes #145.
  • Loading branch information
dlyongemallo committed Apr 18, 2024
1 parent 4e1ab6e commit fea52e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions demos/AKLT/AKLT hexagonal lattice.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@
"zx.hsimplify.zh_simp(g,quiet=True)\n",
"zx.draw(g)\n",
"print(\"Indeed, these implement the same linear map up to global scalar:\")\n",
"display(zx.print_matrix(diagram))\n",
"display(zx.print_matrix(g))"
"zx.print_matrix(diagram)\n",
"zx.print_matrix(g)"
]
},
{
Expand Down
17 changes: 9 additions & 8 deletions pyzx/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__all__ = ['draw', 'arrange_scalar_diagram', 'draw_matplotlib', 'draw_d3',
__all__ = ['draw', 'arrange_scalar_diagram', 'draw_matplotlib', 'draw_d3',
'matrix_to_latex', 'print_matrix', 'graphs_to_gif']

import os
Expand Down Expand Up @@ -588,18 +588,19 @@ def matrix_to_latex(m: np.ndarray) -> str:
out += "\n\\end{pmatrix}\n\\end{equation}"
return out

def print_matrix(m: Union[np.ndarray,BaseGraph,Circuit]) -> 'Label':
"""Returns a Label() Jupyter widget
that displays a pretty latex representation of the given matrix.

def print_matrix(m: Union[np.ndarray,BaseGraph,Circuit]) -> None:
"""Display a Label() Jupyter widget with a pretty LaTeX representation of the given matrix.
Instead of a matrix, can also give a Circuit or Graph.
"""
if get_mode() != "notebook":
raise TypeError("Unsupported mode for print_matrix: '{}'".format(get_mode()))

from ipywidgets import Label
if isinstance(m,BaseGraph) or isinstance(m,Circuit):
if isinstance(m, BaseGraph) or isinstance(m, Circuit):
m = m.to_matrix()

return Label(matrix_to_latex(m))


display(Label(matrix_to_latex(m)))


def graphs_to_gif(graphs: List[BaseGraph], filename: str, frame_duration: float=0.5):
Expand Down

0 comments on commit fea52e6

Please sign in to comment.