Skip to content

Commit

Permalink
Make plot_gate_map generic for any backend (#7814)
Browse files Browse the repository at this point in the history
* Make plot_gate_map generic for any backend

Previously the plot_gate_map() family of visualization functions only
worked with a fixed hard coded list of number of qubits. The function
made the assumption if the backend had a matching number of qubits it
was an IBM backend that had the same number of qubits (and fell into a
fixed connectivity topology) and would use the manually set graph layout
to visualize the function. This was problematic because it didn't
actually work for any backend, and if a backend had the same number of
qubits as an IBM backend but a different connectivity it would use a potentially nonoptimal layout. This also required us to update the hard
coded list whenever a new IBM backend was released to support
visualizing them. Additionally the functions only worked with BackendV1
and BaseBackend based backends and would fail if a BackendV2 object were
passed to the function. This commit address these issues by making 4 key
changes to the function, first it adds a new qubit_coordinates kwarg to
all the methods which if specifed can be used to manually specify a
graph layout to use for the visualization. The second is that the
hardcoded list for layouts are only used if it's an IBM backend (or a
fake backend) so that we're using them where appropriate (the 127q
layout for ibm_washington is also added). The third change is that if a
graph layout isn't provided and it's not an ibm backend with a hardcoded
entry, retworkx's spring_layout() function is used to create a layout.
In the future we can use a more appropriate planar_layout() function
after its available in retworkx (see
Qiskit/rustworkx#438 ). Finally the last change
is that BackendV2 support is added to the function to make sure we're
able to use the visualization function regardless of what type of
backend is passed in.

* Add missing kwargs

* Fix import cycle

* Remove stray debug prints

* Inline function causing import cycle

* Update 127q ibm layout to be consistent with others

* Tweak spring_layout() usage and coordinate system conversion

* Fix lint

* Only use hardcoded layouts if qubit_coordinates is not set

This commit updates the logic around when we use the default hard coded
graph layouts for ibm backends. Previously there was a bug in the if
condition which would cause the hard coded layout to be unconditionally
used for an ibm backend, even if the user specified their own layout.
This fixes this by only using the hard coded layouts when
qubit_coordinates is not set.

* Apply suggestions from code review

Co-authored-by: Kevin Hartman <kevin@hart.mn>

* Fix negative value offset correction in plot_gate_map()

Previously the offset detection was looking for values < 0 which was
needlessly triggering an offset loop for values >=0 and < 1. We only
need to correct for negative values if there are any negative values in
the 2d coordinates being used for the graph layout.

* Remove unnecessary continue

* Update docstring to say qubit_coordinates is explicitly optional

* Fix lint

Co-authored-by: Kevin Hartman <kevin@hart.mn>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 31, 2022
1 parent 1b6c1b3 commit 009150f
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 59 deletions.
3 changes: 2 additions & 1 deletion qiskit/transpiler/passmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import dill

from qiskit.visualization import pass_manager_drawer
from qiskit.tools.parallel import parallel_map
from qiskit.circuit import QuantumCircuit
from .basepasses import BasePass
Expand Down Expand Up @@ -301,6 +300,8 @@ def draw(self, filename=None, style=None, raw=False):
Raises:
ImportError: when nxpd or pydot not installed.
"""
from qiskit.visualization import pass_manager_drawer

return pass_manager_drawer(self, filename=filename, style=style, raw=raw)

def passes(self) -> List[Dict[str, BasePass]]:
Expand Down
Loading

0 comments on commit 009150f

Please sign in to comment.