qiskit 1.3.2__cp39-abi3-win32.whl → 1.4.0__cp39-abi3-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. qiskit/VERSION.txt +1 -1
  2. qiskit/__init__.py +1 -0
  3. qiskit/_accelerate.pyd +0 -0
  4. qiskit/circuit/__init__.py +5 -2
  5. qiskit/circuit/bit.py +12 -0
  6. qiskit/circuit/classicalfunction/__init__.py +13 -1
  7. qiskit/circuit/classicalfunction/boolean_expression.py +10 -1
  8. qiskit/circuit/classicalfunction/classicalfunction.py +10 -1
  9. qiskit/circuit/classicalfunction/exceptions.py +7 -1
  10. qiskit/circuit/delay.py +5 -0
  11. qiskit/circuit/library/generalized_gates/mcmt.py +5 -6
  12. qiskit/circuit/library/phase_oracle.py +24 -17
  13. qiskit/circuit/library/standard_gates/rz.py +7 -7
  14. qiskit/circuit/library/standard_gates/xx_minus_yy.py +0 -30
  15. qiskit/circuit/parametervector.py +25 -5
  16. qiskit/circuit/quantumcircuit.py +68 -2
  17. qiskit/circuit/register.py +13 -0
  18. qiskit/compiler/assembler.py +16 -8
  19. qiskit/compiler/transpiler.py +1 -1
  20. qiskit/dagcircuit/dagdependency_v2.py +4 -2
  21. qiskit/passmanager/passmanager.py +19 -1
  22. qiskit/primitives/backend_estimator_v2.py +17 -0
  23. qiskit/primitives/backend_sampler_v2.py +15 -0
  24. qiskit/providers/backend_compat.py +46 -11
  25. qiskit/providers/basic_provider/basic_simulator.py +15 -3
  26. qiskit/providers/exceptions.py +23 -2
  27. qiskit/providers/models/backendproperties.py +19 -1
  28. qiskit/providers/models/backendstatus.py +10 -0
  29. qiskit/providers/models/jobstatus.py +11 -0
  30. qiskit/pulse/schedule.py +1 -1
  31. qiskit/quantum_info/operators/channel/transformations.py +15 -0
  32. qiskit/result/result.py +109 -20
  33. qiskit/synthesis/evolution/product_formula.py +1 -2
  34. qiskit/synthesis/evolution/qdrift.py +1 -2
  35. qiskit/synthesis/evolution/suzuki_trotter.py +1 -2
  36. qiskit/transpiler/__init__.py +772 -542
  37. qiskit/transpiler/layout.py +6 -6
  38. qiskit/transpiler/passes/calibration/rzx_templates.py +7 -0
  39. qiskit/transpiler/passes/layout/dense_layout.py +12 -0
  40. qiskit/transpiler/passes/layout/sabre_layout.py +13 -0
  41. qiskit/transpiler/passes/layout/vf2_layout.py +11 -0
  42. qiskit/transpiler/passes/layout/vf2_post_layout.py +22 -0
  43. qiskit/transpiler/passes/optimization/normalize_rx_angle.py +8 -0
  44. qiskit/transpiler/passes/optimization/remove_identity_equiv.py +2 -3
  45. qiskit/transpiler/passes/routing/star_prerouting.py +17 -2
  46. qiskit/transpiler/passes/scheduling/scheduling/alap.py +1 -1
  47. qiskit/transpiler/passes/scheduling/scheduling/asap.py +1 -1
  48. qiskit/transpiler/passes/synthesis/unitary_synthesis.py +11 -0
  49. qiskit/transpiler/passmanager_config.py +11 -0
  50. qiskit/transpiler/preset_passmanagers/__init__.py +26 -6
  51. qiskit/transpiler/preset_passmanagers/builtin_plugins.py +188 -118
  52. qiskit/transpiler/preset_passmanagers/common.py +133 -83
  53. qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py +29 -3
  54. qiskit/transpiler/preset_passmanagers/plugin.py +33 -42
  55. qiskit/transpiler/target.py +41 -9
  56. qiskit/visualization/circuit/text.py +3 -2
  57. qiskit/visualization/gate_map.py +50 -0
  58. qiskit/visualization/pulse_v2/interface.py +0 -2
  59. qiskit/visualization/timeline/interface.py +3 -3
  60. {qiskit-1.3.2.dist-info → qiskit-1.4.0.dist-info}/METADATA +4 -7
  61. {qiskit-1.3.2.dist-info → qiskit-1.4.0.dist-info}/RECORD +65 -65
  62. {qiskit-1.3.2.dist-info → qiskit-1.4.0.dist-info}/LICENSE.txt +0 -0
  63. {qiskit-1.3.2.dist-info → qiskit-1.4.0.dist-info}/WHEEL +0 -0
  64. {qiskit-1.3.2.dist-info → qiskit-1.4.0.dist-info}/entry_points.txt +0 -0
  65. {qiskit-1.3.2.dist-info → qiskit-1.4.0.dist-info}/top_level.txt +0 -0
@@ -36,80 +36,162 @@ compilation flow follows the structure given below:
36
36
  :alt: The transpilation process takes the input circuit, applies the transpilation \
37
37
  passes, then produces the output circuit.
38
38
 
39
- .. raw:: html
40
-
41
- <br>
42
-
43
- Qiskit has four pre-built transpilation pipelines available here:
44
- :mod:`qiskit.transpiler.preset_passmanagers`. Unless the reader is familiar with
45
- quantum circuit optimization methods and their usage, it is best to use one of
46
- these ready-made routines. By default the preset pass managers are composed
47
- of six stages:
48
-
49
- #. ``init`` - This stage runs any initial passes that are required before we start embedding the
50
- circuit to the backend. This typically involves unrolling custom instructions and converting
51
- the circuit to all 1 and 2 qubit gates.
52
- #. ``layout`` - This stage applies a layout, mapping the virtual qubits in the circuit to the
53
- physical qubits on a backend. See :ref:`layout_stage` for more details.
54
- #. ``routing`` - This stage runs after a layout has been applied and will inject
55
- gates (i.e. swaps) into the original circuit to make it compatible
56
- with the backend's connectivity. See :ref:`routing_stage` for more details.
57
- #. ``translation`` - This stage translates the gates in the circuit to the target backend's basis set.
58
- See :ref:`translation_stage` for more details.
59
- #. ``optimization`` - This stage runs the main optimization loop repeatedly
60
- until a condition (such as fixed depth) is reached. See :ref:`optimization_stage` for more details.
61
- #. ``scheduling`` - This stage is for any hardware-aware scheduling passes. See
62
- :ref:`scheduling_stage` for more details.
63
-
64
- When using :func:`~.transpile`, the implementation of each stage can be modified with the ``*_method``
65
- arguments (e.g. ``layout_method``). These can be set to one of the built-in methods and
66
- can also refer to available external plugins. See
67
- :mod:`qiskit.transpiler.preset_passmanagers.plugin` for details on this plugin interface.
68
-
69
- .. _working_with_preset_pass_managers:
70
-
71
- Working with Preset Pass Managers
72
- =================================
73
-
74
- Qiskit includes functions to build preset :class:`~.PassManager` objects.
75
- These preset passmanagers are used by the :func:`~.transpile` function
76
- for each optimization level. There are 4 optimization levels ranging from 0 to 3, where higher
77
- optimization levels take more time and computational effort but may yield a
78
- more optimal circuit.
79
- Optimization level 0 is intended for device characterization experiments and, as such, only
80
- maps the input circuit to the constraints of the target backend, without
81
- performing any optimizations. Optimization level 3 spends the most effort to optimize the circuit.
82
- However, as many of the optimization techniques in the transpiler are heuristic based, spending more
83
- computational effort does not always result in an improvement in the quality of the output
84
- circuit.
39
+ Qiskit uses the graph-based :class:`.DAGCircuit` intermediate representation (IR) of a circuit
40
+ throughout the transpiler stack, rather than the tree-based :class:`.QuantumCircuit`. A transpiler
41
+ pipeline is a :class:`.PassManager` object, whose :meth:`.PassManager.run` method takes in a
42
+ :class:`.QuantumCircuit` and converts it to a :class:`.DAGCircuit`, then subjects the IR to a
43
+ sequence of *passes*, finally returning a :class:`.QuantumCircuit` back. A pass is either an
44
+ :class:`.AnalysisPass`, which calculates and stores properties about the circuit in the
45
+ stateful :class:`.PropertySet`, or a :class:`.TransformationPass`, which modifies the IR
46
+ to achieve a particular singular goal. You can think of a pipeline as being split into
47
+ "stages", where each stage is responsible for one high-level transformation.
48
+
49
+ Qiskit exposes a default transpilation pipeline builder using the function
50
+ :func:`.generate_preset_pass_manager`. This returns a properly configured pipeline for complete
51
+ transpilation, at a chosen ``optimization_level`` (between 0 and 3, inclusive). Unless you are
52
+ looking for something highly specialized, this is almost certainly the entry point you want. A
53
+ sample transpilation looks like::
54
+
55
+ from qiskit.circuit import QuantumCircuit
56
+ from qiskit.transpiler import generate_preset_pass_manager
57
+ from qiskit_ibm_runtime import QiskitRuntimeService
58
+
59
+ # Any abstract circuit you want:
60
+ abstract = QuantumCircuit(2)
61
+ abstract.h(0)
62
+ abstract.cx(0, 1)
63
+
64
+ # Any method you like to retrieve the backend you want to run on:
65
+ backend = QiskitRuntimeService().backend("some-backend")
66
+
67
+ # Create the pass manager for the transpilation ...
68
+ pm = generate_preset_pass_manager(backend=backend)
69
+ # ... and use it (as many times as you like).
70
+ physical = pm.run(abstract)
71
+
72
+ For most use cases, this is all you need.
73
+ All of Qiskit's transpiler infrastructure is highly extensible and configurable, however.
74
+ The rest of this page details how to harness the low-level capabilities of the transpiler stack.
75
+
76
+ .. _transpiler-preset:
77
+
78
+ Preset pass managers
79
+ ====================
85
80
 
86
- If you'd like to work directly with a
87
- preset pass manager you can use the :func:`~.generate_preset_pass_manager`
88
- function to easily generate one. For example:
81
+ The function :func:`.generate_preset_pass_manager` creates the "preset pass managers".
82
+ These are all instances of :class:`.PassManager`, so are used by passing a :class:`.QuantumCircuit`
83
+ to the :meth:`.PassManager.run` method. More specifically, the preset pass managers are instances
84
+ of :class:`.StagedPassManager`, which allows greater configuration of the individual stages of a
85
+ transpilation, including pre- and post-stage hooks.
86
+
87
+ A preset pass manager has up to six named stages. These are summarized, in order of execution,
88
+ below, with more in-depth information in the following subsections.
89
+
90
+ ``init``
91
+ Abstract-circuit optimizations, and reduction of multi-qubit operations to one- and two-qubit
92
+ operations. See :ref:`transpiler-preset-stage-init` for more details.
93
+
94
+ ``layout``
95
+ Choose an initial mapping of virtual qubits to physical qubits, including expansion of the
96
+ circuit to contain explicit ancillas. This stage sometimes subsumes ``routing``. See
97
+ :ref:`transpiler-preset-stage-layout` for more details.
98
+
99
+ ``routing``
100
+ Insert gates into the circuit to ensure it matches the connectivity constraints of the
101
+ :class:`.Target`. The inserted gates need not match the target ISA yet, so are often just
102
+ ``swap`` instructions. This stage is sometimes omitted, when the ``layout`` stage handles its
103
+ job. See :ref:`transpiler-preset-stage-routing` for more details.
104
+
105
+ ``translation``
106
+ Convert all gates in the circuit to ones matching the ISA of the :class:`Target`. See
107
+ :ref:`transpiler-preset-stage-translation` for more details.
108
+
109
+ ``optimization``
110
+ Low-level, hardware-aware optimizations. Unlike the abstract optimizations of the ``init``
111
+ stage, this stage acts on a physical circuit. See :ref:`transpiler-preset-stage-optimization`
112
+ for more details.
113
+
114
+ ``scheduling``
115
+ Insert :class:`~.circuit.Delay` instructions to make the wall-clock timing of a circuit
116
+ explicit. This may also include hardware-aware online error reduction techniques such as
117
+ dynamical decoupling, which are dependent on knowing wall-clock timings. See
118
+ :ref:`transpiler-preset-stage-scheduling` for more details.
119
+
120
+ The preset transpiler pipelines can also be configured at a high level by setting an
121
+ ``optimization_level``. This is an integer from 0 to 3, inclusive, indicating the relative effort to
122
+ exert in attempting to optimize the circuit for the hardware. Level 0 disables all unnecessary
123
+ optimizations; only transformations needed to make the circuit runnable are used. On
124
+ the other end, level 3 enables a full range of optimization techniques, some of which can be very
125
+ expensive in compilation time. Similar to classical compilers, optimization level 3 is not always
126
+ guaranteed to produce the best results. Qiskit defaults to optimization level 2, as a trade-off
127
+ between compilation time and the expected amount of optimization.
128
+
129
+ The optimization level affects which implementations are used for a given stage by default, though
130
+ this can be overridden by passing explicit ``<stage>_method="<choice>"`` arguments to
131
+ :func:`.generate_preset_pass_manager`.
132
+
133
+ .. note::
134
+
135
+ The preset pass managers almost always include stochastic, heuristic-based passes. If you need
136
+ to ensure reproducibility of a compilation, pass a known integer to the ``seed_transpiler``
137
+ argument to the generator functions.
138
+
139
+ This stochasticity arises because many of the problems the transpiler must solve are known to be
140
+ non-polynomial in complexity, but transpilation must complete in a workable amount of time.
141
+
142
+ Choosing preset stage implementations
143
+ -------------------------------------
144
+
145
+ Qiskit includes several implementations of the above stages, and more can be installed as
146
+ separate "plugins". To control which implementation of a stage is used, pass its name to the
147
+ ``<stage>_method`` keyword argument of the two functions, such as
148
+ ``translation_method="translator"``. To read more about implementing such external plugins for a
149
+ stage, see :mod:`qiskit.transpiler.preset_passmanagers.plugin`.
150
+
151
+ For example, to generate a preset pass manager at optimization level 1 that explicitly uses the
152
+ ``trivial`` method for layout with the ``sabre`` method for routing, we would do:
89
153
 
90
- .. code-block:: python
154
+ .. plot::
155
+ :include-source:
156
+ :nofigs:
91
157
 
92
- from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
158
+ from qiskit.transpiler import generate_preset_pass_manager
93
159
  from qiskit.providers.fake_provider import GenericBackendV2
94
160
 
161
+ # Whatever backend you like:
95
162
  backend = GenericBackendV2(num_qubits=5)
96
- pass_manager = generate_preset_pass_manager(3, backend)
97
-
98
- which will generate a :class:`~.StagedPassManager` object for optimization level 3
99
- targeting the :class:`~.GenericBackendV2` backend (equivalent to what is used internally
100
- by :func:`~.transpile` with ``backend=GenericBackendV2(5)`` and ``optimization_level=3``).
101
- You can use this just like you would any other :class:`~.PassManager`. However,
102
- because it is a :class:`~.StagedPassManager` it also makes it easy to compose and/or
103
- replace stages of the pipeline. For example, if you wanted to run a custom scheduling
104
- stage using dynamical decoupling (via the :class:`~.PadDynamicalDecoupling` pass) and
105
- also add initial logical optimization prior to routing, you would do something like
106
- (building off the previous example):
163
+
164
+ pass_manager = generate_preset_pass_manager(
165
+ optimization_level=1,
166
+ backend=backend,
167
+ layout_method="trivial",
168
+ routing_method="sabre",
169
+ )
170
+
171
+ .. note::
172
+
173
+ The built-in set of available plugins for each stage is part of Qiskit's public API, and subject
174
+ to all the stability guarantees. This includes the high-level logical effects of that method
175
+ (for example, ``routing_method="sabre"`` will always use a Sabre-derived algorithm). The exact
176
+ internal construction of the :class:`.PassManager` representing the stage is not, however; the
177
+ order of passes might change between minor versions, or new passes might be introduced.
178
+
179
+ For any stage that has one, the method named ``"default"`` is the most subject to change.
180
+ Qiskit typically only makes complete algorithmic changes in the default method across a
181
+ major-version boundary, but it might rebalance heuristics and add new passes to default
182
+ methods between minor versions.
183
+
184
+ Since the output of :func:`.generate_preset_pass_manager` is a :class:`.StagedPassManager`, you can
185
+ also modify the pass manager after its creation to provide an entirely custom stage implementation.
186
+ For example, if you wanted to run a custom scheduling stage using dynamical decoupling (using the
187
+ :class:`~.PadDynamicalDecoupling` pass) and also add initial logical optimization prior to routing,
188
+ you would do something like the following (building off the previous example):
107
189
 
108
190
  .. code-block:: python
109
191
 
110
192
  import numpy as np
111
193
  from qiskit.providers.fake_provider import GenericBackendV2
112
- from qiskit.circuit.library import HGate, PhaseGate, RXGate, TdgGate, TGate, XGate
194
+ from qiskit.circuit import library as lib
113
195
  from qiskit.transpiler import PassManager, generate_preset_pass_manager
114
196
  from qiskit.transpiler.passes import (
115
197
  ALAPScheduleAnalysis,
@@ -119,7 +201,7 @@ also add initial logical optimization prior to routing, you would do something l
119
201
  )
120
202
 
121
203
  backend = GenericBackendV2(num_qubits=5)
122
- dd_sequence = [XGate(), XGate()]
204
+ dd_sequence = [lib.XGate(), lib.XGate()]
123
205
  scheduling_pm = PassManager(
124
206
  [
125
207
  ALAPScheduleAnalysis(target=backend.target),
@@ -127,22 +209,15 @@ also add initial logical optimization prior to routing, you would do something l
127
209
  ]
128
210
  )
129
211
  inverse_gate_list = [
130
- HGate(),
131
- (RXGate(np.pi / 4), RXGate(-np.pi / 4)),
132
- (PhaseGate(np.pi / 4), PhaseGate(-np.pi / 4)),
133
- (TGate(), TdgGate()),
212
+ lib.CXGate(),
213
+ lib.HGate(),
214
+ (lib.RXGate(np.pi / 4), lib.RXGate(-np.pi / 4)),
215
+ (lib.PhaseGate(np.pi / 4), lib.PhaseGate(-np.pi / 4)),
216
+ (lib.TGate(), lib.TdgGate()),
134
217
  ]
135
- logical_opt = PassManager(
136
- [
137
- CXCancellation(),
138
- InverseCancellation(inverse_gate_list),
139
- ]
140
- )
141
-
142
- pass_manager = generate_preset_pass_manager(
143
- optimization_level=0
144
- )
218
+ logical_opt = PassManager([InverseCancellation(inverse_gate_list)])
145
219
 
220
+ pass_manager = generate_preset_pass_manager(optimization_level=0)
146
221
  # Add pre-layout stage to run extra logical optimization
147
222
  pass_manager.pre_layout = logical_opt
148
223
  # Set scheduling stage to custom pass manager
@@ -152,7 +227,593 @@ Now, when the staged pass manager is run via the :meth:`~.StagedPassManager.run`
152
227
  the ``logical_opt`` pass manager will be called before the ``layout`` stage, and the
153
228
  ``scheduling_pm`` pass manager will be used for the ``scheduling`` stage instead of the default.
154
229
 
155
- Custom Pass Managers
230
+ If you are constructing custom stages for the preset pass managers, you may find some of the
231
+ low-level helper functions in :mod:`qiskit.transpiler.preset_passmanagers` useful.
232
+
233
+ .. _transpiler-preset-stage-init:
234
+
235
+ Initialization stage
236
+ --------------------
237
+
238
+ .. seealso::
239
+ `Init stage explanation <https://docs.quantum.ibm.com/guides/transpiler-stages#init-stage>`__
240
+ Higher-level user-facing explanation of the init stage in the IBM Quantum guide.
241
+
242
+ The ``init`` stage is responsible for high-level, logical optimizations on abstract circuits, and
243
+ for lowering multi-qubit (3+) operations down to a series of one- and two-qubit operations. As this is
244
+ the first stage run, its input is a fully abstract circuit. The ``init`` stage must be able to
245
+ handle custom user-defined gates, and all the high-level abstract circuit-description objects, such
246
+ as :class:`.AnnotatedOperation`.
247
+
248
+ The output of the ``init`` stage is an abstract circuit that contains only one- and two-qubit
249
+ operations.
250
+
251
+ When writing :ref:`stage plugins <transpiler-preset-stage-plugins>`, the entry point for ``init`` is
252
+ ``qiskit.transpiler.init``. The built-in plugins are:
253
+
254
+ .. list-table::
255
+ :header-rows: 1
256
+
257
+ * - Method
258
+ - Summary
259
+
260
+ * - :ref:`default <transpiler-preset-stage-init-default>`
261
+ - Built-in unrolling of multi-qubit operations and abstract optimizations.
262
+
263
+
264
+ .. _transpiler-preset-stage-init-default:
265
+
266
+ Built-in ``default`` plugin
267
+ ...........................
268
+
269
+ At optimization level 0, no abstract optimization is done. The default plugin simply "unrolls"
270
+ operations with more than three qubits by accessing their hierarchical
271
+ :class:`~.circuit.Instruction.definition` fields.
272
+
273
+ At optimization levels 1 and above, the default plugin also does simple cancellation of adjacent
274
+ inverse gates, such as two back-to-back ``cx`` gates.
275
+
276
+ At optimization levels 2 and 3, the default plugin enables a much wider range of abstract
277
+ optimizations. This includes:
278
+
279
+ * "Virtual permutation elision" (see :class:`.ElidePermutations`), where explicit
280
+ permutation-inducing operations are removed and instead effected as remapping of virtual qubits.
281
+ * Analysis of the commutation structure of the IR to find pairs of gates that can be canceled out.
282
+ * Numerical splitting of two-qubit operations that can be expressed as a series of separable
283
+ one-qubit operations.
284
+ * Removal of imperceivable operations, such as tiny-angle Pauli rotations and diagonal operations
285
+ immediately preceding measurements.
286
+
287
+ .. _transpiler-preset-stage-layout:
288
+
289
+ Layout stage
290
+ ------------
291
+
292
+ .. seealso::
293
+ `Layout stage explanation <https://docs.quantum.ibm.com/guides/transpiler-stages#layout-stage>`__
294
+ Higher-level user-facing explanation of the layout stage in the IBM Quantum guide.
295
+
296
+ The layout stage is responsible for making an initial mapping between the virtual qubits of the
297
+ input circuit, and the hardware qubits of the target. This includes expanding the input circuit
298
+ with explicit ancillas so it has as many qubits as the target has, and rewriting all operations in
299
+ terms of hardware qubits. You may also see this problem called the "placement" problem in other
300
+ toolkits or literature.
301
+
302
+ The layout stage must set the properties ``layout`` and ``original_qubit_indices`` in the pipeline's
303
+ :class:`.PropertySet`.
304
+
305
+ .. note::
306
+
307
+ All built-in plugins for the layout stage will give priority to an explicit layout selected
308
+ using the ``initial_layout`` argument to :func:`.generate_preset_pass_manager` or
309
+ :func:`.transpile`.
310
+
311
+ At any given point in a circuit, we can identify a mapping between currently active "virtual" qubits
312
+ of the input circuit to hardware qubits of the backend. A hardware qubit can only ever represent a
313
+ single virtual qubit at a given point, but the mapping might vary over the course of the circuit.
314
+ In principle, some virtual qubits might not be mapped at all points in the circuit
315
+ execution, if the lifetime of a virtual qubit state can be shortened, though Qiskit's built-in
316
+ pipelines do not use this currently.
317
+
318
+ .. image:: /source_images/mapping.png
319
+ :alt: Illustration of how virtual qubits from an input circuit could be mapped to hardware
320
+ qubits on a backend device's connectivity map.
321
+
322
+ The layout stage is not responsible for ensuring that the connectivity of the target
323
+ is respected all the way through the circuit, nor that all operations are valid for direct execution
324
+ on the target; these are the responsibilities of the :ref:`routing
325
+ <transpiler-preset-stage-routing>` and :ref:`translation <transpiler-preset-stage-translation>`
326
+ stages, respectively.
327
+
328
+ The choice of initial layout is one of the most important factors that affects the quality of the
329
+ output circuit. The layout stage is often the most computationally expensive stage in the default
330
+ pipelines; the default plugin for layout even tries several different algorithms (described in more
331
+ detail in :ref:`transpiler-preset-stage-layout-default`).
332
+
333
+ The ideal situation for the layout stage is to find a "perfect" layout, where all operations
334
+ respect the connectivity constraints of the :class:`.Target` such that the routing stage
335
+ is not required. This is typically not possible for arbitrary input circuits, but when it is, the
336
+ :class:`.VF2Layout` pass can be used to find a valid initial layout. If multiple perfect layouts
337
+ are found, a scoring heuristic based on estimated error rates is used to decide which one to use.
338
+
339
+ In all built-in plugins, passing the :func:`.generate_preset_pass_manager` argument
340
+ ``initial_layout`` causes the given layout to be used verbatim, skipping the individual "choosing"
341
+ logic. All built-in plugins also handle embedding the circuit into the full width of the device,
342
+ including assigning ancillas.
343
+
344
+ If you write your own layout plugin, you might find :func:`.generate_embed_passmanager` useful for
345
+ automating the "embedding" stage of the layout application.
346
+
347
+ When writing :ref:`stage plugins <transpiler-preset-stage-plugins>`, the entry point for ``layout``
348
+ is ``qiskit.transpiler.layout``. The built-in plugins are:
349
+
350
+ .. list-table::
351
+ :header-rows: 1
352
+
353
+ * - Method
354
+ - Summary
355
+
356
+ * - :ref:`default <transpiler-preset-stage-layout-default>`
357
+ - At the highest optimization levels, attempts to find a perfect layout, then tries a
358
+ Sabre-based layout-and-routing combined pass.
359
+
360
+ * - :ref:`dense <transpiler-preset-stage-layout-dense>`
361
+ - Finds the densest subgraph (in terms of qubit link degrees) of the backend to use as the
362
+ initial qubits.
363
+
364
+ * - :ref:`trivial <transpiler-preset-stage-layout-trivial>`
365
+ - Maps virtual qubit 0 to physical qubit 0, and so on.
366
+
367
+ * - :ref:`sabre <transpiler-preset-stage-layout-sabre>`
368
+ - Uses `Qiskit's enhanced Sabre layout algorithm <sabre-lightsabre-paper_>`_.
369
+
370
+ At all optimization levels, the default layout method is ``default``, though the structure of this
371
+ stage changes dramatically based on the level.
372
+
373
+ .. _transpiler-preset-stage-layout-default:
374
+
375
+ Built-in ``default`` plugin
376
+ ...........................
377
+
378
+ An amalgamation of several different layout techniques.
379
+
380
+ At optimization level 0, the trivial layout is chosen.
381
+
382
+ At optimization levels above 0, there is a two-step process:
383
+
384
+ #. First, use :class:`.VF2Layout` to attempt to find a "perfect" layout. The maximum number of
385
+ calls to the isomorphism evaluator increases with the optimization level. For huge, complex
386
+ targets, we are not guaranteed to find perfect layouts even if they exist, but the chance
387
+ increases with the optimization level.
388
+
389
+ #. If no perfect layout can be found, use :class:`.SabreLayout` to choose an initial layout, with
390
+ the numbers of initial layout trials, swap-map trials, and forwards–backwards iterations
391
+ increasing with the optimization level.
392
+
393
+ In addition, optimization level 1 also tries the trivial layout before the VF2-based version,
394
+ for historical backwards compatibility.
395
+
396
+
397
+ .. _transpiler-preset-stage-layout-dense:
398
+
399
+ Built-in ``dense`` plugin
400
+ .........................
401
+
402
+ Uses the :class:`.DenseLayout` pass to choose the layout. This pass finds the densest connected
403
+ subgraph of the complete target connectivity graph, where "densest" means that hardware qubits with
404
+ the greatest number of available connections are preferred. The virtual-to-hardware mapping is
405
+ completed by assigning the highest-degree virtual qubits to the highest-degree hardware qubits.
406
+
407
+ This is a relatively cheap heuristic for choosing an initial layout, but typically has far worse
408
+ output quality than Sabre-based methods. The :ref:`default layout plugin
409
+ <transpiler-preset-stage-layout-default>` uses the initial mapping selected by :class:`.DenseLayout`
410
+ as one of its initial layouts to seed the Sabre algorithm.
411
+
412
+ .. _transpiler-preset-stage-layout-trivial:
413
+
414
+ Built-in ``trivial`` plugin
415
+ ...........................
416
+
417
+ Uses the :class:`.TrivialLayout` pass to choose the layout. This is the simplest assignment, where
418
+ each virtual qubit is assigned to the hardware qubit with the same index, so virtual qubit 0 is
419
+ mapped to hardware qubit 0, and so on.
420
+
421
+ This method is most useful for hardware-characterization experiments, where the incoming "abstract"
422
+ circuit is already full-width on the device, its operations correspond to physical operations, and
423
+ the transpiler is just being invoked to formalize the creation of a physical
424
+ :class:`.QuantumCircuit`.
425
+
426
+
427
+ .. _transpiler-preset-stage-layout-sabre:
428
+
429
+ Built-in ``sabre`` plugin
430
+ .........................
431
+
432
+ Uses the :class:`.SabreLayout` to choose an initial layout, using Qiskit's modified :ref:`Sabre
433
+ routing algorithm <transpiler-preset-stage-routing-sabre>` as the subroutine to swap-map the
434
+ candidate circuit both forwards and backwards.
435
+
436
+ Summarily, the layout component of `the original Sabre algorithm <sabre-original-paper_>`_
437
+ chooses an initial layout arbitrarily, then tries to "improve" it by running routing on the circuit,
438
+ reversing the circuit, and running routing on the reversed circuit with the previous "final"
439
+ virtual-to-hardware assignment as the initial state. The configured optimization level decides how
440
+ many iterations of this to-and-fro to do, and how many different random initial layouts to try.
441
+
442
+ The principal difference to the :ref:`default stage <transpiler-preset-stage-layout-default>` at
443
+ optimization levels other than 0 is that this plugin *only* runs the Sabre-based algorithm. It
444
+ does not attempt to find a perfect layout, nor attempt the trivial layout.
445
+
446
+
447
+
448
+ .. _transpiler-preset-stage-routing:
449
+
450
+ Routing stage
451
+ -------------
452
+
453
+ .. seealso::
454
+ `Routing stage explanation <https://docs.quantum.ibm.com/guides/transpiler-stages#routing-stage>`__
455
+ Higher-level user-facing explanation of the routing stage in the IBM Quantum guide.
456
+
457
+ The routing stage ensures that the virtual connectivity graph of the circuit is compatible with the
458
+ hardware connectivity graph of the target. In simpler terms, the routing stage makes sure that all
459
+ two-qubit gates in the circuit are mapped to hardware qubits that have a defined two-qubit operation
460
+ in the target ISA. You may also see this problem referred to as the "mapping" or "swap-mapping"
461
+ problem in other toolkits or literature.
462
+
463
+ Routing algorithms typically do this by inserting ``swap`` gates into the circuit, and modifying the
464
+ virtual-to-hardware mapping of qubits over the course of the circuit execution.
465
+
466
+ The routing stage does not need to ensure that all the gates in the circuit are valid for the target
467
+ ISA. For example, a routing plugin can leave literal ``swap`` gates in the circuit, even if the
468
+ :class:`.Target` does not contain :class:`.SwapGate`. However, there must be at least one two-qubit
469
+ gate defined in the :class:`.Target` for any pair of hardware qubits that has a gate applied in the
470
+ circuit.
471
+
472
+ The routing stage must set the ``final_layout`` and ``virtual_permutation_layout`` properties in
473
+ the :class:`.PropertySet` if routing has taken place.
474
+
475
+ All of Qiskit's built-in routing stages will additionally run the :class:`.VF2PostLayout` pass after
476
+ routing. This might reassign the initial layout, if lower-error qubits can be found. This
477
+ pass is very similar to the :class:`.VF2Layout` class that :ref:`the default layout plugin
478
+ <transpiler-preset-stage-layout-default>` uses, except in :class:`.VF2PostLayout` we can guarantee
479
+ that there is at least one isomorphic induced subgraph of the target topology that matches the
480
+ circuit topology.
481
+
482
+ .. note::
483
+
484
+ Qiskit's built-in routing plugins all generally assume that all pairs of qubits with a
485
+ defined two-qubit link have a *universal* set of gates defined for those two qubits. Hardware
486
+ does not necessarily need to respect this (for example, if the only defined two-qubit gate is
487
+ ``swap``, then entangling operations like ``cx`` cannot be realized), but Qiskit does not yet
488
+ consider this possibility.
489
+
490
+ .. note::
491
+
492
+ Finding the minimal number of swaps to insert is known to be a non-polynomial problem. This
493
+ means it is prohibitively expensive to attempt, so many of Qiskit's built-in algorithms are
494
+ stochastic, and you may see large variations between different compilations. If you need
495
+ reproducibility, be sure to set the ``seed_transpiler`` argument of
496
+ :func:`.generate_preset_pass_manager` or :func:`.transpile`.
497
+
498
+ When writing :ref:`stage plugins <transpiler-preset-stage-plugins>`, the entry point for ``routing``
499
+ is ``qiskit.transpiler.routing``. The built-in plugins are:
500
+
501
+ .. list-table::
502
+ :header-rows: 1
503
+
504
+ * - Method
505
+ - Summary
506
+
507
+ * - :ref:`sabre <transpiler-preset-stage-routing-sabre>`
508
+ - Default. Uses `Qiskit's modified Sabre routing algorithm <sabre-lightsabre-paper_>`_ to
509
+ swap map.
510
+
511
+ * - :ref:`none <transpiler-preset-stage-routing-none>`
512
+ - Disable routing. Raises an error if routing is required.
513
+
514
+ * - :ref:`basic <transpiler-preset-stage-routing-basic>`
515
+ - Greedy swap insertion to route a single operation at a time.
516
+
517
+ * - :ref:`stochastic <transpiler-preset-stage-routing-stochastic>`
518
+ - Consider operations layer-by-layer, using a stochastic algorithm to find swap networks that
519
+ implement a suitable permutation to make the layer executable.
520
+
521
+ * - :ref:`lookahead <transpiler-preset-stage-routing-lookahead>`
522
+ - Breadth-first search with heuristic pruning to find swaps that make gates executable.
523
+
524
+ .. _transpiler-preset-stage-routing-none:
525
+
526
+ Built-in ``none`` plugin
527
+ ........................
528
+
529
+ A dummy plugin used to disable routing entirely. This can occasionally be useful for
530
+ hardware-configuration experiments, or in certain special cases of partial compilation.
531
+
532
+ .. _transpiler-preset-stage-routing-basic:
533
+
534
+ Built-in ``basic`` plugin
535
+ .........................
536
+
537
+ Uses the :class:`.BasisSwap` greedy swap-insertion algorithm. This is conceptually very simple; for
538
+ each operation in topological order, insert the shortest-path swaps needed to make the connection
539
+ executable on the device.
540
+
541
+ The optimization level only affects the amount of work the :class:`.VF2PostLayout` step does to
542
+ attempt to improve the initial layout after routing.
543
+
544
+ This method typically has poor output quality.
545
+
546
+ .. _transpiler-preset-stage-routing-stochastic:
547
+
548
+ Built-in ``stochastic`` plugin
549
+ ..............................
550
+
551
+ .. deprecated:: 1.3
552
+ Use :ref:`transpiler-preset-stage-routing-sabre` instead.
553
+
554
+ Uses the :class:`.StochasticSwap` algorithm to route. In short, this stratifies the circuit into
555
+ layers, then uses a stochastic algorithm to find a permutation that will allow the layer to execute,
556
+ and a series of swaps that will implement that permutation in a hardware-valid way.
557
+
558
+ The optimization level affects the number of stochastic trials used for each layer, and the amount
559
+ of work spent in :class:`.VF2PostLayout` to optimize the initial layout.
560
+
561
+ This was Qiskit's primary routing algorithm for several years, until approximately 2021. Now, it
562
+ is reliably beaten in runtime and output quality by :ref:`Qiskit's custom Sabre-based routing
563
+ algorithm <transpiler-preset-stage-routing-sabre>`.
564
+
565
+ .. _transpiler-preset-stage-routing-lookahead:
566
+
567
+ Built-in ``lookahead`` plugin
568
+ .............................
569
+
570
+ Uses the :class:`.LookaheadSwap` algorithm to route. This is essentially a breadth-first search
571
+ at producing a swap network, where the tree being explored is pruned down to a small number of
572
+ candidate swaps at each depth.
573
+
574
+ This algorithm is similar to the ``basic`` heuristic of :ref:`the "sabre" plugin
575
+ <transpiler-preset-stage-routing-sabre>`, except it considers the following effects of each swap to
576
+ a small depth as well.
577
+
578
+ The optimization level affects the search depth, the amount of per-depth pruning, and amount of work
579
+ done by :class:`.VF2PostLayout` to post-optimize the initial layout.
580
+
581
+ In practice, :ref:`the "sabre" plugin <transpiler-preset-stage-routing-sabre>` runs several orders
582
+ of magnitude faster, and produces better output.
583
+
584
+ .. _transpiler-preset-stage-routing-sabre:
585
+
586
+ Built-in ``sabre`` plugin
587
+ .........................
588
+
589
+ Uses the :class:`.SabreSwap` algorithm to route. This uses `Qiskit's enhanced version
590
+ <sabre-lightsabre-paper_>`_ of `the original Sabre routing algorithm <sabre-original-paper_>`_.
591
+
592
+ This routing algorithm runs with threaded parallelism to consider several different possibilities
593
+ for routing, choosing the one that minimizes the number of inserted swaps.
594
+
595
+ The optimization level affects how many different stochastic seeds are attempted for the full
596
+ routing, and the amount of work done by :class:`.VF2PostLayout` to post-optimize the initial layout.
597
+
598
+ This is almost invariably the best-performing built-in plugin, and the one Qiskit uses by default in
599
+ all cases where routing is necessary.
600
+
601
+ .. _transpiler-preset-stage-translation:
602
+
603
+ Translation stage
604
+ -----------------
605
+
606
+ .. seealso::
607
+ `Translation stage explanation`__
608
+ Higher-level user-facing explanation of the translation stage in the IBM Quantum guide.
609
+
610
+ .. __: https://docs.quantum.ibm.com/guides/transpiler-stages#translation-stage
611
+
612
+ The translation stage is responsible for rewriting all gates in the circuit into ones that are
613
+ supported by the target ISA. For example, if a ``cx`` is requested on hardware qubits 0 and 1, but
614
+ the ISA only contains a ``cz`` operation on those qubits, the translation stage must find a way of
615
+ representing the ``cx`` gate using the ``cz`` and available one-qubit gates.
616
+
617
+ .. note::
618
+
619
+ In Qiskit 1.x, translation plugins need not output gates with the correct
620
+ directionality, provided the gate exists with opposite directionality on the given qubit pair.
621
+ For example, if ``cx(0, 1)`` is ISA-supported, the translation stage can output
622
+ ``cx(1, 0)``.
623
+
624
+ This is likely to change in later versions of Qiskit.
625
+
626
+ The translation stage is called before entering the optimization stage. Optimization plugins
627
+ (including Qiskit's built-in plugins) may also use the translation stage as a "fixup" stage after
628
+ the optimization loop, if the optimization loop returns a circuit that includes non-ISA gates. This
629
+ latter situation is fairly common; the optimization loop may only be concerned with minimizing
630
+ properties like "number of two-qubit gates", and will leave its output in terms of locally
631
+ equivalent gates, which the translation stage can easily rewrite without affecting the target
632
+ optimization properties. This allows easier separation of concerns between the two stages. Some
633
+ optimization plugins may be stricter in their output, and so this follow-up to the translation stage
634
+ may no longer be necessary.
635
+
636
+ When writing :ref:`stage plugins <transpiler-preset-stage-plugins>`, the entry point for
637
+ ``translation`` is ``qiskit.transpiler.translation``. The built-in plugins are:
638
+
639
+ .. list-table::
640
+ :header-rows: 1
641
+
642
+ * - Method
643
+ - Summary
644
+
645
+ * - :ref:`translator <transpiler-preset-stage-translation-translator>`
646
+ - Symbolic translation of gates to the target basis using known equivalences.
647
+
648
+ * - :ref:`synthesis <transpiler-preset-stage-translation-synthesis>`
649
+ - Collect each run of one- and two-qubit gates into a matrix representation, and resynthesize
650
+ from there.
651
+
652
+ .. _transpiler-preset-stage-translation-synthesis:
653
+
654
+ Built-in ``synthesis`` plugin
655
+ .............................
656
+
657
+ Collect runs of gates on the same qubits into matrix form, and then resynthesize using the
658
+ :class:`.UnitarySynthesis` pass (with the configured ``unitary_synthesis_method``). This is, in
659
+ large part, similar to the optimization loop itself at high optimization levels.
660
+
661
+ The collection into matrices is typically more expensive than matrix-free translations, but in
662
+ principle the quality of the translations can be better. In practice, this requires a synthesis
663
+ algorithm tailored to the target ISA, which makes this method less general than other methods. It
664
+ can produce higher-quality results when targeting simple ISAs that match the synthesis routines
665
+ already in Qiskit.
666
+
667
+ If this method is used, you might not need the optimization loop.
668
+
669
+ The optimization level has no effect on this plugin.
670
+
671
+
672
+ .. _transpiler-preset-stage-translation-translator:
673
+
674
+ Built-in ``translator`` plugin
675
+ ..............................
676
+
677
+ Uses the :class:`.BasisTranslator` algorithm to symbolically translate gates into the target basis.
678
+ At a high level, this starts from the set of gates requested by the circuit, and uses rules from a
679
+ given :class:`.EquivalenceLibrary` (typically the :data:`.SessionEquivalenceLibrary`) to move
680
+ towards the ISA.
681
+
682
+ This is the default translation method.
683
+
684
+ The optimization level has no effect on this plugin.
685
+
686
+
687
+ .. _transpiler-preset-stage-optimization:
688
+
689
+ Optimization stage
690
+ ------------------
691
+
692
+ .. seealso::
693
+ `Optimization stage explanation`__
694
+ Higher-level user-facing explanation of the optimization stage in the IBM Quantum guide.
695
+
696
+ .. __: https://docs.quantum.ibm.com/guides/transpiler-stages#optimization-stage
697
+
698
+ The optimization stage is for low-level hardware-aware optimizations. Unlike :ref:`the init stage
699
+ <transpiler-preset-stage-init>`, the input to this stage is a circuit that is already
700
+ ISA-compatible, so a low-level optimization plugin can be tailored for a particular ISA.
701
+
702
+ There are very few requirements on an optimization plugin, other than it takes in ISA-supported
703
+ circuits, and returns ISA-supported circuits. An optimization plugin will often contain a loop,
704
+ such as the :class:`.DoWhileController`, and might include the configured translation stage
705
+ as a fix-up pipeline.
706
+
707
+ Qiskit's built-in optimization plugins are general, and apply well to most real-world ISAs for
708
+ non-error-corrected devices. The built-in plugins are less well-suited to ISAs that have no
709
+ continuously parametrized single-qubit gate, such as a Clifford+T basis set.
710
+
711
+ When writing :ref:`stage plugins <transpiler-preset-stage-plugins>`, the entry point for
712
+ ``optimization`` is ``qiskit.transpiler.optimization``. The built-in plugins are:
713
+
714
+ .. list-table::
715
+ :header-rows: 1
716
+
717
+ * - Method
718
+ - Summary
719
+
720
+ * - :ref:`default <transpiler-preset-stage-optimization-default>`
721
+ - A default set of optimization passes. This varies significantly between optimization
722
+ levels.
723
+
724
+ .. _transpiler-preset-stage-optimization-default:
725
+
726
+ Built-in ``default`` plugin
727
+ ...........................
728
+
729
+ This varies significantly between optimization levels.
730
+
731
+ The specifics of this pipeline are subject to change between Qiskit versions. The broad principles
732
+ are described below.
733
+
734
+ At optimization level 0, the stage is empty.
735
+
736
+ At optimization level 1, the stage does matrix-based resynthesis of runs of single-qubit gates, and
737
+ very simple symbolic inverse cancellation of two-qubit gates, if they appear consecutively. This
738
+ runs in a loop until the size and depth of the circuit are fixed.
739
+
740
+ At optimization level 2, in addition the optimizations of level 1, the loop contains commutation
741
+ analysis of sets of gates to widen the range of gates that can be considered for cancellation.
742
+ Before the loop, runs of both one- and two-qubit gates undergo a single matrix-based resynthesis.
743
+
744
+ At optimization level 3, the two-qubit matrix-based resynthesis runs inside the optimization loop.
745
+ The optimization loop condition also tries multiple runs and chooses the minimum point in the case
746
+ of fluctuating output; this is necessary because matrix-based resynthesis is relatively unstable in
747
+ terms of concrete gates.
748
+
749
+ Optimization level 3 is typically very expensive for large circuits.
750
+
751
+
752
+ .. _transpiler-preset-stage-scheduling:
753
+
754
+ Scheduling stage
755
+ ----------------
756
+
757
+ .. seealso::
758
+ :ref:`transpiler-scheduling-description`
759
+ A guide-level explanation of scheduling concepts.
760
+
761
+ The scheduling stage, if requested, is responsible for inserting explicit :class:`~.circuit.Delay`
762
+ instructions to make idle periods of qubits explicit. Plugins may optionally choose to do
763
+ walltime-sensitive transformations, such as inserting dynamical decoupling sequences.
764
+
765
+ The input to the scheduling stage is an ISA-compatible circuit. The output of the scheduling stage
766
+ must also be an ISA-compatible circuit, with explicit :class:`~.circuit.Delay` instructions that
767
+ satisfy the hardware's timing information, if appropriate.
768
+
769
+ The scheduling stage should set the ``node_start_time`` property in the pipeline's
770
+ :class:`.PropertySet`.
771
+
772
+ When writing :ref:`stage plugins <transpiler-preset-stage-plugins>`, the entry point for
773
+ ``scheduling`` is ``qiskit.transpiler.scheduling``. The built-in plugins are:
774
+
775
+ .. list-table::
776
+ :header-rows: 1
777
+
778
+ * - Method
779
+ - Summary
780
+
781
+ * - :ref:`default <transpiler-preset-stage-scheduling-default>`
782
+ - Attempt to satisfy timing alignment constraints without otherwise scheduling.
783
+
784
+ * - :ref:`alap <transpiler-preset-stage-scheduling-alap>`
785
+ - Schedule the circuit, preferring operations to be as late as possible.
786
+
787
+ * - :ref:`asap <transpiler-preset-stage-scheduling-asap>`
788
+ - Schedule the circuit, preferring operations to be as soon as possible.
789
+
790
+ .. _transpiler-preset-stage-scheduling-default:
791
+
792
+ Built-in ``default`` plugin
793
+ ...........................
794
+
795
+ Do nothing, unless the circuit already contains instructions with explicit timings. If there are
796
+ explicitly timed operations in the circuit, insert additional padding to ensure that these timings
797
+ satisfy the alignment and other hardware constraints.
798
+
799
+ .. _transpiler-preset-stage-scheduling-alap:
800
+
801
+ Builtin ``alap`` plugin
802
+ .......................
803
+
804
+ Explicitly schedule all operations using an "as late as possible" strategy. This uses the
805
+ :class:`.ALAPScheduleAnalysis` algorithm to decide where to place gates.
806
+
807
+ .. _transpiler-preset-stage-scheduling-asap:
808
+
809
+ Builtin ``asap`` plugin
810
+ .......................
811
+
812
+ Explicitly schedule all operations using an "as soon as possible" strategy. This uses the
813
+ :class:`.ASAPScheduleAnalysis` algorithm to decide where to place gates.
814
+
815
+
816
+ Custom pass managers
156
817
  ====================
157
818
 
158
819
  In addition to modifying preset pass managers, it is also possible to construct a pass
@@ -160,7 +821,11 @@ manager to build an entirely custom pipeline for transforming input
160
821
  circuits. You can use the :class:`~.StagedPassManager` class directly to do
161
822
  this. You can define arbitrary stage names and populate them with a :class:`~.PassManager`
162
823
  instance. For example, the following code creates a new :class:`~.StagedPassManager`
163
- that has 2 stages, ``init`` and ``translation``.::
824
+ that has two stages, ``init`` and ``translation``.
825
+
826
+ .. plot::
827
+ :include-source:
828
+ :nofigs:
164
829
 
165
830
  from qiskit.transpiler.passes import (
166
831
  UnitarySynthesis,
@@ -185,12 +850,13 @@ that has 2 stages, ``init`` and ``translation``.::
185
850
  stages=["init", "translation"], init=init, translation=translate
186
851
  )
187
852
 
188
- There is no limit on the number of stages you can put in a :class:`~.StagedPassManager`.
853
+ There is no limit on the number of stages you can put in a :class:`~.StagedPassManager`. The stages
854
+ do not need to correspond to the stages used by Qiskit's preset pipelines.
189
855
 
190
- The :ref:`stage_generators` may be useful for the construction of custom :class:`~.StagedPassManager`s.
191
- They generate pass managers which provide common functionality used in many stages.
192
- For example, :func:`~.generate_embed_passmanager` generates a :class:`~.PassManager`
193
- to "embed" a selected initial :class:`~.Layout` from a layout pass to the specified target device.
856
+ The :ref:`stage_generators` may be useful for the construction of custom :class:`~.StagedPassManager`
857
+ instances. They generate pass managers which provide common functionality used in many stages. For
858
+ example, :func:`~.generate_embed_passmanager` generates a :class:`~.PassManager` to "embed" a
859
+ selected initial :class:`~.Layout` from a layout pass to the specified target device.
194
860
 
195
861
  Representing Quantum Computers
196
862
  ==============================
@@ -334,7 +1000,7 @@ example 3 qubit :class:`~.Target` above:
334
1000
  .. plot::
335
1001
  :include-source:
336
1002
  :alt: Output from the previous code.
337
-
1003
+
338
1004
  from qiskit.circuit import Parameter, Measure
339
1005
  from qiskit.transpiler import Target, InstructionProperties
340
1006
  from qiskit.circuit.library import UGate, RZGate, RXGate, RYGate, CXGate, CZGate
@@ -504,458 +1170,19 @@ see the individual connectivity, you can pass the operation name to
504
1170
 
505
1171
  target.build_coupling_map('cz').draw()
506
1172
 
507
- .. _transpiler_stage_descriptions:
508
-
509
- Transpiler Stage Details
510
- ========================
511
-
512
- Below are a description of the default transpiler stages and the problems
513
- they solve. The default passes used for each stage are described, but
514
- the specifics are configurable via the ``*_method`` keyword arguments for
515
- the :func:`~.transpile` and :func:`~.generate_preset_pass_manager` functions
516
- which can be used to override the methods described in this section.
517
-
518
- .. _translation_stage:
519
-
520
- Translation Stage
521
- -----------------
522
-
523
- When writing a quantum circuit you are free to use any quantum gate (unitary operator) that
524
- you like, along with a collection of non-gate operations such as qubit measurements and
525
- reset operations. However, most quantum devices only natively support a handful of quantum gates
526
- and non-gate operations. The allowed instructions for a given backend can be found by querying the
527
- :class:`~.Target` for the devices:
528
-
529
- .. code-block::
530
-
531
- from qiskit.providers.fake_provider import GenericBackendV2
532
- backend = GenericBackendV2(5)
533
-
534
- print(backend.target)
535
-
536
- Every quantum circuit run on the target device must be expressed using only these instructions.
537
- For example, to run a simple phase estimation circuit:
538
-
539
- .. plot::
540
- :alt: Circuit diagram output by the previous code.
541
- :include-source:
542
-
543
- import numpy as np
544
- from qiskit import QuantumCircuit
545
- from qiskit.providers.fake_provider import GenericBackendV2
546
-
547
- backend = GenericBackendV2(5)
548
-
549
- qc = QuantumCircuit(2, 1)
550
-
551
- qc.h(0)
552
- qc.x(1)
553
- qc.cp(np.pi/4, 0, 1)
554
- qc.h(0)
555
- qc.measure([0], [0])
556
- qc.draw(output='mpl')
557
-
558
- We have :math:`H`, :math:`X`, and controlled-:math:`P` gates, none of which are
559
- in our device's basis gate set, and thus must be translated.
560
- We can
561
- transpile the circuit to show what it will look like in the native gate set of
562
- the target IBM Quantum device (the :class:`~.GenericBackendV2` class generates
563
- a fake backend with a specified number of qubits for test purposes):
564
-
565
- .. plot::
566
- :alt: Circuit diagram output by the previous code.
567
- :include-source:
568
-
569
- from qiskit import transpile
570
- from qiskit import QuantumCircuit
571
- from qiskit.providers.fake_provider import GenericBackendV2
572
-
573
- backend = GenericBackendV2(5)
574
-
575
- qc = QuantumCircuit(2, 1)
576
-
577
- qc.h(0)
578
- qc.x(1)
579
- qc.cp(np.pi/4, 0, 1)
580
- qc.h(0)
581
- qc.measure([0], [0])
582
-
583
- qc_basis = transpile(qc, backend)
584
- qc_basis.draw(output='mpl')
585
-
586
- A few things to highlight. First, the circuit has gotten longer with respect to the
587
- original. This can be verified by checking the depth of both circuits:
588
-
589
- .. code-block::
590
-
591
- print('Original depth:', qc.depth(), 'Decomposed Depth:', qc_basis.depth())
592
-
593
- .. code-block:: text
594
-
595
- Original depth: 4 Decomposed Depth: 10
596
-
597
- Second, although we had a single controlled gate, the fact that it was not in the basis
598
- set means that, when expanded, it requires more than a single :class:`~.CXGate` to implement.
599
- All said, unrolling to the basis set of gates leads to an increase in the depth of a
600
- quantum circuit and the number of gates.
601
-
602
- It is important to highlight two special cases:
603
-
604
- 1. If A swap gate is not a native gate and must be decomposed this requires three CNOT gates:
605
-
606
- .. code-block::
607
-
608
- from qiskit.providers.fake_provider import GenericBackendV2
609
- backend = GenericBackendV2(5)
610
-
611
- print(backend.operation_names)
612
-
613
- .. code-block:: text
614
-
615
- ['id', 'rz', 'sx', 'x', 'cx', 'measure', 'delay']
616
-
617
- .. plot:
618
- :alt: Circuit diagram output by the previous code.
619
- :include-source:
620
-
621
- from qiskit.circuit import QuantumCircuit
622
-
623
- swap_circ = QuantumCircuit(2)
624
- swap_circ.swap(0, 1)
625
- swap_circ.decompose().draw(output='mpl')
626
-
627
- As a product of three CNOT gates, swap gates are expensive operations to perform on
628
- noisy quantum devices. However, such operations are usually necessary for embedding a
629
- circuit into the limited gate connectivities of many devices. Thus,
630
- minimizing the number of swap gates in a circuit is a primary goal in the
631
- transpilation process.
632
-
633
1173
 
634
- 2. A Toffoli, or controlled-controlled-not gate (``ccx``), is a three-qubit gate. Given
635
- that our basis gate set includes only single- and two-qubit gates, it is obvious that
636
- this gate must be decomposed. This decomposition is quite costly:
1174
+ .. _transpiler-scheduling-description:
637
1175
 
638
- .. plot::
639
- :alt: Circuit diagram output by the previous code.
640
- :include-source:
1176
+ Scheduling of circuits
1177
+ ======================
641
1178
 
642
- from qiskit.circuit import QuantumCircuit
1179
+ ..
1180
+ This section is still here because the content hasn't fully migrated to other places yet, unlike
1181
+ other discussions of the components of quantum compilation.
643
1182
 
644
- ccx_circ = QuantumCircuit(3)
645
- ccx_circ.ccx(0, 1, 2)
646
- ccx_circ.decompose().draw(output='mpl')
647
-
648
- For every Toffoli gate in a quantum circuit, the hardware may execute up to six CNOT
649
- gates, and a handful of single-qubit gates. From this example, it should be
650
- clear that any algorithm that makes use of multiple Toffoli gates will end up as a
651
- circuit with large depth and will therefore be appreciably affected by noise and gate
652
- errors.
653
-
654
-
655
- .. _layout_stage:
656
-
657
- Layout Stage
658
- ------------
659
-
660
- Quantum circuits are abstract entities whose qubits are "virtual" representations of actual
661
- qubits used in computations. We need to be able to map these virtual qubits in a one-to-one
662
- manner to the "physical" qubits in an actual quantum device.
663
-
664
- .. image:: /source_images/mapping.png
665
- :alt: Diagram illustrating how virtual qubits are mapped to physical qubits.
666
-
667
-
668
- By default, qiskit will do this mapping for you. The choice of mapping depends on the
669
- properties of the circuit, the particular device you are targeting, and the optimization
670
- level that is chosen. The choice of initial layout is extremely important for minimizing the
671
- number of swap operations needed to map the input circuit onto the device topology and
672
- for minimizing the loss due to non-uniform noise properties across a device. Due to the
673
- importance of this stage, the preset pass managers
674
- try a few different methods to find the best layout. Typically this involves 2 steps: first,
675
- trying to find a "perfect" layout (a layout which does not require any swap operations), and then,
676
- a heuristic pass that tries to find the best layout to use if a perfect layout cannot be found.
677
- There are 2 passes typically used for the first stage:
678
-
679
- - :class:`~.VF2Layout`: Models layout selection as a subgraph isomorphism problem and tries
680
- to find a subgraph of the connectivity graph that is isomorphic to the
681
- graph of 2 qubit interactions in the circuit. If more than one isomorphic mapping is found a
682
- scoring heuristic is run to select the mapping which would result in the lowest average error
683
- when executing the circuit.
684
-
685
- - :class:`~.TrivialLayout`: Maps each virtual qubit to the same numbered physical qubit on the device,
686
- i.e. ``[0,1,2,3,4]`` -> ``[0,1,2,3,4]``. This is historical behavior used only in
687
- ``optimization_level=1`` to try to find a perfect layout. If it fails to do so, :class:`~.VF2Layout`
688
- is tried next.
689
-
690
- Next, for the heuristic stage, 2 passes are used by default:
691
-
692
- - :class:`~.SabreLayout`: Selects a layout by starting from an initial random layout and then
693
- repeatedly running a routing algorithm (by default :class:`~.SabreSwap`) both forward and
694
- backward over the circuit, using the permutation caused by swap insertions to adjust that
695
- initial random layout. For more details you can refer to the paper describing the algorithm:
696
- `arXiv:1809.02573 <https://arxiv.org/abs/1809.02573>`__
697
- :class:`~.SabreLayout` is used to select a layout if a perfect layout isn't found for
698
- optimization levels 1, 2, and 3.
699
- - :class:`~.TrivialLayout`: Always used for the layout at optimization level 0.
700
-
701
- There are other passes than can be used for the heuristic stage, but are not included in the default
702
- pipeline, such as:
703
-
704
- - :class:`~.DenseLayout`: Finds the sub-graph of the device with greatest connectivity
705
- that has the same number of qubits as the circuit.
706
-
707
- Let's see what layouts are automatically picked at various optimization levels. The circuits
708
- returned by :func:`qiskit.compiler.transpile` are annotated with this initial layout information,
709
- and we can view this layout selection graphically using
710
- :func:`qiskit.visualization.plot_circuit_layout`:
711
-
712
- .. plot::
713
- :alt: Circuit diagram output by the previous code.
714
- :include-source:
715
-
716
- from qiskit import QuantumCircuit, transpile
717
- from qiskit.visualization import plot_circuit_layout
718
- from qiskit.providers.fake_provider import Fake5QV1
719
- backend = Fake5QV1()
720
-
721
- ghz = QuantumCircuit(3, 3)
722
- ghz.h(0)
723
- ghz.cx(0,range(1,3))
724
- ghz.barrier()
725
- ghz.measure(range(3), range(3))
726
- ghz.draw(output='mpl')
727
-
728
-
729
- - **Layout Using Optimization Level 0**
730
-
731
- .. plot::
732
- :alt: Output from the previous code.
733
- :include-source:
734
-
735
- from qiskit import QuantumCircuit, transpile
736
- from qiskit.visualization import plot_circuit_layout
737
- from qiskit.providers.fake_provider import Fake5QV1
738
- backend = Fake5QV1()
739
-
740
- ghz = QuantumCircuit(3, 3)
741
- ghz.h(0)
742
- ghz.cx(0,range(1,3))
743
- ghz.barrier()
744
- ghz.measure(range(3), range(3))
745
-
746
- new_circ_lv0 = transpile(ghz, backend=backend, optimization_level=0)
747
- plot_circuit_layout(new_circ_lv0, backend)
748
-
749
- - **Layout Using Optimization Level 3**
750
-
751
- .. plot::
752
- :alt: Output from the previous code.
753
- :include-source:
754
-
755
- from qiskit import QuantumCircuit, transpile
756
- from qiskit.visualization import plot_circuit_layout
757
- from qiskit.providers.fake_provider import Fake5QV1
758
- backend = Fake5QV1()
759
-
760
- ghz = QuantumCircuit(3, 3)
761
- ghz.h(0)
762
- ghz.cx(0,range(1,3))
763
- ghz.barrier()
764
- ghz.measure(range(3), range(3))
765
-
766
- new_circ_lv3 = transpile(ghz, backend=backend, optimization_level=3)
767
- plot_circuit_layout(new_circ_lv3, backend)
768
-
769
-
770
- It is possible to override automatic layout selection by specifying an initial layout. To do so we can
771
- pass a list of integers to :func:`qiskit.compiler.transpile` via the `initial_layout`
772
- keyword argument, where the index labels the virtual qubit in the circuit and the
773
- corresponding value is the label for the physical qubit to map onto:
774
-
775
- .. plot::
776
- :alt: Output from the previous code.
777
- :include-source:
778
-
779
- from qiskit import QuantumCircuit, transpile
780
- from qiskit.visualization import plot_circuit_layout
781
- from qiskit.providers.fake_provider import Fake5QV1
782
- backend = Fake5QV1()
783
-
784
- ghz = QuantumCircuit(3, 3)
785
- ghz.h(0)
786
- ghz.cx(0,range(1,3))
787
- ghz.barrier()
788
- ghz.measure(range(3), range(3))
789
-
790
- # Virtual -> physical
791
- # 0 -> 3
792
- # 1 -> 4
793
- # 2 -> 2
794
-
795
- my_ghz = transpile(ghz, backend, initial_layout=[3, 4, 2])
796
- plot_circuit_layout(my_ghz, backend)
797
-
798
- .. _routing_stage:
799
-
800
- Routing Stage
801
- -------------
802
-
803
- In order to implement a 2-qubit gate between qubits in a quantum circuit that are not directly
804
- connected on a quantum device, one or more swap gates must be inserted into the circuit to
805
- move the qubit states around until they are adjacent on the device gate map. Each swap
806
- gate typically represents an expensive and noisy operation to perform. Thus, finding the
807
- minimum number of swap gates needed to map a circuit onto a given device, is an important
808
- step (if not the most important) in the whole execution process.
809
-
810
- However, as with many important things in life, finding the optimal swap mapping is hard.
811
- In fact it is in a class of problems called NP-hard, and is thus prohibitively expensive
812
- to compute for all but the smallest quantum devices and input circuits. To get around this,
813
- by default Qiskit uses a stochastic heuristic algorithm called :class:`~.SabreSwap` to compute
814
- a good, but not necessarily optimal swap mapping. The use of a stochastic method means the
815
- circuits generated by :func:`~.transpile`
816
- are not guaranteed to be the same over repeated runs. Indeed, running the same
817
- circuit repeatedly will in general result in a distribution of circuit depths and gate counts
818
- at the output.
819
-
820
- In order to highlight this, we run a GHZ circuit 100 times, using a "bad" (disconnected)
821
- ``initial_layout`` in a heavy hex coupling map:
822
-
823
- .. plot::
824
- :alt: Diagram illustrating the previously described circuit.
825
-
826
- from qiskit import QuantumCircuit, transpile
827
-
828
- ghz = QuantumCircuit(15)
829
- ghz.h(0)
830
- ghz.cx(0, range(1, 15))
831
- ghz.draw(output='mpl')
832
-
833
- .. plot::
834
- :alt: Output from the previous code.
835
- :include-source:
836
-
837
- import matplotlib.pyplot as plt
838
- from qiskit import QuantumCircuit, transpile
839
- from qiskit.providers.fake_provider import GenericBackendV2
840
- from qiskit.transpiler import CouplingMap
841
-
842
- coupling_map = CouplingMap.from_heavy_hex(3)
843
- backend = GenericBackendV2(coupling_map.size(), coupling_map=coupling_map)
844
-
845
- ghz = QuantumCircuit(15)
846
- ghz.h(0)
847
- ghz.cx(0, range(1, 15))
848
-
849
- depths = []
850
- for i in range(100):
851
- depths.append(
852
- transpile(
853
- ghz,
854
- backend,
855
- seed_transpiler=i,
856
- layout_method='trivial' # Fixed layout mapped in circuit order
857
- ).depth()
858
- )
859
-
860
- plt.figure(figsize=(8, 6))
861
- plt.hist(depths, align='left', color='#AC557C')
862
- plt.xlabel('Depth', fontsize=14)
863
- plt.ylabel('Counts', fontsize=14);
864
-
865
-
866
- This distribution is quite wide, signaling the difficulty the swap mapper is having
867
- in computing the best mapping. Most circuits will have a distribution of depths,
868
- perhaps not as wide as this one, due to the stochastic nature of the default swap
869
- mapper. Of course, we want the best circuit we can get, especially in cases where
870
- the depth is critical to success or failure. The :class:`~.SabreSwap` pass will by default by run its
871
- algorithm in parallel with multiple seed values and select the output which
872
- uses the fewest swaps. If you would like to increase the number of trials
873
- :class:`~.SabreSwap` runs you can refer to :ref:`working_with_preset_pass_managers`
874
- and modify the ``routing`` stage with a custom instance of :class:`~.SabreSwap`
875
- with a larger value for the ``trials`` argument.
876
-
877
- Typically, following the swap mapper, the routing stage in the preset pass managers
878
- also includes running the :class:`~.VF2PostLayout` pass. As its name implies,
879
- :class:`~.VF2PostLayout` uses the same basic algorithm as :class:`~.VF2Layout`,
880
- but instead of using it to find a perfect initial layout, it is designed to run after
881
- mapping and try to find a layout on qubits with lower error rates which will
882
- result in better output fidelity when running the circuit. The details of this
883
- algorithm are described in `arXiv:2209.15512 <https://arxiv.org/abs/2209.15512>`__.
884
-
885
- .. _optimization_stage:
886
-
887
- Optimization Stage
888
- ------------------
889
-
890
- Decomposing quantum circuits into the basis gate set of the target device,
891
- and the addition of swap gates needed to match hardware topology, conspire to
892
- increase the depth and gate count of quantum circuits. Fortunately many routines
893
- for optimizing circuits by combining or eliminating gates exist. In some cases
894
- these methods are so effective the output circuits have lower depth than the inputs.
895
- In other cases, not much can be done, and the computation may be difficult to
896
- perform on noisy devices. Different gate optimizations are turned on with
897
- different ``optimization_level`` values. Below we show the benefits gained from
898
- setting the optimization level higher:
899
-
900
- .. important::
901
-
902
- The output from :func:`.transpile` varies due to the stochastic swap mapper.
903
- So the numbers below will likely change each time you run the code.
904
-
905
-
906
- .. plot::
907
- :alt: Diagram illustrating the previously described circuit.
908
-
909
- import matplotlib.pyplot as plt
910
- from qiskit import QuantumCircuit, transpile
911
- from qiskit.providers.fake_provider import GenericBackendV2
912
- backend = GenericBackendV2(16)
913
-
914
- ghz = QuantumCircuit(15)
915
- ghz.h(0)
916
- ghz.cx(0, range(1, 15))
917
- ghz.draw(output='mpl')
918
-
919
- .. plot::
920
- :alt: Output from the previous code.
921
- :include-source:
922
-
923
- import matplotlib.pyplot as plt
924
- from qiskit import QuantumCircuit, transpile
925
- from qiskit.providers.fake_provider import GenericBackendV2
926
- backend = GenericBackendV2(16)
927
-
928
- ghz = QuantumCircuit(15)
929
- ghz.h(0)
930
- ghz.cx(0, range(1, 15))
931
-
932
- depths = []
933
- gate_counts = []
934
- non_local_gate_counts = []
935
- levels = [str(x) for x in range(4)]
936
- for level in range(4):
937
- circ = transpile(ghz, backend, optimization_level=level)
938
- depths.append(circ.depth())
939
- gate_counts.append(sum(circ.count_ops().values()))
940
- non_local_gate_counts.append(circ.num_nonlocal_gates())
941
- fig, (ax1, ax2) = plt.subplots(2, 1)
942
- ax1.bar(levels, depths, label='Depth')
943
- ax1.set_xlabel("Optimization Level")
944
- ax1.set_ylabel("Depth")
945
- ax1.set_title("Output Circuit Depth")
946
- ax2.bar(levels, gate_counts, label='Number of Circuit Operations')
947
- ax2.bar(levels, non_local_gate_counts, label='Number of non-local gates')
948
- ax2.set_xlabel("Optimization Level")
949
- ax2.set_ylabel("Number of gates")
950
- ax2.legend()
951
- ax2.set_title("Number of output circuit gates")
952
- fig.tight_layout()
953
- plt.show()
954
-
955
- .. _scheduling_stage:
956
-
957
- Scheduling Stage
958
- ----------------
1183
+ .. seealso::
1184
+ :ref:`transpiler-preset-stage-scheduling`
1185
+ How to configure the scheduling stages of the preset pass managers.
959
1186
 
960
1187
  After the circuit has been translated to the target basis, mapped to the device, and optimized,
961
1188
  a scheduling phase can be applied to optionally account for all the idle time in the circuit.
@@ -1027,8 +1254,8 @@ the scheduling and adjustments/rescheduling are finished, a padding pass,
1027
1254
  such as :class:`~.PadDelay` or :class:`~.PadDynamicalDecoupling` is run
1028
1255
  to insert the instructions into the circuit, which completes the scheduling.
1029
1256
 
1030
- Scheduling Analysis with control flow instructions
1031
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1257
+ Scheduling analysis with control-flow instructions
1258
+ --------------------------------------------------
1032
1259
 
1033
1260
  When running scheduling analysis passes on a circuit, you must keep in mind that there
1034
1261
  are additional constraints on classical conditions and control flow instructions. This section
@@ -1036,7 +1263,7 @@ covers the details of these additional
1036
1263
  constraints that any scheduling pass will need to account for.
1037
1264
 
1038
1265
  Topological node ordering in scheduling
1039
- ''''''''''''''''''''''''''''''''''''''''
1266
+ .......................................
1040
1267
 
1041
1268
  The DAG representation of ``QuantumCircuit`` respects the node ordering in the
1042
1269
  classical register wires, though theoretically two conditional instructions
@@ -1072,7 +1299,7 @@ However, any additional optimization should be done in a different pass,
1072
1299
  not to break the topological ordering of the original circuit.
1073
1300
 
1074
1301
  Realistic control flow scheduling (respecting microarchitecture)
1075
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
1302
+ ................................................................
1076
1303
 
1077
1304
  In the dispersive QND readout scheme, the qubit (Q) is measured by sending
1078
1305
  a microwave stimulus, followed by a resonator ring-down (depopulation). This
@@ -1226,8 +1453,8 @@ See https://arxiv.org/abs/2102.01682 for more details.
1226
1453
  Transpiler API
1227
1454
  ==============
1228
1455
 
1229
- Transpiler Target
1230
- -----------------
1456
+ Hardware description
1457
+ --------------------
1231
1458
 
1232
1459
  .. autosummary::
1233
1460
  :toctree: ../stubs/
@@ -1235,8 +1462,8 @@ Transpiler Target
1235
1462
  Target
1236
1463
  InstructionProperties
1237
1464
 
1238
- Pass Manager Construction
1239
- -------------------------
1465
+ Pass Manager Definition
1466
+ -----------------------
1240
1467
 
1241
1468
  .. autosummary::
1242
1469
  :toctree: ../stubs/
@@ -1244,6 +1471,7 @@ Pass Manager Construction
1244
1471
  StagedPassManager
1245
1472
  PassManager
1246
1473
  PassManagerConfig
1474
+ generate_preset_pass_manager
1247
1475
 
1248
1476
  Layout and Topology
1249
1477
  -------------------
@@ -1282,6 +1510,8 @@ Exceptions
1282
1510
  .. autoexception:: CircuitTooWideForTarget
1283
1511
  .. autoexception:: InvalidLayoutError
1284
1512
 
1513
+ .. _sabre-original-paper: https://arxiv.org/abs/1809.02573
1514
+ .. _sabre-lightsabre-paper: https://arxiv.org/abs/2409.08368
1285
1515
  """
1286
1516
 
1287
1517
  # For backward compatibility