# Buggy code
def dag_drawer(dag, scale=0.7, filename=None, style="color", category=None):
    """Plot the directed acyclic graph (dag) to represent operation dependencies
    in a quantum circuit.

    Note this function leverages
    `pydot <https://github.com/erocarrera/pydot>`_ to generate the graph, which
    means that having `Graphviz <https://www.graphviz.org/>`_ installed on your
    system is required for this to work.

    The current release of Graphviz can be downloaded here: <https://graphviz.gitlab.io/download/>.
    Download the version of the sotware that matches your environment and follow the instructions to
    install Graph Visualization Software (Graphviz) on your operating system.

    Args:
        dag (DAGCircuit): The dag to draw.
        scale (float): scaling factor
        filename (str): file path to save image to (format inferred from name)
        style (str): 'plain': B&W graph
                     'color' (default): color input/output/op nodes
        category (str): 'dependency' for drawing DAG dependency

    Returns:
        PIL.Image: if in Jupyter notebook and not saving to file,
            otherwise None.

    Raises:
        VisualizationError: when style is not recognized.
        ImportError: when pydot or pillow are not installed.

    Example:
        .. jupyter-execute::

            %matplotlib inline
            from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
            from qiskit.dagcircuit import DAGCircuit
            from qiskit.converters import circuit_to_dag
            from qiskit.visualization import dag_drawer

            q = QuantumRegister(3, 'q')
            c = ClassicalRegister(3, 'c')
            circ = QuantumCircuit(q, c)
            circ.h(q[0])
            circ.cx(q[0], q[1])
            circ.measure(q[0], c[0])
            circ.rz(0.5, q[1]).c_if(c, 2)

            dag = circuit_to_dag(circ)
            dag_drawer(dag)
    """
    try:
        import pydot  # pylint: disable=unused-import
    except ImportError:
        raise ImportError("dag_drawer requires pydot. Run 'pip install pydot'.")
    if category is None:
        G = dag.to_networkx()
        G.graph["dpi"] = 100 * scale

        if style == "plain":
            pass
        elif style == "color":
            for node in G.nodes:
                n = G.nodes[node]
                n["label"] = node.name
                if node.type == "op":
                    n["color"] = "blue"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightblue"
                if node.type == "in":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "green"
                if node.type == "out":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "red"
            for e in G.edges(data=True):
                e[2]["label"] = e[2]["name"]
        else:
            raise VisualizationError("Unrecognized style for the dag_drawer.")

    elif category == "dependency":
        G = dag.to_networkx()
        G.graph["dpi"] = 100 * scale

        if style == "plain":
            pass
        elif style == "color":
            for node in G.nodes:
                n = G.nodes[node]
                n["label"] = str(node.node_id) + ": " + node.name
                if node.name == "measure":
                    n["color"] = "blue"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightblue"
                if node.name == "barrier":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "green"
                if node.name == "snapshot":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "red"
                if node.condition:
                    n["label"] = str(node.node_id) + ": " + node.name + " (conditional)"
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightgreen"
        else:
            raise VisualizationError("Unrecognized style for the dag_drawer.")
    else:
        raise VisualizationError("Unrecognized category of DAG")

    dot = to_pydot(G)

    if filename:
        extension = filename.split(".")[-1]
        dot.write(filename, format=extension)
        return None
    elif ("ipykernel" in sys.modules) and ("spyder" not in sys.modules):
        if not HAS_PIL:
            raise ImportError(
                "dag_drawer requires pillow for drawing in jupyter directly. "
                "Run 'pip install pillow'."
            )

        with tempfile.TemporaryDirectory() as tmpdirname:
            tmp_path = os.path.join(tmpdirname, "dag.png")
            dot.write_png(tmp_path)
            image = Image.open(tmp_path)
            os.remove(tmp_path)
            return image
    else:
        if not HAS_PIL:
            raise ImportError(
                "dag_drawer requires pillow for drawing to a window directly. "
                "Run 'pip install pillow'."
            )
        with tempfile.TemporaryDirectory() as tmpdirname:
            tmp_path = os.path.join(tmpdirname, "dag.png")
            dot.write_png(tmp_path)
            image = Image.open(tmp_path)
            os.remove(tmp_path)
            image.show()
            return None


# Traceback (PermissionError)
C:\Anaconda3\envs\localqiskit\python.exe C:/zzz/qiskit-terra/sandbox/dag_drawing.py
Traceback (most recent call last):
File "C:\zzz\qiskit-terra\qiskit\visualization\dag_visualization.py", line 176, in dag_drawer
os.remove(tmp_path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\zzz\\AppData\\Local\\Temp\\tmp_1pw6mnx\\dag.png'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/zzz/qiskit-terra/sandbox/dag_drawing.py", line 11, in <module>
dag_drawer(dag)
File "C:\zzz\qiskit-terra\qiskit\visualization\dag_visualization.py", line 178, in dag_drawer
return None
File "C:\Anaconda3\envs\localqiskit\lib\tempfile.py", line 807, in __exit__
self.cleanup()
File "C:\Anaconda3\envs\localqiskit\lib\tempfile.py", line 811, in cleanup
_shutil.rmtree(self.name)
File "C:\Anaconda3\envs\localqiskit\lib\shutil.py", line 516, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Anaconda3\envs\localqiskit\lib\shutil.py", line 400, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File "C:\Anaconda3\envs\localqiskit\lib\shutil.py", line 398, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\zzz\\AppData\\Local\\Temp\\tmp_1pw6mnx\\dag.png'

# Fixed code
def dag_drawer(dag, scale=0.7, filename=None, style="color", category=None):
    """Plot the directed acyclic graph (dag) to represent operation dependencies
    in a quantum circuit.

    Note this function leverages
    `pydot <https://github.com/erocarrera/pydot>`_ to generate the graph, which
    means that having `Graphviz <https://www.graphviz.org/>`_ installed on your
    system is required for this to work.

    The current release of Graphviz can be downloaded here: <https://graphviz.gitlab.io/download/>.
    Download the version of the sotware that matches your environment and follow the instructions to
    install Graph Visualization Software (Graphviz) on your operating system.

    Args:
        dag (DAGCircuit): The dag to draw.
        scale (float): scaling factor
        filename (str): file path to save image to (format inferred from name)
        style (str): 'plain': B&W graph
                     'color' (default): color input/output/op nodes
        category (str): 'dependency' for drawing DAG dependency

    Returns:
        PIL.Image: if in Jupyter notebook and not saving to file,
            otherwise None.

    Raises:
        VisualizationError: when style is not recognized.
        ImportError: when pydot or pillow are not installed.

    Example:
        .. jupyter-execute::

            %matplotlib inline
            from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
            from qiskit.dagcircuit import DAGCircuit
            from qiskit.converters import circuit_to_dag
            from qiskit.visualization import dag_drawer

            q = QuantumRegister(3, 'q')
            c = ClassicalRegister(3, 'c')
            circ = QuantumCircuit(q, c)
            circ.h(q[0])
            circ.cx(q[0], q[1])
            circ.measure(q[0], c[0])
            circ.rz(0.5, q[1]).c_if(c, 2)

            dag = circuit_to_dag(circ)
            dag_drawer(dag)
    """
    try:
        import pydot  # pylint: disable=unused-import
    except ImportError:
        raise ImportError("dag_drawer requires pydot. Run 'pip install pydot'.")
    if category is None:
        G = dag.to_networkx()
        G.graph["dpi"] = 100 * scale

        if style == "plain":
            pass
        elif style == "color":
            for node in G.nodes:
                n = G.nodes[node]
                n["label"] = node.name
                if node.type == "op":
                    n["color"] = "blue"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightblue"
                if node.type == "in":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "green"
                if node.type == "out":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "red"
            for e in G.edges(data=True):
                e[2]["label"] = e[2]["name"]
        else:
            raise VisualizationError("Unrecognized style for the dag_drawer.")

    elif category == "dependency":
        G = dag.to_networkx()
        G.graph["dpi"] = 100 * scale

        if style == "plain":
            pass
        elif style == "color":
            for node in G.nodes:
                n = G.nodes[node]
                n["label"] = str(node.node_id) + ": " + node.name
                if node.name == "measure":
                    n["color"] = "blue"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightblue"
                if node.name == "barrier":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "green"
                if node.name == "snapshot":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "red"
                if node.condition:
                    n["label"] = str(node.node_id) + ": " + node.name + " (conditional)"
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightgreen"
        else:
            raise VisualizationError("Unrecognized style for the dag_drawer.")
    else:
        raise VisualizationError("Unrecognized category of DAG")

    dot = to_pydot(G)

    if filename:
        extension = filename.split(".")[-1]
        dot.write(filename, format=extension)
        return None
    elif ("ipykernel" in sys.modules) and ("spyder" not in sys.modules):
        if not HAS_PIL:
            raise ImportError(
                "dag_drawer requires pillow for drawing in jupyter directly. "
                "Run 'pip install pillow'."
            )

        with tempfile.TemporaryDirectory() as tmpdirname:
            tmp_path = os.path.join(tmpdirname, "dag.png")
            dot.write_png(tmp_path)
            image = Image.open(tmp_path)
            os.remove(tmp_path)
            return image
    else:
        if not HAS_PIL:
            raise ImportError(
                "dag_drawer requires pillow for drawing to a window directly. "
                "Run 'pip install pillow'."
            )
        with tempfile.TemporaryDirectory() as tmpdirname:
            tmp_path = os.path.join(tmpdirname, "dag.png")
            dot.write_png(tmp_path)
            image = Image.open(tmp_path)
            image.show()
            os.remove(tmp_path)
            return None