From 99003e6b097166ef9d05a20774b1145c8a44cfe6 Mon Sep 17 00:00:00 2001 From: Erik Davis Date: Fri, 1 Nov 2019 10:11:05 -0700 Subject: [PATCH] move to_latex back to latex_generation.py --- pyquil/latex/__init__.py | 4 +- pyquil/latex/_diagram.py | 71 +++++++++++++++++++++----------- pyquil/latex/_ipython.py | 4 +- pyquil/latex/latex_generation.py | 60 ++++++++------------------- 4 files changed, 68 insertions(+), 71 deletions(-) diff --git a/pyquil/latex/__init__.py b/pyquil/latex/__init__.py index 43d8887c7..519943210 100644 --- a/pyquil/latex/__init__.py +++ b/pyquil/latex/__init__.py @@ -1,3 +1,3 @@ -from pyquil.latex.latex_generation import DiagramSettings -from pyquil.latex._diagram import to_latex +from pyquil.latex.latex_generation import to_latex +from pyquil.latex._diagram import DiagramSettings from pyquil.latex._ipython import display diff --git a/pyquil/latex/_diagram.py b/pyquil/latex/_diagram.py index 3d3c664bc..51cb0b518 100644 --- a/pyquil/latex/_diagram.py +++ b/pyquil/latex/_diagram.py @@ -28,19 +28,61 @@ ClassicalMove, ClassicalExchange, ClassicalConvert, ClassicalLoad, ClassicalStore, ClassicalComparison, RawInstr) -from pyquil.latex.latex_generation import DiagramSettings from collections import defaultdict -from typing import Optional if sys.version_info < (3, 7): - from pyquil.external.dataclasses import replace + from pyquil.external.dataclasses import dataclass, replace else: - from dataclasses import replace + from dataclasses import dataclass, replace + + +@dataclass +class DiagramSettings: + """ + Settings to control the layout and rendering of circuits. + """ + + texify_numerical_constants: bool = True + """ + Convert numerical constants, such as pi, to LaTeX form. + """ + + impute_missing_qubits: bool = False + """ + Include qubits with indices between those explicitly referenced in the Quil program. + + For example, if true, the diagram for `CNOT 0 2` would have three qubit lines: 0, 1, 2. + """ + + label_qubit_lines: bool = True + """ + Label qubit lines. + """ + + abbreviate_controlled_rotations: bool = False + """ + Write controlled rotations in a compact form. + + For example, `RX(pi)` as `X_{\\pi}`, instead of the longer `R_X(\\pi)` + """ + + qubit_line_open_wire_length: int = 1 + """ + The length by which qubit lines should be extended with open wires at the right of the diagram. + + The default of 1 is the natural choice. The main reason for including this option + is that it may be appropriate for this to be 0 in subdiagrams. + """ + + right_align_terminal_measurements: bool = True + """ + Align measurement operations which appear at the end of the program. + """ # Overview of LaTeX generation. # -# The main entry point is the `to_latex` function below. Here are some high +# The main entry point is the `to_latex` function. Here are some high # points of the generation procedure: # # - The most basic building block are the TikZ operators, which are constructed @@ -62,25 +104,6 @@ # groups are currently not supported. -def to_latex(circuit: Program, settings: Optional[DiagramSettings] = None) -> str: - """ - Translates a given pyquil Program to a TikZ picture in a LaTeX document. - - :param Program circuit: The circuit to be drawn, represented as a pyquil program. - :param DiagramSettings settings: An optional object of settings controlling diagram rendering and layout. - :return: LaTeX document string which can be compiled. - :rtype: string - """ - if settings is None: - settings = DiagramSettings() - text = header() - text += "\n" - text += body(circuit, settings) - text += "\n" - text += footer() - return text - - def header(): """ Writes the LaTeX header using the settings file. diff --git a/pyquil/latex/_ipython.py b/pyquil/latex/_ipython.py index a730e27a3..4e7fc6f51 100644 --- a/pyquil/latex/_ipython.py +++ b/pyquil/latex/_ipython.py @@ -22,8 +22,8 @@ from IPython.display import Image from pyquil import Program -from pyquil.latex.latex_generation import DiagramSettings -from pyquil.latex._diagram import to_latex +from pyquil.latex.latex_generation import to_latex +from pyquil.latex._diagram import DiagramSettings def display(circuit: Program, settings: Optional[DiagramSettings] = None, diff --git a/pyquil/latex/latex_generation.py b/pyquil/latex/latex_generation.py index b48bfbdf9..1b5c10b9a 100644 --- a/pyquil/latex/latex_generation.py +++ b/pyquil/latex/latex_generation.py @@ -15,52 +15,26 @@ ############################################################################## import sys +from typing import Optional -if sys.version_info < (3, 7): - from pyquil.external.dataclasses import dataclass -else: - from dataclasses import dataclass +from pyquil import Program +from pyquil.latex._diagram import header, body, footer, DiagramSettings -@dataclass -class DiagramSettings: - """ - Settings to control the layout and rendering of circuits. - """ - - texify_numerical_constants: bool = True - """ - Convert numerical constants, such as pi, to LaTeX form. - """ - - impute_missing_qubits: bool = False - """ - Include qubits with indices between those explicitly referenced in the Quil program. - - For example, if true, the diagram for `CNOT 0 2` would have three qubit lines: 0, 1, 2. - """ - - label_qubit_lines: bool = True - """ - Label qubit lines. - """ - - abbreviate_controlled_rotations: bool = False - """ - Write controlled rotations in a compact form. - - For example, `RX(pi)` as `X_{\\pi}`, instead of the longer `R_X(\\pi)` +def to_latex(circuit: Program, settings: Optional[DiagramSettings] = None) -> str: """ + Translates a given pyquil Program to a TikZ picture in a LaTeX document. - qubit_line_open_wire_length: int = 1 - """ - The length by which qubit lines should be extended with open wires at the right of the diagram. - - The default of 1 is the natural choice. The main reason for including this option - is that it may be appropriate for this to be 0 in subdiagrams. - """ - - right_align_terminal_measurements: bool = True - """ - Align measurement operations which appear at the end of the program. + :param Program circuit: The circuit to be drawn, represented as a pyquil program. + :param DiagramSettings settings: An optional object of settings controlling diagram rendering and layout. + :return: LaTeX document string which can be compiled. + :rtype: string """ + if settings is None: + settings = DiagramSettings() + text = header() + text += "\n" + text += body(circuit, settings) + text += "\n" + text += footer() + return text