qiskit-documentation/docs/api/qiskit/0.29/qiskit.compiler.transpile.mdx

140 lines
8.9 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: transpile (v0.29)
description: API reference for qiskit.compiler.transpile in qiskit v0.29
in_page_toc_min_heading_level: 1
python_api_type: function
python_api_name: qiskit.compiler.transpile
---
<span id="qiskit-compiler-transpile" />
# qiskit.compiler.transpile
<Function id="qiskit.compiler.transpile" isDedicatedPage={true} github="https://github.com/qiskit/qiskit/tree/stable/0.18/qiskit/compiler/transpiler.py" signature="transpile(circuits, backend=None, basis_gates=None, coupling_map=None, backend_properties=None, initial_layout=None, layout_method=None, routing_method=None, translation_method=None, scheduling_method=None, instruction_durations=None, dt=None, approximation_degree=None, timing_constraints=None, seed_transpiler=None, optimization_level=None, pass_manager=None, callback=None, output_name=None)">
Transpile one or more circuits, according to some desired transpilation targets.
All arguments may be given as either a singleton or list. In case of a list, the length must be equal to the number of circuits being transpiled.
Transpilation is done in parallel using multiprocessing.
**Parameters**
* **circuits** (`Union`\[`QuantumCircuit`, `List`\[`QuantumCircuit`]]) Circuit(s) to transpile
* **backend** (`Union`\[`Backend`, `BaseBackend`, `None`])
If set, transpiler options are automatically grabbed from `backend.configuration()` and `backend.properties()`. If any other option is explicitly set (e.g., `coupling_map`), it will override the backends.
<Admonition title="Note" type="note">
The backend arg is purely for convenience. The resulting circuit may be run on any backend as long as it is compatible.
</Admonition>
* **basis\_gates** (`Optional`\[`List`\[`str`]]) List of basis gate names to unroll to (e.g: `['u1', 'u2', 'u3', 'cx']`). If `None`, do not unroll.
* **coupling\_map** (`Union`\[`CouplingMap`, `List`\[`List`\[`int`]], `None`])
Coupling map (perhaps custom) to target in mapping. Multiple formats are supported:
1. `CouplingMap` instance
2. List, must be given as an adjacency matrix, where each entry specifies all two-qubit interactions supported by backend, e.g: `[[0, 1], [0, 3], [1, 2], [1, 5], [2, 5], [4, 1], [5, 3]]`
* **backend\_properties** (`Optional`\[`BackendProperties`]) properties returned by a backend, including information on gate errors, readout errors, qubit coherence times, etc. Find a backend that provides this information with: `backend.properties()`
* **initial\_layout** (`Union`\[`Layout`, `Dict`, `List`, `None`])
Initial position of virtual qubits on physical qubits. If this layout makes the circuit compatible with the coupling\_map constraints, it will be used. The final layout is not guaranteed to be the same, as the transpiler may permute qubits through swaps or other means. Multiple formats are supported:
1. `Layout` instance
2. Dict \* virtual to physical:
```python
{qr[0]: 0,
qr[1]: 3,
qr[2]: 5}
```
* physical to virtual:
```python
{0: qr[0],
3: qr[1],
5: qr[2]}
```
3. List
* virtual to physical:
```python
[0, 3, 5] # virtual qubits are ordered (in addition to named)
```
* physical to virtual:
```python
[qr[0], None, None, qr[1], None, qr[2]]
```
* **layout\_method** (`Optional`\[`str`]) Name of layout selection pass (trivial, dense, noise\_adaptive, sabre)
* **routing\_method** (`Optional`\[`str`]) Name of routing pass (basic, lookahead, stochastic, sabre, none)
* **translation\_method** (`Optional`\[`str`]) Name of translation pass (unroller, translator, synthesis)
* **scheduling\_method** (`Optional`\[`str`]) Name of scheduling pass. \* `'as_soon_as_possible'`: Schedule instructions greedily, as early as possible on a qubit resource. (alias: `'asap'`) \* `'as_late_as_possible'`: Schedule instructions late, i.e. keeping qubits in the ground state when possible. (alias: `'alap'`) If `None`, no scheduling will be done.
* **instruction\_durations** (`Union`\[`List`\[`Tuple`\[`str`, `Optional`\[`Iterable`\[`int`]], `float`, `str`]], `List`\[`Tuple`\[`str`, `Optional`\[`Iterable`\[`int`]], `float`]], `InstructionDurations`, `None`]) Durations of instructions. The gate lengths defined in `backend.properties` are used as default. They are overwritten if this `instruction_durations` is specified. The format of `instruction_durations` must be as follows. The instruction\_durations must be given as a list of tuples \[(instruction\_name, qubits, duration, unit), …]. | \[(cx, \[0, 1], 12.3, ns), (u3, \[0], 4.56, ns)] | \[(cx, \[0, 1], 1000), (u3, \[0], 300)] If unit is omitted, the default is dt, which is a sample time depending on backend. If the time unit is dt, the duration must be an integer.
* **dt** (`Optional`\[`float`]) Backend sample time (resolution) in seconds. If `None` (default), `backend.configuration().dt` is used.
* **approximation\_degree** (*float*) heuristic dial used for circuit approximation (1.0=no approximation, 0.0=maximal approximation)
* **timing\_constraints** (`Optional`\[`Dict`\[`str`, `int`]])
An optional control hardware restriction on instruction time resolution. A quantum computer backend may report a set of restrictions, namely:
* granularity: An integer value representing minimum pulse gate resolution in units of `dt`. A user-defined pulse gate should have duration of a multiple of this granularity value.
* min\_length: An integer value representing minimum pulse gate length in units of `dt`. A user-defined pulse gate should be longer than this length.
* pulse\_alignment: An integer value representing a time resolution of gate instruction starting time. Gate instruction should start at time which is a multiple of the alignment value.
* acquire\_alignment: An integer value representing a time resolution of measure instruction starting time. Measure instruction should start at time which is a multiple of the alignment value.
This information will be provided by the backend configuration. If the backend doesnt have any restriction on the instruction time allocation, then `timing_constraints` is None and no adjustment will be performed.
* **seed\_transpiler** (`Optional`\[`int`]) Sets random seed for the stochastic parts of the transpiler
* **optimization\_level** (`Optional`\[`int`]) How much optimization to perform on the circuits. Higher levels generate more optimized circuits, at the expense of longer transpilation time. \* 0: no optimization \* 1: light optimization \* 2: heavy optimization \* 3: even heavier optimization If `None`, level 1 will be chosen as default.
* **pass\_manager** (`Optional`\[`PassManager`]) The pass manager to use for a custom pipeline of transpiler passes. If this arg is present, all other args will be ignored and the pass manager will be used directly (Qiskit will not attempt to auto-select a pass manager based on transpile options).
* **callback** (`Optional`\[`Callable`\[\[`BasePass`, `DAGCircuit`, `float`, `PropertySet`, `int`], `Any`]])
A callback function that will be called after each pass execution. The function will be called with 5 keyword arguments, | `pass_`: the pass being run. | `dag`: the dag output of the pass. | `time`: the time to execute the pass. | `property_set`: the property set. | `count`: the index for the pass execution. The exact arguments passed expose the internals of the pass manager, and are subject to change as the pass manager internals change. If you intend to reuse a callback function over multiple releases, be sure to check that the arguments being passed are the same. To use the callback feature, define a function that will take in kwargs dict and access the variables. For example:
```python
def callback_func(**kwargs):
pass_ = kwargs['pass_']
dag = kwargs['dag']
time = kwargs['time']
property_set = kwargs['property_set']
count = kwargs['count']
...
transpile(circ, callback=callback_func)
```
* **output\_name** (`Union`\[`str`, `List`\[`str`], `None`]) A list with strings to identify the output circuits. The length of the list should be exactly the length of the `circuits` parameter.
**Return type**
`Union`\[`QuantumCircuit`, `List`\[`QuantumCircuit`]]
**Returns**
The transpiled circuit(s).
**Raises**
[**TranspilerError**](qiskit.transpiler.TranspilerError "qiskit.transpiler.TranspilerError") in case of bad inputs to transpiler (like conflicting parameters) or errors in passes
</Function>