Skip to content

Commit

Permalink
Remove list argument broadcasting and simplify transpile() (#10291)
Browse files Browse the repository at this point in the history
* Remove list argument broadcasting and simplify transpile()

This commit updates the transpile() function to no longer support
broadcast of lists of arguments. This functionality was deprecated in
the 0.23.0 release. As part of this removal the internals of the
transpile() function are simplified so we don't need to handle
broadcasting, building preset pass managers, parallel dispatch, etc
anymore as this functionality (without broadcasting) already exists
through the transpiler API. Besides greatly simplifying the transpile()
code and using more aspects of the public APIs that exist in the
qiskit.transpiler module, this commit also should fix the overhead we
have around parallel execution due to the complexity of supporting
broadcasting. This overhead was partially addressed before in #7789
which leveraged shared memory to minimize the serialization time
necessary for IPC but by using `PassManager.run()` internally now all of
that overhead is removed as the initial fork will have all the necessary
context in each process from the start.

Three seemingly unrelated changes made here were necessary to support our
current transpile() API without building custom pass manager
construction.

The first is the handling of layout from intlist. The
current Layout class is dependent on a circuit because it maps Qubit
objects to a physical qubit index. Ideally the layout structure would
just map virtual indices to physical indices (see #8060 for a similar
issue, also it's worth noting this is how the internal NLayout and QPY
represent layout), but because of the existing API the construction of
a Layout is dependent on a circuit. For the initial_layout argument when
running with multiple circuits to avoid the need to broadcasting the
layout construction for supported input types that need the circuit to
lookup the Qubit objects the SetLayout pass now supports taking in an
int list and will construct a Layout object at run time. This
effectively defers the Layout object creation for initial_layout to
run time so it can be built as a function of the circuit as the API
demands.

The second is the FakeBackend class used in some tests was constructing
invalid backends in some cases. This wasn't caught in the previous
structure because the backends were not actually being parsed by
transpile() previously which masked this issue. This commit fixes that
issue because PassManagerConfig.from_backend() was failing because of
the invalid backend construction.

The third issue is a new _skip_target private argument to
generate_preset_pass_manager() and PassManagerConfig. This was necessary
to recreate the behavior of transpile() when a user provides a BackendV2
and either `basis_gates` or `coupling_map` arguments. In general the
internals of the transpiler treat a target as higher priority because it
has more complete and restrictive constraints than the
basis_gates/coupling map objects. However, for transpile() if a
backendv2 is passed in for backend paired with coupling_map and/or
basis_gates the expected workflow is that the basis_gates and
coupling_map arguments take priority and override the equivalent
attributes from the backend. To facilitate this we need to block pulling
the target from the backend This should only be needed for a short
period of time as when #9256 is implemented we'll just build a single
target from the arguments as needed.

Fixes #7741

* Fix _skip_target logic

* Fix InstructionScheduleMap handling with backendv2

* Fix test failure caused by exception being raised later

* Fix indentation error

* Update qiskit/providers/fake_provider/fake_backend.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* Fix standalone dt argument handling

* Remove unused code

* Fix lint

* Remove duplicate import in set_layout.py

A duplicate import slipped through in the most recent rebase.
This commit fixes that oversight and removes the duplicate.

* Update release notes

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>

* Adjust logic for _skip_transpile to check if None

* Simplify check cmap code

* Only check backend if it exists

---------

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
  • Loading branch information
3 people authored Jul 19, 2023
1 parent a7fcf23 commit 346ca77
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 495 deletions.
615 changes: 150 additions & 465 deletions qiskit/compiler/transpiler.py

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions qiskit/providers/fake_provider/fake_1q.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,3 @@ def __init__(self):
general=[],
)
super().__init__(configuration)

def defaults(self):
"""defaults == configuration"""
return self._configuration

def properties(self):
"""properties == configuration"""
return self._configuration
2 changes: 2 additions & 0 deletions qiskit/providers/fake_provider/fake_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ def _setup_sim(self):
def properties(self):
"""Return backend properties"""
coupling_map = self.configuration().coupling_map
if coupling_map is None:
return None
unique_qubits = list(set().union(*coupling_map))

properties = {
Expand Down
5 changes: 2 additions & 3 deletions qiskit/transpiler/passmanager_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(
self.hls_config = hls_config

@classmethod
def from_backend(cls, backend, **pass_manager_options):
def from_backend(cls, backend, _skip_target=False, **pass_manager_options):
"""Construct a configuration based on a backend and user input.
This method automatically gererates a PassManagerConfig object based on the backend's
Expand All @@ -128,7 +128,6 @@ def from_backend(cls, backend, **pass_manager_options):
backend_version = 0
if backend_version < 2:
config = backend.configuration()

if res.basis_gates is None:
if backend_version < 2:
res.basis_gates = getattr(config, "basis_gates", None)
Expand Down Expand Up @@ -156,7 +155,7 @@ def from_backend(cls, backend, **pass_manager_options):
res.instruction_durations = backend.instruction_durations
if res.backend_properties is None and backend_version < 2:
res.backend_properties = backend.properties()
if res.target is None:
if res.target is None and not _skip_target:
if backend_version >= 2:
res.target = backend.target
if res.scheduling_method is None and hasattr(backend, "get_scheduling_stage_plugin"):
Expand Down
4 changes: 3 additions & 1 deletion qiskit/transpiler/preset_passmanagers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def generate_preset_pass_manager(
hls_config=None,
init_method=None,
optimization_method=None,
*,
_skip_target=False,
):
"""Generate a preset :class:`~.PassManager`
Expand Down Expand Up @@ -241,10 +243,10 @@ def generate_preset_pass_manager(
}

if backend is not None:
pm_options["_skip_target"] = _skip_target
pm_config = PassManagerConfig.from_backend(backend, **pm_options)
else:
pm_config = PassManagerConfig(**pm_options)

if optimization_level == 0:
pm = level_0_pass_manager(pm_config)
elif optimization_level == 1:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
upgrade:
- |
Support for passing in lists of argument values to the :func:`~.transpile`
function is removed. This functionality was deprecated as part of the
0.23.0 release and is now being removed. You are still able to pass in a
list of :class:`~.QuantumCircuit` objects for the first positional argument,
what has been removed is the broadcasting lists of the other arguments to
each circuit in that input list. Removing this functionality was necessary
to greatly reduce the overhead for parallel execution for transpiling
multiple circuits at once. If you’re using this functionality
currently you can call :func:`~.transpile` multiple times instead. For
example if you were previously doing something like::
from qiskit.transpiler import CouplingMap
from qiskit import QuantumCircuit
from qiskit import transpile
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
cmaps = [CouplingMap.from_heavy_hex(d) for d in range(3, 15, 2)]
results = transpile([qc] * 6, coupling_map=cmaps)
instead you should now run something like::
from qiskit.transpiler import CouplingMap
from qiskit import QuantumCircuit
from qiskit import transpile
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
cmaps = [CouplingMap.from_heavy_hex(d) for d in range(3, 15, 2)]
results = [transpile(qc, coupling_map=cm) for cm in cmap]
You can also leverage :func:`~.parallel_map` or ``multiprocessing`` from
the Python standard library if you want to run this in parallel.
14 changes: 3 additions & 11 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def test_wrong_initial_layout(self):
QuantumRegister(3, "q")[2],
]

with self.assertRaisesRegex(TranspilerError, "different numbers of qubits"):
with self.assertRaises(TranspilerError):
transpile(qc, backend, initial_layout=bad_initial_layout)

def test_parameterized_circuit_for_simulator(self):
Expand Down Expand Up @@ -2085,24 +2085,16 @@ def test_transpile_with_multiple_coupling_maps(self):
cmap = CouplingMap.from_line(7)
cmap.add_edge(0, 2)

with self.assertWarnsRegex(
DeprecationWarning, "Passing in a list of arguments for coupling_map is deprecated"
):
with self.assertRaisesRegex(TranspilerError, "Only a single input coupling"):
# Initial layout needed to prevent transpiler from relabeling
# qubits to avoid doing the swap
tqc = transpile(
transpile(
[qc] * 2,
backend,
coupling_map=[backend.coupling_map, cmap],
initial_layout=(0, 1, 2),
)

# Check that the two coupling maps were used. The default should
# require swapping (extra cx's) and the second one should not (just the
# original cx).
self.assertEqual(tqc[0].count_ops()["cx"], 4)
self.assertEqual(tqc[1].count_ops()["cx"], 1)

@data(0, 1, 2, 3)
def test_backend_and_custom_gate(self, opt_level):
"""Test transpile() with BackendV2, custom basis pulse gate."""
Expand Down
7 changes: 0 additions & 7 deletions test/python/providers/test_backend_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

from qiskit.circuit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.compiler import transpile
from qiskit.compiler.transpiler import _parse_inst_map
from qiskit.pulse.instruction_schedule_map import InstructionScheduleMap
from qiskit.test.base import QiskitTestCase
from qiskit.providers.fake_provider import FakeMumbaiFractionalCX
from qiskit.providers.fake_provider.fake_backend_v2 import (
Expand Down Expand Up @@ -178,11 +176,6 @@ def test_transpile_mumbai_target(self):
expected.measure(qr[1], cr[1])
self.assertEqual(expected, tqc)

def test_transpile_parse_inst_map(self):
"""Test that transpiler._parse_inst_map() supports BackendV2."""
inst_map = _parse_inst_map(inst_map=None, backend=self.backend)
self.assertIsInstance(inst_map, InstructionScheduleMap)

@data(0, 1, 2, 3, 4)
def test_drive_channel(self, qubit):
"""Test getting drive channel with qubit index."""
Expand Down

0 comments on commit 346ca77

Please sign in to comment.