Skip to content

Commit

Permalink
Remove list argument broadcasting and simplify transpile()
Browse files Browse the repository at this point in the history
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 Qiskit#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 Qiskit#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 Qiskit#9256 is implemented we'll just build a single
target from the arguments as needed.

Fixes Qiskit#7741
  • Loading branch information
mtreinish committed Jun 15, 2023
1 parent 7df290b commit 094fb0f
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 481 deletions.
603 changes: 152 additions & 451 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
6 changes: 5 additions & 1 deletion qiskit/providers/fake_provider/fake_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,11 @@ def _setup_sim(self):
def properties(self):
"""Return backend properties"""
coupling_map = self.configuration().coupling_map
unique_qubits = list(set().union(*coupling_map))
unique_qubits = []
if coupling_map is not None:
unique_qubits = list(set().union(*coupling_map))
else:
return None

properties = {
"backend_name": self.name(),
Expand Down
8 changes: 7 additions & 1 deletion qiskit/transpiler/passes/layout/set_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


from qiskit.transpiler.basepasses import AnalysisPass
from qiskit.transpiler.layout import Layout


class SetLayout(AnalysisPass):
Expand Down Expand Up @@ -41,5 +42,10 @@ def run(self, dag):
Returns:
DAGCircuit: the original DAG.
"""
self.property_set["layout"] = None if self.layout is None else self.layout.copy()
if isinstance(self.layout, list):
layout = Layout({dag.qubits[i]: phys for i, phys in enumerate(self.layout)})
else:
layout = self.layout

self.property_set["layout"] = None if layout is None else layout.copy()
return dag
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
3 changes: 3 additions & 0 deletions 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,6 +243,7 @@ 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
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. 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 itertools import cycle
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 = []
for qc, cmap in zip(cycle([qc]), cmaps):
results.append(transpile(qc, coupling_map=cmap))
You can also leverage :func:`~.parallel_map` or ``multiprocessing`` from
the Python standard library if you want to run this in parallel.
12 changes: 2 additions & 10 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2071,24 +2071,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 094fb0f

Please sign in to comment.