qiskit-documentation/docs/api/qiskit/qiskit.transpiler.passes.Sa...

204 lines
10 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: SabreLayout
description: API reference for qiskit.transpiler.passes.SabreLayout
in_page_toc_min_heading_level: 1
python_api_type: class
python_api_name: qiskit.transpiler.passes.SabreLayout
---
# SabreLayout
<Class id="qiskit.transpiler.passes.SabreLayout" isDedicatedPage={true} github="https://github.com/Qiskit/qiskit/tree/stable/1.1/qiskit/transpiler/passes/layout/sabre_layout.py#L51-L461" signature="qiskit.transpiler.passes.SabreLayout(*args, **kwargs)" modifiers="class">
Bases: [`TransformationPass`](qiskit.transpiler.TransformationPass "qiskit.transpiler.basepasses.TransformationPass")
Choose a Layout via iterative bidirectional routing of the input circuit.
Starting with a random initial Layout, the algorithm does a full routing of the circuit (via the routing\_pass method) to end up with a final\_layout. This final\_layout is then used as the initial\_layout for routing the reverse circuit. The algorithm iterates a number of times until it finds an initial\_layout that reduces full routing cost.
This method exploits the reversibility of quantum circuits, and tries to include global circuit information in the choice of initial\_layout.
By default, this pass will run both layout and routing and will transform the circuit so that the layout is applied to the input dag (meaning that the output circuit will have ancilla qubits allocated for unused qubits on the coupling map and the qubits will be reordered to match the mapped physical qubits) and then routing will be applied (inserting `AnalysisPass` objects and just find an initial layout and set that on the property set. This is done because by default the pass will run parallel seed trials with different random seeds for selecting the random initial layout and then selecting the routed output which results in the least number of swap gates needed.
You can use the `routing_pass` argument to have this pass operate as a typical layout pass. When specified this will use the specified routing pass to select an initial layout only and will not run multiple seed trials.
In addition to starting with a random initial Layout the pass can also take in an additional list of starting layouts which will be used for additional trials. If the `sabre_starting_layouts` is present in the property set when this pass is run, that will be used for additional trials. There will still be `layout_trials` of full random starting layouts run and the contents of `sabre_starting_layouts` will be run in addition to those. The output which results in the lowest amount of swap gates (whether from the random trials or the property set starting point) will be used. The value for this property set field should be a list of [`Layout`](qiskit.transpiler.Layout "qiskit.transpiler.Layout") objects representing the starting layouts to use. If a virtual qubit is missing from an [`Layout`](qiskit.transpiler.Layout "qiskit.transpiler.Layout") object in the list a random qubit will be selected.
## Property Set Fields Read
**`sabre_starting_layouts` (`list[Layout]`)**
An optional list of [`Layout`](qiskit.transpiler.Layout "qiskit.transpiler.Layout") objects to use for additional layout trials. This is in addition to the full random trials specified with the `layout_trials` argument.
## Property Set Values Written
**`layout` ([`Layout`](qiskit.transpiler.Layout "qiskit.transpiler.Layout"))**
The chosen initial mapping of virtual to physical qubits, including the ancilla allocation.
**`final_layout` ([`Layout`](qiskit.transpiler.Layout "qiskit.transpiler.Layout"))**
A permutation of how swaps have been applied to the input qubits at the end of the circuit.
**References:**
\[1] Li, Gushu, Yufei Ding, and Yuan Xie. “Tackling the qubit mapping problem for NISQ-era quantum devices.” ASPLOS 2019. [arXiv:1809.02573](https://arxiv.org/pdf/1809.02573.pdf)
SabreLayout initializer.
**param coupling\_map**
directed graph representing a coupling map.
**type coupling\_map**
Union\[CouplingMap, Target]
**param routing\_pass**
the routing pass to use while iterating. If specified this pass operates as an [`AnalysisPass`](qiskit.transpiler.AnalysisPass "qiskit.transpiler.AnalysisPass") and will only populate the `layout` field in the property set and the input dag is returned unmodified. This argument is mutually exclusive with the `swap_trials` and the `layout_trials` arguments and if this is specified at the same time as either argument an error will be raised.
**type routing\_pass**
BasePass
**param seed**
seed for setting a random first trial layout.
**type seed**
int
**param max\_iterations**
number of forward-backward iterations.
**type max\_iterations**
int
**param swap\_trials**
The number of trials to run of [`SabreSwap`](qiskit.transpiler.passes.SabreSwap "qiskit.transpiler.passes.SabreSwap") for each iteration. This is equivalent to the `trials` argument on [`SabreSwap`](qiskit.transpiler.passes.SabreSwap "qiskit.transpiler.passes.SabreSwap"). If this is not specified (and `routing_pass` isnt set) by default the number of physical CPUs on your local system will be used. For reproducibility between environments it is best to set this to an explicit number because the output will potentially depend on the number of trials run. This option is mutually exclusive with the `routing_pass` argument and an error will be raised if both are used.
**type swap\_trials**
int
**param layout\_trials**
The number of random seed trials to run layout with. When > 1 the trial that resuls in the output with the fewest swap gates will be selected. If this is not specified (and `routing_pass` is not set) then the number of local physical CPUs will be used as the default value. This option is mutually exclusive with the `routing_pass` argument and an error will be raised if both are used.
**type layout\_trials**
int
**param skip\_routing**
If this is set `True` and `routing_pass` is not used then routing will not be applied to the output circuit. Only the layout will be set in the property set. This is a tradeoff to run custom routing with multiple layout trials, as using this option will cause SabreLayout to run the routing stage internally but not use that result.
**type skip\_routing**
bool
**raises TranspilerError**
If both `routing_pass` and `swap_trials` or
**raises both `routing_pass` and `layout_trials` are specified**
## Attributes
### is\_analysis\_pass
<Attribute id="qiskit.transpiler.passes.SabreLayout.is_analysis_pass">
Check if the pass is an analysis pass.
If the pass is an AnalysisPass, that means that the pass can analyze the DAG and write the results of that analysis in the property set. Modifications on the DAG are not allowed by this kind of pass.
</Attribute>
### is\_transformation\_pass
<Attribute id="qiskit.transpiler.passes.SabreLayout.is_transformation_pass">
Check if the pass is a transformation pass.
If the pass is a TransformationPass, that means that the pass can manipulate the DAG, but cannot modify the property set (but it can be read).
</Attribute>
## Methods
### execute
<Function id="qiskit.transpiler.passes.SabreLayout.execute" github="https://github.com/Qiskit/qiskit/tree/stable/1.1/qiskit/transpiler/basepasses.py#L189-L211" signature="execute(passmanager_ir, state, callback=None)">
Execute optimization task for input Qiskit IR.
**Parameters**
* **passmanager\_ir** ([*Any*](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.12)")) Qiskit IR to optimize.
* **state** ([*PassManagerState*](qiskit.passmanager.PassManagerState "qiskit.passmanager.compilation_status.PassManagerState")) State associated with workflow execution by the pass manager itself.
* **callback** ([*Callable*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Callable "(in Python v3.12)") *| None*) A callback function which is caller per execution of optimization task.
**Returns**
Optimized Qiskit IR and state of the workflow.
**Return type**
[tuple](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.12)")\[[*Any*](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.12)"), [qiskit.passmanager.compilation\_status.PassManagerState](qiskit.passmanager.PassManagerState "qiskit.passmanager.compilation_status.PassManagerState")]
</Function>
### name
<Function id="qiskit.transpiler.passes.SabreLayout.name" github="https://github.com/Qiskit/qiskit/tree/stable/1.1/qiskit/passmanager/base_tasks.py#L68-L70" signature="name()">
Name of the pass.
**Return type**
[str](https://docs.python.org/3/library/stdtypes.html#str "(in Python v3.12)")
</Function>
### run
<Function id="qiskit.transpiler.passes.SabreLayout.run" github="https://github.com/Qiskit/qiskit/tree/stable/1.1/qiskit/transpiler/passes/layout/sabre_layout.py#L196-L364" signature="run(dag)">
Run the SabreLayout pass on dag.
**Parameters**
**dag** ([*DAGCircuit*](qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")) DAG to find layout for.
**Returns**
**The output dag if swap mapping was run**
(otherwise the input dag is returned unmodified).
**Return type**
[DAGCircuit](qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")
**Raises**
[**TranspilerError**](transpiler#qiskit.transpiler.TranspilerError "qiskit.transpiler.TranspilerError") if dag wider than self.coupling\_map
</Function>
### update\_status
<Function id="qiskit.transpiler.passes.SabreLayout.update_status" github="https://github.com/Qiskit/qiskit/tree/stable/1.1/qiskit/transpiler/basepasses.py#L213-L221" signature="update_status(state, run_state)">
Update workflow status.
**Parameters**
* **state** ([*PassManagerState*](qiskit.passmanager.PassManagerState "qiskit.passmanager.compilation_status.PassManagerState")) Pass manager state to update.
* **run\_state** (*RunState*) Completion status of current task.
**Returns**
Updated pass manager state.
**Return type**
[*PassManagerState*](qiskit.passmanager.PassManagerState "qiskit.passmanager.compilation_status.PassManagerState")
</Function>
</Class>