Repository: Qiskit/qiskit

Issue:
remove pylatexenc as required dependency
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues to confirm this idea does not exist. -->

### What is the expected enhancement?
pylatexenc is used in a relatively minor place for formatting latex_drawer's strings. Given that everything associated with latex drawing is optional itself, this part should also be optional and not a core dependency of terra.




Discussion:
Well that's not entirely the case, we also have pillow in requirements which is also only used for latex drawing. The thing which is optional is the external dependencies like pdflatex and the latex requirements (because we don't have a mechanism to require them). I'd actually argue that pillow should be optional but not pylatexenc, mostly because pylatexenc is used by the latex_source backend which has no other external dependencies and just returns a string.
pillow should also be optional.
This was the point of creating a text drawer so that we could make the dependencies light. Terra is very much usable without them.
I think all of these should be part of the extra visualization requirements -- along with matplotlib, nxpd, ipywidgets, pydot that are already there.

Resolving patch:
diff --git a/qiskit/visualization/circuit_visualization.py b/qiskit/visualization/circuit_visualization.py
--- 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
--- 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/setup.py b/setup.py
--- 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={


Test patch: