Skip to content

Commit

Permalink
Remove deprecated functions, options, and behavior from circuit_drawer (
Browse files Browse the repository at this point in the history
Qiskit#1567)

* Remove deprecated functions, options, and behavior from circuit_drawer

In the 0.7 release we deprecated several functions, options, and
behaviors in the circuit drawers. Now that we've got that release out
the door we can stage the removal of those things. This commit does that
and removes all the things we deprecated to stage for removal in the
circuit drawers.

* Fix test imports

* Fix whitespace

* Fix lint
  • Loading branch information
mtreinish authored and jaygambetta committed Jan 5, 2019
1 parent 266097d commit 1d146cc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 167 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The format is based on `Keep a Changelog`_.

`UNRELEASED`_
=============

Added
-----

Expand All @@ -36,7 +37,9 @@ Changed
``slot`` (#1615).
- The test folders have been reorganized to match the python modules (#1625)
- The circuits_to_qobj no longers uses the unrollers (#1629)

- The previously deprecated default output of ``circuit_drawer()`` (using latex
and falling back to mpl) is no longer present. Instead the default output
is the ascii art ``text`` output backend.

Fixed
-----
Expand All @@ -47,6 +50,15 @@ Fixed
Removed
-------

- The previously deprecated functions ``plot_circuit()``,
``latex_circuit_drawer()``, ``generate_latex_source()``, and
``matplotlib_circuit_drawer()`` from ``qiskit.tools.visualization`` have
been removed. The ``circuit_drawer()`` function from the same module should
be used instead.
- The previously deprecated keys ``plot_barriers`` and ``reverse_bits`` keys in
the ``style`` kwarg dict are deprecated, instead the
``qiskit.tools.visualization.circuit_drawer()`` kwargs ``plot_barriers`` and
``reverse_bits`` should be used instead.
- Removed the wrapper folder as part of the post 0.7 cleanup (#1613).
- Removed the python wrappers of the legacy simualtors now that
Qiskit Aer is out (#1615).
Expand Down
3 changes: 1 addition & 2 deletions qiskit/tools/visualization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
plot_state_qsphere,
plot_state)

from ._circuit_visualization import circuit_drawer, plot_circuit, generate_latex_source, \
latex_circuit_drawer, matplotlib_circuit_drawer, _text_circuit_drawer, qx_color_scheme
from ._circuit_visualization import circuit_drawer, qx_color_scheme
from .exceptions import VisualizationError
from ._matplotlib import HAS_MATPLOTLIB

Expand Down
185 changes: 26 additions & 159 deletions qiskit/tools/visualization/_circuit_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import os
import subprocess
import tempfile
import warnings

from PIL import Image

Expand All @@ -34,25 +33,11 @@
logger = logging.getLogger(__name__)


def plot_circuit(circuit,
basis="id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,rx,ry,rz,"
"cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,cswap",
scale=0.7,
style=None):
"""Plot and show circuit (opens new window, cannot inline in Jupyter)"""
warnings.warn('The plot_circuit() function is deprecated and will be '
'removed in the future. Instead use circuit_drawer() with '
'the `interactive` flag set true', DeprecationWarning)
image = circuit_drawer(circuit, scale=scale, style=style)
if image:
image.show()


def circuit_drawer(circuit,
scale=0.7,
filename=None,
style=None,
output=None,
output='text',
interactive=False,
line_length=None,
plot_barriers=True,
Expand Down Expand Up @@ -166,10 +151,6 @@ def circuit_drawer(circuit,
fold (int): The number of circuit elements to fold the circuit at.
Defaults to 20 (`mpl` only)
cregbundle (bool): If set True bundle classical registers (`mpl` only)
plotbarrier (bool): Enable/disable drawing barriers in the output
circuit. Defaults to True. This is deprecated in the style dict
and will be removed in a future release. Use the `plot_barriers`
kwarg instead.
showindex (bool): If set True draw an index. (`mpl` only)
compress (bool): If set True draw a compressed circuit (`mpl` only)
figwidth (int): The maximum width (in inches) for the output figure.
Expand All @@ -180,65 +161,34 @@ def circuit_drawer(circuit,
creglinestyle (str): The style of line to use for classical registers.
Choices are `'solid'`, `'doublet'`, or any valid matplotlib
`linestyle` kwarg value. Defaults to `doublet`(`mpl` only)
reversebits (bool): When set to True reverse the bit order inside
registers for the output visualization. This is deprecated in the
style dict and will be removed in a future release use the
`reverse_bits` kwarg instead.
"""
image = None
if style:
if 'reversebits' in style:
warnings.warn('The reversebits key in style is deprecated and will'
'not work in the future. Instead use the '
'``reverse_bits`` kwarg.', DeprecationWarning)
reverse_bits = style.get('reversebits')
if 'plotbarrier' in style:
warnings.warn('The plotbarrier key in style is deprecated and will'
'not work in the future. Instead use the '
'``plot_barriers`` kwarg.', DeprecationWarning)
plot_barriers = style.get('plotbarrier')

if not output:
warnings.warn('The current behavior for the default output will change'
' in a future release. Instead of trying latex and '
'falling back to mpl on failure it will just use '
'"text" by default', DeprecationWarning)
try:
image = _latex_circuit_drawer(circuit, scale, filename, style)
except (OSError, subprocess.CalledProcessError, FileNotFoundError):
if _matplotlib.HAS_MATPLOTLIB:
image = _matplotlib_circuit_drawer(circuit, scale, filename,
style)
else:
raise ImportError('The default output needs matplotlib. '
'Run "pip install matplotlib" before.')

if output == 'text':
return _text_circuit_drawer(circuit, filename=filename,
line_length=line_length,
reversebits=reverse_bits,
plotbarriers=plot_barriers)
elif output == 'latex':
image = _latex_circuit_drawer(circuit, scale=scale,
filename=filename, style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits)
elif output == 'latex_source':
return _generate_latex_source(circuit,
filename=filename, scale=scale,
style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits)
elif output == 'mpl':
image = _matplotlib_circuit_drawer(circuit, scale=scale,
filename=filename, style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits)
else:
if output == 'text':
return _text_circuit_drawer(circuit, filename=filename,
line_length=line_length,
reversebits=reverse_bits,
plotbarriers=plot_barriers)
elif output == 'latex':
image = _latex_circuit_drawer(circuit, scale=scale,
filename=filename, style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits)
elif output == 'latex_source':
return _generate_latex_source(circuit,
filename=filename, scale=scale,
style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits)
elif output == 'mpl':
image = _matplotlib_circuit_drawer(circuit, scale=scale,
filename=filename, style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits)
else:
raise exceptions.VisualizationError(
'Invalid output type %s selected. The only valid choices '
'are latex, latex_source, text, and mpl' % output)
raise exceptions.VisualizationError(
'Invalid output type %s selected. The only valid choices '
'are latex, latex_source, text, and mpl' % output)
if image and interactive:
image.show()
return image
Expand Down Expand Up @@ -352,38 +302,6 @@ def _text_circuit_drawer(circuit, filename=None, line_length=None, reversebits=F
# latex_circuit_drawer
# -----------------------------------------------------------------------------

def latex_circuit_drawer(circuit,
basis="id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,rx,ry,rz,"
"cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,cswap",
scale=0.7,
filename=None,
style=None):
"""Draw a quantum circuit based on latex (Qcircuit package)
Requires version >=2.6.0 of the qcircuit LaTeX package.
Args:
circuit (QuantumCircuit): a quantum circuit
basis (str): comma separated list of gates
scale (float): scaling factor
filename (str): file path to save image to
style (dict or str): dictionary of style or file name of style file
Returns:
PIL.Image: an in-memory representation of the circuit diagram
Raises:
OSError: usually indicates that ```pdflatex``` or ```pdftocairo``` is
missing.
CalledProcessError: usually points errors during diagram creation.
"""
warnings.warn('The latex_circuit_drawer() function is deprecated and will '
'be removed in a future release. Instead use the '
'circuit_drawer() function with the `output` kwarg set to '
'`latex`.', DeprecationWarning)
return _latex_circuit_drawer(circuit, scale=scale,
filename=filename, style=style)


def _latex_circuit_drawer(circuit,
scale=0.7,
Expand Down Expand Up @@ -460,30 +378,6 @@ def _latex_circuit_drawer(circuit,
return image


def generate_latex_source(circuit, filename=None,
basis="id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,rx,ry,rz,"
"cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,cswap",
scale=0.7, style=None):
"""Convert QuantumCircuit to LaTeX string.
Args:
circuit (QuantumCircuit): input circuit
scale (float): image scaling
filename (str): optional filename to write latex
basis (str): optional comma-separated list of gate names
style (dict or str): dictionary of style or file name of style file
Returns:
str: Latex string appropriate for writing to file.
"""
warnings.warn('The generate_latex_source() function is deprecated and will'
' be removed in a future release. Instead use the '
'circuit_drawer() function with the `output` kwarg set to '
'`latex_source`.', DeprecationWarning)
return _generate_latex_source(circuit, filename=filename,
scale=scale, style=style)


def _generate_latex_source(circuit, filename=None,
scale=0.7, style=None, reverse_bits=False,
plot_barriers=True):
Expand Down Expand Up @@ -518,33 +412,6 @@ def _generate_latex_source(circuit, filename=None,
# matplotlib_circuit_drawer
# -----------------------------------------------------------------------------

def matplotlib_circuit_drawer(circuit,
basis='id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,rx,ry,rz,'
'cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,cswap',
scale=0.7,
filename=None,
style=None):
"""Draw a quantum circuit based on matplotlib.
If `%matplotlib inline` is invoked in a Jupyter notebook, it visualizes a circuit inline.
We recommend `%config InlineBackend.figure_format = 'svg'` for the inline visualization.
Args:
circuit (QuantumCircuit): a quantum circuit
basis (str): comma separated list of gates
scale (float): scaling factor
filename (str): file path to save image to
style (dict or str): dictionary of style or file name of style file
Returns:
PIL.Image: an in-memory representation of the circuit diagram
"""
warnings.warn('The matplotlib_circuit_drawer() function is deprecated and '
'will be removed in a future release. Instead use the '
'circuit_drawer() function with the `output` kwarg set to '
'`mpl`.', DeprecationWarning)
return _matplotlib_circuit_drawer(circuit, scale=scale,
filename=filename, style=style)


def _matplotlib_circuit_drawer(circuit,
scale=0.7,
Expand Down
3 changes: 2 additions & 1 deletion test/python/tools/visualization/test_circuit_text_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from codecs import encode
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.tools.visualization import _text as elements
from qiskit.tools.visualization import _text_circuit_drawer
from qiskit.tools.visualization._circuit_visualization import \
_text_circuit_drawer
from qiskit.test import QiskitTestCase


Expand Down
8 changes: 4 additions & 4 deletions test/python/tools/visualization/test_visualization_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from codecs import encode
from math import pi
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.tools.visualization import (HAS_MATPLOTLIB, latex_circuit_drawer,
matplotlib_circuit_drawer, circuit_drawer)

from qiskit.tools.visualization import HAS_MATPLOTLIB, circuit_drawer
from qiskit.test import QiskitTestCase


Expand Down Expand Up @@ -82,7 +82,7 @@ def sample_circuit(self):
def test_latex_drawer(self):
filename = self._get_resource_path('current_latex.png')
qc = self.sample_circuit()
latex_circuit_drawer(qc, filename=filename)
circuit_drawer(qc, filename=filename, output='latex')
self.assertImagesAreEqual(filename, self.latex_reference)
os.remove(filename)

Expand All @@ -93,7 +93,7 @@ def test_latex_drawer(self):
def test_matplotlib_drawer(self):
filename = self._get_resource_path('current_matplot.png')
qc = self.sample_circuit()
matplotlib_circuit_drawer(qc, filename=filename)
circuit_drawer(qc, filename=filename, output='mpl')
self.assertImagesAreEqual(filename, self.matplotlib_reference)
os.remove(filename)

Expand Down

0 comments on commit 1d146cc

Please sign in to comment.