Repository: Qiskit/qiskit

Issue:
Better error messaging when graphviz is not present
_For reference, this was originally posted by @jaygambetta in https://github.com/Qiskit/qiskit-terra/issues/2281#issuecomment-489417445_

> @ajavadia and @mtreinish  it has been lost where to find how to add this dependencies outside pip. It is in the doc for the function https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/visualization/dag_visualization.py but I think we need to make this clearer in the documentation in the Qiskit repo. 
>
> I would split this into two issues -- 
> 1. In terra add better error messaging. If you call drag_drawer and you don't have graphviz give that this dependency needs to be installed on your system. 
> 2. in qiskit add a documentation on how to setup the dag drawer for different operating systems.

This is issue is about the first item. 


Discussion:

Resolving patch:
diff --git a/qiskit/visualization/dag_visualization.py b/qiskit/visualization/dag_visualization.py
--- a/qiskit/visualization/dag_visualization.py
+++ b/qiskit/visualization/dag_visualization.py
@@ -51,8 +51,8 @@ def dag_drawer(dag, scale=0.7, filename=None, style='color'):
         import nxpd
         import pydot  # pylint: disable=unused-import
     except ImportError:
-        raise ImportError("dag_drawer requires nxpd, pydot, and Graphviz. "
-                          "Run 'pip install nxpd pydot', and install graphviz")
+        raise ImportError("dag_drawer requires nxpd and pydot. "
+                          "Run 'pip install nxpd pydot'.")
 
     G = dag.to_networkx()
     G.graph['dpi'] = 100 * scale
@@ -87,4 +87,9 @@ def dag_drawer(dag, scale=0.7, filename=None, style='color'):
     else:
         show = True
 
-    return nxpd.draw(G, filename=filename, show=show)
+    try:
+        return nxpd.draw(G, filename=filename, show=show)
+    except nxpd.pydot.InvocationException:
+        raise VisualizationError("dag_drawer requires GraphViz installed in the system. "
+                                 "Check https://www.graphviz.org/download/ for details on "
+                                 "how to install GraphViz in your system.")


Test patch:
diff --git a/test/python/visualization/test_dag_drawer.py b/test/python/visualization/test_dag_drawer.py
new file mode 100644
--- /dev/null
+++ b/test/python/visualization/test_dag_drawer.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+
+# This code is part of Qiskit.
+#
+# (C) Copyright IBM 2019.
+#
+# This code is licensed under the Apache License, Version 2.0. You may
+# obtain a copy of this license in the LICENSE.txt file in the root directory
+# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
+#
+# Any modifications or derivative works of this code must retain this
+# copyright notice, and modified files need to carry a notice indicating
+# that they have been altered from the originals.
+
+"""Tests for DAG visualization tool."""
+
+import unittest
+
+from qiskit import QuantumRegister, QuantumCircuit
+from qiskit.test import QiskitTestCase
+from qiskit.tools.visualization import dag_drawer
+from qiskit.visualization.exceptions import VisualizationError
+from qiskit.converters import circuit_to_dag
+
+
+class TestDagDrawer(QiskitTestCase):
+    """Qiskit DAG drawer tests."""
+
+    def setUp(self):
+        qr = QuantumRegister(2, 'qr')
+        circuit = QuantumCircuit(qr)
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        self.dag = circuit_to_dag(circuit)
+
+    def test_dag_drawer_no_graphviz(self):
+        """Test dag draw with no graphviz."""
+        with unittest.mock.patch('nxpd.pydot.find_graphviz', return_value=None) as _:
+            self.assertRaises(VisualizationError, dag_drawer, self.dag)
+
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2)