def plot_circuit_layout(circuit, backend, view='virtual'):
    "Plot the layout of a circuit transpiled for a given\n    target backend.\n\n    Args:\n        circuit (QuantumCircuit): Input quantum circuit.\n        backend (BaseBackend): Target backend.\n        view (str): Layout view: either 'virtual' or 'physical'.\n\n    Returns:\n        Figure: A matplotlib figure showing layout.\n\n    Raises:\n        QiskitError: Invalid view type given.\n        VisualizationError: Circuit has no layout attribute.\n\n    Example:\n        .. jupyter-execute::\n            :hide-code:\n            :hide-output:\n\n            from qiskit.test.ibmq_mock import mock_get_backend\n            mock_get_backend('FakeVigo')\n\n        .. jupyter-execute::\n\n            import numpy as np\n            from qiskit import QuantumCircuit, IBMQ, transpile\n            from qiskit.visualization import plot_histogram, plot_gate_map, plot_circuit_layout\n            from qiskit.tools.monitor import job_monitor\n            import matplotlib.pyplot as plt\n            %matplotlib inline\n\n            IBMQ.load_account()\n\n            ghz = QuantumCircuit(3, 3)\n            ghz.h(0)\n            for idx in range(1,3):\n                ghz.cx(0,idx)\n            ghz.measure(range(3), range(3))\n\n            provider = IBMQ.get_provider(hub='ibm-q')\n            backend = provider.get_backend('ibmq_vigo')\n            new_circ_lv3 = transpile(ghz, backend=backend, optimization_level=3)\n            plot_circuit_layout(new_circ_lv3, backend)\n    "
    if (circuit._layout is None):
        raise QiskitError('Circuit has no layout. Perhaps it has not been transpiled.')
    num_qubits = backend.configuration().n_qubits
    qubits = []
    qubit_labels = ([None] * num_qubits)
    if (view == 'virtual'):
        for (key, val) in circuit._layout.get_virtual_bits().items():
            if (key.register.name != 'ancilla'):
                qubits.append(val)
                qubit_labels[val] = key.index
    elif (view == 'physical'):
        for (key, val) in circuit._layout.get_physical_bits().items():
            if (val.register.name != 'ancilla'):
                qubits.append(key)
                qubit_labels[key] = key
    else:
        raise VisualizationError("Layout view must be 'virtual' or 'physical'.")
    qcolors = (['#648fff'] * num_qubits)
    for k in qubits:
        qcolors[k] = 'k'
    cmap = backend.configuration().coupling_map
    lcolors = (['#648fff'] * len(cmap))
    for (idx, edge) in enumerate(cmap):
        if ((edge[0] in qubits) and (edge[1] in qubits)):
            lcolors[idx] = 'k'
    fig = plot_gate_map(backend, qubit_color=qcolors, qubit_labels=qubit_labels, line_color=lcolors)
    return fig
Plot the layout of a circuit transpiled for a given
target backend.

Args:
    circuit (QuantumCircuit): Input quantum circuit.
    backend (BaseBackend): Target backend.
    view (str): Layout view: either 'virtual' or 'physical'.

Returns:
    Figure: A matplotlib figure showing layout.

Raises:
    QiskitError: Invalid view type given.
    VisualizationError: Circuit has no layout attribute.

Example:
    .. jupyter-execute::
        :hide-code:
        :hide-output:

        from qiskit.test.ibmq_mock import mock_get_backend
        mock_get_backend('FakeVigo')

    .. jupyter-execute::

        import numpy as np
        from qiskit import QuantumCircuit, IBMQ, transpile
        from qiskit.visualization import plot_histogram, plot_gate_map, plot_circuit_layout
        from qiskit.tools.monitor import job_monitor
        import matplotlib.pyplot as plt
        %matplotlib inline

        IBMQ.load_account()

        ghz = QuantumCircuit(3, 3)
        ghz.h(0)
        for idx in range(1,3):
            ghz.cx(0,idx)
        ghz.measure(range(3), range(3))

        provider = IBMQ.get_provider(hub='ibm-q')
        backend = provider.get_backend('ibmq_vigo')
        new_circ_lv3 = transpile(ghz, backend=backend, optimization_level=3)
        plot_circuit_layout(new_circ_lv3, backend)
def plot_circuit_layout(circuit, backend, view='virtual'):
    "Plot the layout of a circuit transpiled for a given\n    target backend.\n\n    Args:\n        circuit (QuantumCircuit): Input quantum circuit.\n        backend (BaseBackend): Target backend.\n        view (str): Layout view: either 'virtual' or 'physical'.\n\n    Returns:\n        Figure: A matplotlib figure showing layout.\n\n    Raises:\n        QiskitError: Invalid view type given.\n        VisualizationError: Circuit has no layout attribute.\n\n    Example:\n        .. jupyter-execute::\n            :hide-code:\n            :hide-output:\n\n            from qiskit.test.ibmq_mock import mock_get_backend\n            mock_get_backend('FakeVigo')\n\n        .. jupyter-execute::\n\n            import numpy as np\n            from qiskit import QuantumCircuit, IBMQ, transpile\n            from qiskit.visualization import plot_histogram, plot_gate_map, plot_circuit_layout\n            from qiskit.tools.monitor import job_monitor\n            import matplotlib.pyplot as plt\n            %matplotlib inline\n\n            IBMQ.load_account()\n\n            ghz = QuantumCircuit(3, 3)\n            ghz.h(0)\n            for idx in range(1,3):\n                ghz.cx(0,idx)\n            ghz.measure(range(3), range(3))\n\n            provider = IBMQ.get_provider(hub='ibm-q')\n            backend = provider.get_backend('ibmq_vigo')\n            new_circ_lv3 = transpile(ghz, backend=backend, optimization_level=3)\n            plot_circuit_layout(new_circ_lv3, backend)\n    "
    if (circuit._layout is None):
        raise QiskitError('Circuit has no layout. Perhaps it has not been transpiled.')
    num_qubits = backend.configuration().n_qubits
    qubits = []
    qubit_labels = ([None] * num_qubits)
    if (view == 'virtual'):
        for (key, val) in circuit._layout.get_virtual_bits().items():
            if (key.register.name != 'ancilla'):
                qubits.append(val)
                qubit_labels[val] = key.index
    elif (view == 'physical'):
        for (key, val) in circuit._layout.get_physical_bits().items():
            if (val.register.name != 'ancilla'):
                qubits.append(key)
                qubit_labels[key] = key
    else:
        raise VisualizationError("Layout view must be 'virtual' or 'physical'.")
    qcolors = (['#648fff'] * num_qubits)
    for k in qubits:
        qcolors[k] = 'k'
    cmap = backend.configuration().coupling_map
    lcolors = (['#648fff'] * len(cmap))
    for (idx, edge) in enumerate(cmap):
        if ((edge[0] in qubits) and (edge[1] in qubits)):
            lcolors[idx] = 'k'
    fig = plot_gate_map(backend, qubit_color=qcolors, qubit_labels=qubit_labels, line_color=lcolors)
    return fig