Skip to content

Commit

Permalink
Add flag to latex drawer to enable label passthrough
Browse files Browse the repository at this point in the history
This commit adds a new flag latex_labels to the circuit_drawer for use
with the latex and latex_source drawer. When it is set to true it
disables the pylatexenc conversion from unicode input to a latex
encoding. This enables users who want to manual create gate names that
are properly latex encoded (for example to have gate names with
subscripts) to do so.

Fixes #3171
  • Loading branch information
mtreinish committed Sep 30, 2019
1 parent 352f2bd commit 632559f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
9 changes: 7 additions & 2 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def qasm(self):
def draw(self, scale=0.7, filename=None, style=None, output=None,
interactive=False, line_length=None, plot_barriers=True,
reverse_bits=False, justify=None, vertical_compression='medium', idle_wires=True,
with_layout=True):
with_layout=True, latex_labels=False):
"""Draw the quantum circuit
Using the output parameter you can specify the format. The choices are:
Expand Down Expand Up @@ -604,6 +604,10 @@ def draw(self, scale=0.7, filename=None, style=None, output=None,
idle_wires (bool): Include idle wires. Default is True.
with_layout (bool): Include layout information, with labels on the physical
layout. Default is True.
latex_labels (bool): When set to true input labels from the circuit
are treated as already encoded for latex and no conversion is
performed. (latex and latex_source only)
Returns:
PIL.Image or matplotlib.figure or str or TextDrawing:
* PIL.Image: (output `latex`) an in-memory representation of the
Expand All @@ -629,7 +633,8 @@ def draw(self, scale=0.7, filename=None, style=None, output=None,
justify=justify,
vertical_compression=vertical_compression,
idle_wires=idle_wires,
with_layout=with_layout)
with_layout=with_layout,
latex_labels=latex_labels)

def size(self):
"""Returns total number of gate operations in circuit.
Expand Down
31 changes: 24 additions & 7 deletions qiskit/visualization/circuit_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def circuit_drawer(circuit,
justify=None,
vertical_compression='medium',
idle_wires=True,
with_layout=True):
with_layout=True,
latex_labels=False):
"""Draw a quantum circuit to different formats (set by output parameter):
0. text: ASCII art TextDrawing that can be printed in the console.
1. latex: high-quality images, but heavy external software dependencies
Expand Down Expand Up @@ -102,6 +103,9 @@ def circuit_drawer(circuit,
idle_wires (bool): Include idle wires. Default is True.
with_layout (bool): Include layout information, with labels on the physical
layout.
latex_labels (bool): When set to true input labels from the circuit
are treated as already encoded for latex and no conversion is
performed. (latex and latex_source only)
Returns:
PIL.Image: (output `latex`) an in-memory representation of the image
of the circuit diagram.
Expand Down Expand Up @@ -237,7 +241,8 @@ def circuit_drawer(circuit,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
with_layout=with_layout)
with_layout=with_layout,
latex_labels=latex_labels)
elif output == 'latex_source':
return _generate_latex_source(circuit,
filename=filename, scale=scale,
Expand All @@ -246,7 +251,8 @@ def circuit_drawer(circuit,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
with_layout=with_layout)
with_layout=with_layout,
latex_labels=latex_labels)
elif output == 'mpl':
image = _matplotlib_circuit_drawer(circuit, scale=scale,
filename=filename, style=style,
Expand Down Expand Up @@ -396,7 +402,8 @@ def _latex_circuit_drawer(circuit,
reverse_bits=False,
justify=None,
idle_wires=True,
with_layout=True):
with_layout=True,
latex_labels=False):
"""Draw a quantum circuit based on latex (Qcircuit package)
Requires version >=2.6.0 of the qcircuit LaTeX package.
Expand All @@ -415,6 +422,10 @@ def _latex_circuit_drawer(circuit,
idle_wires (bool): Include idle wires. Default is True.
with_layout (bool): Include layout information, with labels on the physical
layout. Default: True
latex_labels (bool): When set to true input labels from the circuit
are treated as already encoded for latex and no conversion is
performed.
Returns:
PIL.Image: an in-memory representation of the circuit diagram
Expand All @@ -431,7 +442,8 @@ def _latex_circuit_drawer(circuit,
scale=scale, style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits, justify=justify,
idle_wires=idle_wires, with_layout=with_layout)
idle_wires=idle_wires, with_layout=with_layout,
latex_labels=latex_labels)
try:

subprocess.run(["pdflatex", "-halt-on-error",
Expand Down Expand Up @@ -478,7 +490,7 @@ def _latex_circuit_drawer(circuit,
def _generate_latex_source(circuit, filename=None,
scale=0.7, style=None, reverse_bits=False,
plot_barriers=True, justify=None, idle_wires=True,
with_layout=True):
with_layout=True, latex_labels=False):
"""Convert QuantumCircuit to LaTeX string.
Args:
Expand All @@ -495,6 +507,10 @@ def _generate_latex_source(circuit, filename=None,
idle_wires (bool): Include idle wires. Default is True.
with_layout (bool): Include layout information, with labels on the physical
layout. Default: True
latex_labels (bool): When set to true input labels from the circuit
are treated as already encoded for latex and no conversion is
performed.
Returns:
str: Latex string appropriate for writing to file.
"""
Expand All @@ -508,7 +524,8 @@ def _generate_latex_source(circuit, filename=None,

qcimg = _latex.QCircuitImage(qregs, cregs, ops, scale, style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits, layout=layout)
reverse_bits=reverse_bits, layout=layout,
latex_labels=latex_labels)
latex = qcimg.latex()
if filename:
with open(filename, 'w') as latex_file:
Expand Down
12 changes: 10 additions & 2 deletions qiskit/visualization/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class QCircuitImage:
"""

def __init__(self, qubits, clbits, ops, scale, style=None,
plot_barriers=True, reverse_bits=False, layout=None):
plot_barriers=True, reverse_bits=False, layout=None,
latex_labels=False):
"""
Args:
qubits (list[Qubit]): list of qubits
Expand All @@ -59,6 +60,9 @@ def __init__(self, qubits, clbits, ops, scale, style=None,
circuit. Defaults to True.
layout (Layout or None): If present, the layout information will be
included.
latex_labels (bool): When set to true input labels from the circuit
are treated as already encoded for latex and no conversion is
performed.
Raises:
ImportError: If pylatexenc is not installed
"""
Expand Down Expand Up @@ -122,6 +126,7 @@ def __init__(self, qubits, clbits, ops, scale, style=None,
self.reverse_bits = reverse_bits
self.layout = layout
self.plot_barriers = plot_barriers
self.latex_labels = latex_labels

#################################
self.qregs = _get_register_specs(qubits)
Expand Down Expand Up @@ -367,7 +372,10 @@ def _build_latex_array(self, aliases=None):
'b').zfill(self.cregs[if_reg])[::-1]
if op.name not in ['measure', 'barrier', 'snapshot', 'load',
'save', 'noise']:
nm = utf8tolatex(op.name).replace(" ", "\\,")
if not self.latex_labels:
nm = utf8tolatex(op.name).replace(" ", "\\,")
else:
nm = op.name
qarglist = op.qargs
if aliases is not None:
qarglist = map(lambda x: aliases[x], qarglist)
Expand Down
11 changes: 11 additions & 0 deletions releasenotes/notes/latex-labels-9f41997194ef753b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
features:
- |
A new boolean kwarg flag, ``latex_labels``, has been added to
``qiskit.visualization.circuit_drawer`` and ``qiskit.QuantumCircuit.draw``.
This flag only is used by the ``latex`` and ``latex_source`` circuit drawer
backends. When set on all other backends it will be ignored. When this flag
is set to ``True`` with the latex drawers it will disable the automated
conversion of input gate names from unicode to a latex encoding. This
enables users who want to manually format gate names for rendering in latex
to do so.

0 comments on commit 632559f

Please sign in to comment.