Skip to content

Commit

Permalink
move to_latex back to latex_generation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Davis committed Nov 1, 2019
1 parent b1b9015 commit 99003e6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 71 deletions.
4 changes: 2 additions & 2 deletions pyquil/latex/__init__.py
Original file line number Diff line number Diff line change
@@ -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
71 changes: 47 additions & 24 deletions pyquil/latex/_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions pyquil/latex/_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 17 additions & 43 deletions pyquil/latex/latex_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 99003e6

Please sign in to comment.