qiskit.compiler.transpile
transpile(circuits, backend=None, basis_gates=None, inst_map=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=1.0, timing_constraints=None, seed_transpiler=None, optimization_level=None, callback=None, output_name=None, unitary_synthesis_method='default', unitary_synthesis_plugin_config=None, target=None, hls_config=None, init_method=None, optimization_method=None, ignore_backend_supplied_default_methods=False)
Transpile one or more circuits, according to some desired transpilation targets.
Previously, all arguments accepted lists of the same length as circuits
, which was used to specialize arguments for circuits at the corresponding indices. Support for using such argument lists is now deprecated and will be removed in the 0.25.0 release. If you need to use multiple values for an argument, you can use multiple transpile()
calls (and potentially parallel_map()
to leverage multiprocessing if needed).
Transpilation is done in parallel using multiprocessing.
Parameters
-
circuits (
Union
[QuantumCircuit
,List
[QuantumCircuit
]]) – Circuit(s) to transpile -
backend (
Optional
[Backend
]) – If set, the transpiler will compile the input circuit to this target device. If any other option is explicitly set (e.g.,coupling_map
), it will override the backend’s. -
basis_gates (
Optional
[List
[str
]]) – List of basis gate names to unroll to (e.g:['u1', 'u2', 'u3', 'cx']
). IfNone
, do not unroll. -
inst_map (
Optional
[List
[InstructionScheduleMap
]]) – Mapping of unrolled gates to pulse schedules. If this is not provided, transpiler tries to get from the backend. If any user defined calibration is found in the map and this is used in a circuit, transpiler attaches the custom gate definition to the circuit. This enables one to flexibly override the low-level instruction implementation. This feature is available iff the backend supports the pulse gate experiment. -
coupling_map (
Union
[CouplingMap
,List
[List
[int
]],None
]) –Directed coupling map (perhaps custom) to target in mapping. If the coupling map is symmetric, both directions need to be specified.
Multiple formats are supported:
CouplingMap
instance- List, must be given as an adjacency matrix, where each entry specifies all directed 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:
-
Layout
instance -
Dict * virtual to physical:
{qr[0]: 0, qr[1]: 3, qr[2]: 5}
-
physical to virtual:
{0: qr[0], 3: qr[1], 5: qr[2]}
-
-
List
-
virtual to physical:
[0, 3, 5] # virtual qubits are ordered (in addition to named)
-
physical to virtual:
[qr[0], None, None, qr[1], None, qr[2]]
-
-
-
layout_method (
Optional
[str
]) – Name of layout selection pass (‘trivial’, ‘dense’, ‘noise_adaptive’, ‘sabre’). This can also be the external plugin name to use for thelayout
stage. You can see a list of installed plugins by usinglist_stage_plugins()
with"layout"
for thestage_name
argument. -
routing_method (
Optional
[str
]) – Name of routing pass (‘basic’, ‘lookahead’, ‘stochastic’, ‘sabre’, ‘none’). Note This can also be the external plugin name to use for therouting
stage. You can see a list of installed plugins by usinglist_stage_plugins()
with"routing"
for thestage_name
argument. -
translation_method (
Optional
[str
]) – Name of translation pass (‘unroller’, ‘translator’, ‘synthesis’) This can also be the external plugin name to use for thetranslation
stage. You can see a list of installed plugins by usinglist_stage_plugins()
with"translation"
for thestage_name
argument. -
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'
) IfNone
, no scheduling will be done. This can also be the external plugin name to use for thescheduling
stage. You can see a list of installed plugins by usinglist_stage_plugins()
with"scheduling"
for thestage_name
argument. -
instruction_durations (
Union
[List
[Tuple
[str
,Optional
[Iterable
[int
]],float
,Optional
[Iterable
[float
]],str
]],List
[Tuple
[str
,Optional
[Iterable
[int
]],float
,Optional
[Iterable
[float
]]]],List
[Tuple
[str
,Optional
[Iterable
[int
]],float
,str
]],List
[Tuple
[str
,Optional
[Iterable
[int
]],float
]],InstructionDurations
,None
]) – Durations of instructions. Applicable only if scheduling_method is specified. The gate lengths defined inbackend.properties
are used as default. They are overwritten if thisinstruction_durations
is specified. The format ofinstruction_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. IfNone
(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 doesn’t have any restriction on the instruction time allocation, then
timing_constraints
is None and no adjustment will be performed. - granularity: An integer value representing minimum pulse gate resolution in units of
-
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 IfNone
, level 1 will be chosen as default. -
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: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 thecircuits
parameter. -
unitary_synthesis_method (str) – The name of the unitary synthesis method to use. By default ‘default’ is used, which is the only method included with qiskit. If you have installed any unitary synthesis plugins you can use the name exported by the plugin.
-
unitary_synthesis_plugin_config (
Optional
[dict
]) – An optional configuration dictionary that will be passed directly to the unitary synthesis plugin. By default this setting will have no effect as the default unitary synthesis method does not take custom configuration. This should only be necessary when a unitary synthesis plugin is specified with theunitary_synthesis
argument. As this is custom for each unitary synthesis plugin refer to the plugin documentation for how to use this option. -
target (
Optional
[Target
]) – A backend transpiler target. Normally this is specified as part of thebackend
argument, but if you have manually constructed aTarget
object you can specify it manually here. This will override the target frombackend
. -
hls_config (
Optional
[HLSConfig
]) – An optional configuration classHLSConfig
that will be passed directly toHighLevelSynthesis
transformation pass. This configuration class allows to specify for various high-level objects the lists of synthesis algorithms and their parameters. -
init_method (
Optional
[str
]) – The plugin name to use for theinit
stage. By default an external plugin is not used. You can see a list of installed plugins by usinglist_stage_plugins()
with"init"
for the stage name argument. -
optimization_method (
Optional
[str
]) – The plugin name to use for theoptimization
stage. By default an external plugin is not used. You can see a list of installed plugins by usinglist_stage_plugins()
with"optimization"
for thestage_name
argument. -
ignore_backend_supplied_default_methods (
bool
) – If set toTrue
any default methods specified by a backend will be ignored. Some backends specify alternative default methods to support custom compilation target-specific passes/plugins which support backend-specific compilation techniques. If you’d prefer that these defaults were not used this option is used to disable those backend-specific defaults.
Return type
Union
[QuantumCircuit
, List
[QuantumCircuit
]]
Returns
The transpiled circuit(s).
Raises
TranspilerError – in case of bad inputs to transpiler (like conflicting parameters) or errors in passes