Skip to main contentIBM Quantum Documentation
This page is from the dev version of Qiskit SDK. Go to the stable version.

Compilation Routines

qiskit.compiler


Circuit Compilation Functions

transpile

qiskit.compiler.transpile(circuits, backend=None, basis_gates=None, coupling_map=None, initial_layout=None, layout_method=None, routing_method=None, translation_method=None, scheduling_method=None, dt=None, approximation_degree=1.0, 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, num_processes=None, qubits_initially_zero=True)

GitHub

Transpile one or more circuits, according to some desired transpilation targets.

Transpilation is potentially done in parallel using multiprocessing when circuits is a list with > 1 QuantumCircuit object, depending on the local environment and configuration.

The prioritization of transpilation target constraints works as follows: if a target input is provided, it will take priority over any backend input or loose constraints (basis_gates, coupling_map, or dt). If a backend is provided together with any loose constraint from the list above, the loose constraint will take priority over the corresponding backend constraint. This behavior is summarized in the table below. The first column in the table summarizes the potential user-provided constraints, and each cell shows whether the priority is assigned to that specific constraint input or another input (target/backend(V2)).

User Providedtargetbackend(V2)
basis_gatestargetbasis_gates
coupling_maptargetcoupling_map
dttargetdt

Parameters

  • circuits (_CircuitT) – Circuit(s) to transpile

  • backend (Backend | None) – 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 (List[str] | None) – List of basis gate names to unroll to (e.g: ['u1', 'u2', 'u3', 'cx']). If None, do not unroll.

  • coupling_map (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:

    1. CouplingMap instance
    2. 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]]
  • initial_layout (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:

      {qr[0]: 0,
       qr[1]: 3,
       qr[2]: 5}
      • physical to virtual:

        {0: qr[0],
         3: qr[1],
         5: qr[2]}
    3. 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 (str | None) – Name of layout selection pass (‘trivial’, ‘dense’, ‘sabre’). This can also be the external plugin name to use for the layout stage. You can see a list of installed plugins by using list_stage_plugins() with "layout" for the stage_name argument.

  • routing_method (str | None) – Name of routing pass (‘basic’, ‘lookahead’, ‘stochastic’, ‘sabre’, ‘none’). Note This can also be the external plugin name to use for the routing stage. You can see a list of installed plugins by using list_stage_plugins() with "routing" for the stage_name argument.

  • translation_method (str | None) – Name of translation pass ("default", "translator" or "synthesis"). This can also be the external plugin name to use for the translation stage. You can see a list of installed plugins by using list_stage_plugins() with "translation" for the stage_name argument.

  • scheduling_method (str | None) – 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. This can also be the external plugin name to use for the scheduling stage. You can see a list of installed plugins by using list_stage_plugins() with "scheduling" for the stage_name argument.

  • dt (float | None) – Backend sample time (resolution) in seconds. If None (default), backend.dt is used.

  • approximation_degree (float) – heuristic dial used for circuit approximation (1.0=no approximation, 0.0=maximal approximation)

  • seed_transpiler (int | None) – Sets random seed for the stochastic parts of the transpiler

  • optimization_level (int | None) –

    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 2 will be chosen as default.

  • callback (Callable[[BasePass, DAGCircuit, float, PropertySet, int], Any] | None) –

    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 (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.

  • unitary_synthesis_method (str) – The name of the unitary synthesis method to use. By default 'default' is used. You can see a list of installed plugins with unitary_synthesis_plugin_names().

  • unitary_synthesis_plugin_config (dict | None) – 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 the unitary_synthesis_method argument. As this is custom for each unitary synthesis plugin refer to the plugin documentation for how to use this option.

  • target (Target | None) – A backend transpiler target. Normally this is specified as part of the backend argument, but if you have manually constructed a Target object you can specify it manually here. This will override the target from backend.

  • hls_config (HLSConfig | None) – An optional configuration class HLSConfig that will be passed directly to HighLevelSynthesis transformation pass. This configuration class allows to specify for various high-level objects the lists of synthesis algorithms and their parameters.

  • init_method (str | None) – The plugin name to use for the init stage. By default an external plugin is not used. You can see a list of installed plugins by using list_stage_plugins() with "init" for the stage name argument.

  • optimization_method (str | None) – The plugin name to use for the optimization stage. By default an external plugin is not used. You can see a list of installed plugins by using list_stage_plugins() with "optimization" for the stage_name argument.

  • ignore_backend_supplied_default_methods (bool) – If set to True 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.

  • num_processes (int | None) – The maximum number of parallel processes to launch for this call to transpile if parallel execution is enabled. This argument overrides num_processes in the user configuration file, and the QISKIT_NUM_PROCS environment variable. If set to None the system default or local user configuration will be used.

  • qubits_initially_zero (bool) – Indicates whether the input circuit is zero-initialized.

Returns

The transpiled circuit(s).

Raises

TranspilerError – in case of bad inputs to transpiler (like conflicting parameters) or errors in passes

Return type

_CircuitT

Was this page helpful?
Report a bug or request content on GitHub.