Skip to content

Commit

Permalink
Make pylatexenc and pillow optional (Qiskit#2451)
Browse files Browse the repository at this point in the history
* 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 Qiskit#2417

* Fix lint

* Fix lint again
  • Loading branch information
mtreinish authored and nonhermitian committed May 21, 2019
1 parent bc8e0d3 commit 00ee0c6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion qiskit/visualization/circuit_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 13 additions & 1 deletion qiskit/visualization/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ vcrpy
PyGithub
wheel
cython>=0.27.1
pillow>=4.2.1
pylatexenc>=1.4
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down Expand Up @@ -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={
Expand Down

0 comments on commit 00ee0c6

Please sign in to comment.