Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to latex drawer to enable label passthrough #3172

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ 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, idle_wires=True, vertical_compression='medium',
with_layout=True, fold=None):
reverse_bits=False, justify=None, vertical_compression='medium', idle_wires=True,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked the source of this diff and it looks #3108 accidentally changed this (my rebase reverted it back to the previous order). See the last release: https://github.com/Qiskit/qiskit-terra/blob/0.9.0/qiskit/circuit/quantumcircuit.py#L534

This actually fixes a potentially breaking change because the order of kwargs is part of the api. (unless we specified a ** to indicate keyword only, which we can't introduce for the same backwards compat concerns)

with_layout=True, fold=None, latex_labels=False):
"""Draw the quantum circuit

Using the output parameter you can specify the format. The choices are:
Expand Down Expand Up @@ -610,6 +610,10 @@ def draw(self, scale=0.7, filename=None, style=None, output=None,
guess the console width using `shutil.get_terminal_size()`. However, if
running in jupyter, the default line length is set to 80 characters.
In `mpl` is the amount of operations before folding. Default is 25.
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 @@ -635,7 +639,9 @@ 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, fold=fold)
with_layout=with_layout,
fold=fold,
latex_labels=latex_labels)

def size(self):
"""Returns total number of gate operations in circuit.
Expand Down
32 changes: 25 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,
vertical_compression='medium',
idle_wires=True,
with_layout=True,
fold=None):
fold=None,
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 @@ -103,6 +104,10 @@ def circuit_drawer(circuit,
guess the console width using `shutil.get_terminal_size()`. However, if
running in jupyter, the default line length is set to 80 characters.
In `mpl` is the amount of operations before folding. Default is 25.
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 @@ -239,7 +244,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 @@ -248,7 +254,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 @@ -403,7 +410,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 @@ -422,6 +430,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 @@ -438,7 +450,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 @@ -485,7 +498,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 @@ -502,6 +515,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 @@ -515,7 +532,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.