Repository: Qiskit/qiskit

Issue:
Detail more  latex error messages when plotting circuit.
<!-- ⚠️ 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?
I would expect to have more debug information for errors in the generation of circuits plotted by qiskit.
For instance, I would like to know that the name i gave to a register contain unallowed character, instead of guessing.
Instead I received a generic "is Qcircuit library installed?" error. 




Discussion:
Related to #601 
@delapuente I actually would say this isn't really related to #601.

 The fundamental issue here is that when we subprocess out to call pdflatex here: https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/tools/visualization/_circuit_visualization.py#L282-L305 we don't really do anything with the actual output from the pdflatex command. Aside from a few checks for known error messages. If those don't match we just assume that the error is because the Qcircuit latex package isn't installed and print the message @Scinawa is referring to.

The fix here would be probably to just print the pdflatex output on error (or alternatively write it to a log file since pdflatex is fairly verbose) so that there is actually debug info on what went wrong.
@mtreinish Right, I agree that logging the error is the way to go. Then we can probably simplify the current error catching hierarchy which tries to catch known errors.

@Scinawa it would be helpful if you could provide a code example to reproduce the error (for example what disallowed characters did you use?)

Resolving patch:
diff --git a/qiskit/tools/visualization/_circuit_visualization.py b/qiskit/tools/visualization/_circuit_visualization.py
--- a/qiskit/tools/visualization/_circuit_visualization.py
+++ b/qiskit/tools/visualization/_circuit_visualization.py
@@ -280,9 +280,12 @@ def latex_circuit_drawer(circuit,
                               scale=scale, style=style)
         im = None
         try:
-            subprocess.run(["pdflatex", "-output-directory={}".format(tmpdirname),
+
+            subprocess.run(["pdflatex", "-halt-on-error",
+                            "-output-directory={}".format(tmpdirname),
                             "{}".format(tmpfilename + '.tex')],
-                           stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, check=True)
+                           stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
+                           check=True)
         except OSError as e:
             if e.errno == os.errno.ENOENT:
                 logger.warning('WARNING: Unable to compile latex. '
@@ -290,18 +293,11 @@ def latex_circuit_drawer(circuit,
                                'Skipping latex circuit drawing...')
             raise
         except subprocess.CalledProcessError as e:
-            if "capacity exceeded" in str(e.stdout):
-                logger.warning('WARNING: Unable to compile latex. '
-                               'Circuit too large for memory. '
-                               'Skipping latex circuit drawing...')
-            elif "Dimension too large." in str(e.stdout):
-                logger.warning('WARNING: Unable to compile latex. '
-                               'Dimension too large for the beamer template. '
-                               'Skipping latex circuit drawing...')
-            else:
-                logger.warning('WARNING: Unable to compile latex. '
-                               'Is the `Qcircuit` latex package installed? '
-                               'Skipping latex circuit drawing...')
+            with open('latex_error.log', 'wb') as error_file:
+                error_file.write(e.stdout)
+            logger.warning('WARNING Unable to complile latex. '
+                           'The output from the pdflatex command can '
+                           'be found in latex_error.log')
             raise
         else:
             try:


Test patch: