|
| 1 | +# This code is part of Qiskit. |
| 2 | +# |
| 3 | +# (C) Copyright IBM 2021. |
| 4 | +# |
| 5 | +# This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +# obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +# |
| 9 | +# Any modifications or derivative works of this code must retain this |
| 10 | +# copyright notice, and modified files need to carry a notice indicating |
| 11 | +# that they have been altered from the originals. |
| 12 | +""" |
| 13 | +Functions for preparing circuits for execution |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from collections.abc import Sequence |
| 19 | + |
| 20 | +from qiskit import QuantumCircuit, QuantumRegister |
| 21 | +from qiskit.exceptions import QiskitError |
| 22 | +from qiskit.providers import Backend |
| 23 | +from qiskit.pulse.calibration_entries import CalibrationPublisher |
| 24 | +from qiskit.transpiler import Target |
| 25 | + |
| 26 | + |
| 27 | +def map_qubits( |
| 28 | + circuit: QuantumCircuit, |
| 29 | + physical_qubits: Sequence[int], |
| 30 | + n_qubits: int | None = None, |
| 31 | +) -> QuantumCircuit: |
| 32 | + """Generate a new version of a circuit with new qubit indices |
| 33 | +
|
| 34 | + This function iterates through the instructions of ``circuit`` and copies |
| 35 | + them into a new circuit with qubit indices replaced according to the |
| 36 | + entries in ``physical_qubits``. So qubit 0's instructions are applied to |
| 37 | + ``physical_qubits[0]`` and qubit 1's to ``physical_qubits[1]``, etc. |
| 38 | +
|
| 39 | + This function behaves similarly to passing ``initial_layout`` to |
| 40 | + :func:`qiskit.transpile` but does not use a Qiskit |
| 41 | + :class:`~qiskit.transpiler.PassManager` and does not fill the circuit with |
| 42 | + ancillas. |
| 43 | +
|
| 44 | + Args: |
| 45 | + circuit: The :class:`~qiskit.QuantumCircuit` to re-index. |
| 46 | + physical_qubits: The list of new indices for ``circuit``'s qubit indices. |
| 47 | + n_qubits: Optional qubit size to use for the output circuit. If |
| 48 | + ``None``, then the maximum of ``physical_qubits`` will be used. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + The quantum circuit with new qubit indices |
| 52 | + """ |
| 53 | + if len(physical_qubits) != circuit.num_qubits: |
| 54 | + raise QiskitError( |
| 55 | + f"Circuit to map has {circuit.num_qubits} qubits, but " |
| 56 | + f"{len(physical_qubits)} physical qubits specified for mapping." |
| 57 | + ) |
| 58 | + |
| 59 | + # if all(p == r for p, r in zip(physical_qubits, range(circuit.num_qubits))): |
| 60 | + # # No mapping necessary |
| 61 | + # return circuit |
| 62 | + |
| 63 | + circ_size = n_qubits if n_qubits is not None else (max(physical_qubits) + 1) |
| 64 | + p_qregs = QuantumRegister(circ_size) |
| 65 | + p_circ = QuantumCircuit( |
| 66 | + p_qregs, |
| 67 | + *circuit.cregs, |
| 68 | + name=circuit.name, |
| 69 | + metadata=circuit.metadata, |
| 70 | + global_phase=circuit.global_phase, |
| 71 | + ) |
| 72 | + p_circ.compose( |
| 73 | + circuit, |
| 74 | + qubits=physical_qubits, |
| 75 | + inplace=True, |
| 76 | + copy=False, |
| 77 | + ) |
| 78 | + return p_circ |
| 79 | + |
| 80 | + |
| 81 | +def _has_calibration(target: Target, name: str, qubits: tuple[int, ...]) -> bool: |
| 82 | + """Wrapper to work around bug in Target.has_calibration""" |
| 83 | + try: |
| 84 | + has_cal = target.has_calibration(name, qubits) |
| 85 | + except AttributeError: |
| 86 | + has_cal = False |
| 87 | + |
| 88 | + return has_cal |
| 89 | + |
| 90 | + |
| 91 | +def check_transpilation_needed( |
| 92 | + circuits: Sequence[QuantumCircuit], |
| 93 | + backend: Backend, |
| 94 | +) -> bool: |
| 95 | + """Test if circuits are already compatible with backend |
| 96 | +
|
| 97 | + This function checks if circuits are able to be executed on ``backend`` |
| 98 | + without transpilation. It loops through the circuits to check if any gate |
| 99 | + instructions are not included in the backend's |
| 100 | + :class:`~qiskit.transpiler.Target`. The :class:`~qiskit.transpiler.Target` |
| 101 | + is also checked for custom pulse gate calibrations for circuit's |
| 102 | + instructions. If all gates are included in the target and there are no |
| 103 | + custom calibrations, the function returns ``False`` indicating that |
| 104 | + transpilation is not needed. |
| 105 | +
|
| 106 | + This function returns ``True`` if the version of ``backend`` is less than |
| 107 | + 2. |
| 108 | +
|
| 109 | + The motivation for this function is that when no transpilation is necessary |
| 110 | + it is faster to check the circuits in this way than to run |
| 111 | + :func:`~qiskit.transpile` and have it do nothing. |
| 112 | +
|
| 113 | + Args: |
| 114 | + circuits: The circuits to prepare for the backend. |
| 115 | + backend: The backend for which the circuits should be prepared. |
| 116 | +
|
| 117 | + Returns: |
| 118 | + ``True`` if transpilation is needed. Otherwise, ``False``. |
| 119 | + """ |
| 120 | + transpilation_needed = False |
| 121 | + |
| 122 | + if getattr(backend, "version", 0) <= 1: |
| 123 | + # Fall back to transpilation for BackendV1 |
| 124 | + return True |
| 125 | + |
| 126 | + target = backend.target |
| 127 | + |
| 128 | + for circ in circuits: |
| 129 | + for inst in circ.data: |
| 130 | + if inst.operation.name == "barrier": |
| 131 | + continue |
| 132 | + qubits = tuple(circ.find_bit(q).index for q in inst.qubits) |
| 133 | + if not target.instruction_supported(inst.operation.name, qubits): |
| 134 | + transpilation_needed = True |
| 135 | + break |
| 136 | + if not circ.has_calibration_for(inst) and _has_calibration( |
| 137 | + target, inst.operation.name, qubits |
| 138 | + ): |
| 139 | + cal = target.get_calibration(inst.operation.name, qubits, *inst.operation.params) |
| 140 | + if ( |
| 141 | + cal.metadata.get("publisher", CalibrationPublisher.QISKIT) |
| 142 | + != CalibrationPublisher.BACKEND_PROVIDER |
| 143 | + ): |
| 144 | + transpilation_needed = True |
| 145 | + break |
| 146 | + if transpilation_needed: |
| 147 | + break |
| 148 | + |
| 149 | + return transpilation_needed |
0 commit comments