From 00ee0c66807ee4ed14ceb5958feb620712c77143 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 21 May 2019 07:36:50 -0400 Subject: [PATCH] Make pylatexenc and pillow optional (#2451) * Make pylatexenc and pillow optional The pylatexenc and pillow requirements are only used in the latex and latex_source circuit drawers. Since this isn't used by everyone and is a non-default circuit drawer there is no reason we should force our users to install these by default. This commit updates the requirements list and setup.py to make these 2 requirements optional and included in the visualization setuptools extras to ease installation. If the packages are not installed it will raise an ImportError with a detailed exception explaining how to install the missing dependency. Fixes #2417 * Fix lint * Fix lint again --- CHANGELOG.rst | 7 +++++++ qiskit/visualization/circuit_visualization.py | 11 ++++++++++- qiskit/visualization/latex.py | 14 +++++++++++++- requirements-dev.txt | 2 ++ requirements.txt | 2 -- setup.py | 4 +--- 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9c93b8312b1f..2341372b5fb6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -19,6 +19,13 @@ The format is based on `Keep a Changelog`_. `UNRELEASED`_ ============= +Changed +------- + +- The ``pylatexenc`` and ``pillow`` requirements are now optional. These are + only used by the ``latex`` and ``latex_source`` circuit visualization + backends. To continue using them ensure these are installed. + Removed ------- - The previously deprecated functions ``qiksit.visualization.plot_state`` and diff --git a/qiskit/visualization/circuit_visualization.py b/qiskit/visualization/circuit_visualization.py index a4d6567a8662..8dc426b5a568 100644 --- a/qiskit/visualization/circuit_visualization.py +++ b/qiskit/visualization/circuit_visualization.py @@ -28,7 +28,11 @@ import subprocess import tempfile -from PIL import Image +try: + from PIL import Image + HAS_PIL = True +except ImportError: + HAS_PIL = False from qiskit import user_config from qiskit.visualization import exceptions @@ -369,6 +373,7 @@ def _latex_circuit_drawer(circuit, OSError: usually indicates that ```pdflatex``` or ```pdftocairo``` is missing. CalledProcessError: usually points errors during diagram creation. + ImportError: if pillow is not installed """ tmpfilename = 'circuit' with tempfile.TemporaryDirectory() as tmpdirname: @@ -399,6 +404,10 @@ def _latex_circuit_drawer(circuit, 'be found in latex_error.log') raise else: + if not HAS_PIL: + raise ImportError('The latex drawer needs pillow installed. ' + 'Run "pip install pillow" before using the ' + 'latex drawer.') try: base = os.path.join(tmpdirname, tmpfilename) subprocess.run(["pdftocairo", "-singlefile", "-png", "-q", diff --git a/qiskit/visualization/latex.py b/qiskit/visualization/latex.py index b8e199744a45..8a6147c1e36c 100644 --- a/qiskit/visualization/latex.py +++ b/qiskit/visualization/latex.py @@ -24,7 +24,12 @@ import operator import re -from pylatexenc.latexencode import utf8tolatex +try: + from pylatexenc.latexencode import utf8tolatex + HAS_PYLATEX = True +except ImportError: + HAS_PYLATEX = False + import numpy as np from qiskit.visualization import qcstyle as _qcstyle from qiskit.visualization import exceptions @@ -53,7 +58,14 @@ def __init__(self, qregs, cregs, ops, scale, style=None, registers for the output visualization. plot_barriers (bool): Enable/disable drawing barriers in the output circuit. Defaults to True. + Raises: + ImportError: If pylatexenc is not installed """ + if not HAS_PYLATEX: + raise ImportError('The latex and latex_source drawers need ' + 'pylatexenc installed. Run "pip install ' + 'pylatexenc" before using the latex or ' + 'latex_source drawers.') # style sheet self._style = _qcstyle.BWStyle() if style: diff --git a/requirements-dev.txt b/requirements-dev.txt index 83b5648fc473..4254e7fd6115 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -13,3 +13,5 @@ vcrpy PyGithub wheel cython>=0.27.1 +pillow>=4.2.1 +pylatexenc>=1.4 diff --git a/requirements.txt b/requirements.txt index ea5dafa60082..c7fd1852f4a3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,8 +3,6 @@ marshmallow>=2.17.0,<3 marshmallow_polyfield>=3.2,<4 networkx>=2.2 numpy>=1.13 -pillow>=4.2.1 -pylatexenc>=1.4 ply>=3.10 psutil>=5 scipy>=1.0 diff --git a/setup.py b/setup.py index 7f7f4c8a8ab0..469794ac807f 100755 --- a/setup.py +++ b/setup.py @@ -30,10 +30,8 @@ "marshmallow_polyfield>=3.2,<4", "networkx>=2.2", "numpy>=1.13", - "pillow>=4.2.1", "ply>=3.10", "psutil>=5", - "pylatexenc>=1.4", "scipy>=1.0", "sympy>=1.3" ] @@ -101,7 +99,7 @@ python_requires=">=3.5", extras_require={ 'visualization': ['matplotlib>=2.1', 'nxpd>=0.2', 'ipywidgets>=7.3.0', - 'pydot'], + 'pydot', "pillow>=4.2.1", "pylatexenc>=1.4"], 'full-featured-simulators': ['qiskit-aer>=0.1'] }, project_urls={