def _convert_unit(self, duration: float, from_unit: str, to_unit: str) -> Union[float, int]:
        if from_unit.endswith('s') and from_unit != 's':
            duration = apply_prefix(duration, from_unit)
            from_unit = 's'

        # assert both from_unit and to_unit in {'s', 'dt'}
        if from_unit == to_unit:
            return duration

        if self.dt is None:
            raise TranspilerError("dt is necessary to convert durations from '{}' to '{}'"
                                  .format(from_unit, to_unit))
        if from_unit == 's' and to_unit == 'dt':
            return duration_in_dt(duration, self.dt)
        elif from_unit == 'dt' and to_unit == 's':
            return duration * self.dt
        else:
            raise TranspilerError("Conversion from '{}' to '{}' is not supported"
                                  .format(from_unit, to_unit))
Esempio n. 2
0
    def _convert_unit(self, duration: float, from_unit: str, to_unit: str) -> float:
        if from_unit.endswith("s") and from_unit != "s":
            duration = apply_prefix(duration, from_unit)
            from_unit = "s"

        # assert both from_unit and to_unit in {'s', 'dt'}
        if from_unit == to_unit:
            return duration

        if self.dt is None:
            raise TranspilerError(
                f"dt is necessary to convert durations from '{from_unit}' to '{to_unit}'"
            )
        if from_unit == "s" and to_unit == "dt":
            if isinstance(duration, ParameterExpression):
                return duration / self.dt
            return duration_in_dt(duration, self.dt)
        elif from_unit == "dt" and to_unit == "s":
            return duration * self.dt
        else:
            raise TranspilerError(f"Conversion from '{from_unit}' to '{to_unit}' is not supported")
    def _deprecate_id_instruction(
        self, circuits: Union[QasmQobj, PulseQobj, QuantumCircuit, Schedule,
                              List[Union[QuantumCircuit, Schedule]]]
    ) -> None:
        """Raise a DeprecationWarning if any circuit contains an 'id' instruction.

        Additionally, if 'delay' is a 'supported_instruction', replace each 'id'
        instruction (in-place) with the equivalent ('sx'-length) 'delay' instruction.

        Args:
            circuits: The individual or list of :class:`~qiskit.circuits.QuantumCircuit` or
                :class:`~qiskit.pulse.Schedule` objects passed to
                :meth:`IBMQBackend.run()<IBMQBackend.run>`. Modified in-place.

        Returns:
            None
        """

        if isinstance(circuits, PulseQobj):
            return

        id_support = 'id' in getattr(self.configuration(), 'basis_gates', [])
        delay_support = 'delay' in getattr(self.configuration(),
                                           'supported_instructions', [])

        if not delay_support:
            return

        if isinstance(circuits, QasmQobj):
            circuit_has_id = any(instr.name == 'id'
                                 for experiment in circuits.experiments
                                 for instr in experiment.instructions)
        else:
            if not isinstance(circuits, List):
                circuits = [circuits]

            circuit_has_id = any(instr.name == 'id' for circuit in circuits
                                 if isinstance(circuit, QuantumCircuit)
                                 for instr, qargs, cargs in circuit.data)

        if not circuit_has_id:
            return

        if not self.id_warning_issued:
            if id_support and delay_support:
                warnings.warn(
                    "Support for the 'id' instruction has been deprecated "
                    "from IBM hardware backends. Any 'id' instructions "
                    "will be replaced with their equivalent 'delay' instruction. "
                    "Please use the 'delay' instruction instead.",
                    DeprecationWarning,
                    stacklevel=4)
            else:
                warnings.warn(
                    "Support for the 'id' instruction has been removed "
                    "from IBM hardware backends. Any 'id' instructions "
                    "will be replaced with their equivalent 'delay' instruction. "
                    "Please use the 'delay' instruction instead.",
                    DeprecationWarning,
                    stacklevel=4)

            self.id_warning_issued = True

        dt_in_s = self.configuration().dt

        if isinstance(circuits, QasmQobj):
            for experiment in circuits.experiments:
                for instr in experiment.instructions:
                    if instr.name == 'id':
                        sx_duration = self.properties().gate_length(
                            'sx', instr.qubits[0])
                        sx_duration_in_dt = duration_in_dt(
                            sx_duration, dt_in_s)

                        instr.name = 'delay'
                        instr.params = [sx_duration_in_dt]
        else:
            for circuit in circuits:
                if isinstance(circuit, Schedule):
                    continue

                for idx, (instr, qargs, cargs) in enumerate(circuit.data):
                    if instr.name == 'id':

                        sx_duration = self.properties().gate_length(
                            'sx', qargs[0].index)
                        sx_duration_in_dt = duration_in_dt(
                            sx_duration, dt_in_s)

                        delay_instr = Delay(sx_duration_in_dt)

                        circuit.data[idx] = (delay_instr, qargs, cargs)
Esempio n. 4
0
    def _deprecate_id_instruction(
        self,
        circuits: Union[QuantumCircuit, Schedule, List[Union[QuantumCircuit,
                                                             Schedule]]],
    ) -> Union[QuantumCircuit, Schedule, List[Union[QuantumCircuit,
                                                    Schedule]]]:
        """Raise a DeprecationWarning if any circuit contains an 'id' instruction.

        Additionally, if 'delay' is a 'supported_instruction', replace each 'id'
        instruction (in-place) with the equivalent ('sx'-length) 'delay' instruction.

        Args:
            circuits: The individual or list of :class:`~qiskit.circuits.QuantumCircuit` or
                :class:`~qiskit.pulse.Schedule` objects passed to
                :meth:`IBMBackend.run()<IBMBackend.run>`. Modified in-place.

        Returns:
            A modified copy of the original circuit where 'id' instructions are replaced with
            'delay' instructions. A copy is used so the original circuit is not modified.
            If there are no 'id' instructions or 'delay' is not supported, return the original circuit.
        """

        id_support = "id" in getattr(self.configuration(), "basis_gates", [])
        delay_support = "delay" in getattr(self.configuration(),
                                           "supported_instructions", [])

        if not delay_support:
            return circuits

        if not isinstance(circuits, List):
            circuits = [circuits]

        circuit_has_id = any(instr.name == "id" for circuit in circuits
                             if isinstance(circuit, QuantumCircuit)
                             for instr, qargs, cargs in circuit.data)

        if not circuit_has_id:
            return circuits

        if not self.id_warning_issued:
            if id_support and delay_support:
                warnings.warn(
                    "Support for the 'id' instruction has been deprecated "
                    "from IBM hardware backends. Any 'id' instructions "
                    "will be replaced with their equivalent 'delay' instruction. "
                    "Please use the 'delay' instruction instead.",
                    DeprecationWarning,
                    stacklevel=4,
                )
            else:
                warnings.warn(
                    "Support for the 'id' instruction has been removed "
                    "from IBM hardware backends. Any 'id' instructions "
                    "will be replaced with their equivalent 'delay' instruction. "
                    "Please use the 'delay' instruction instead.",
                    DeprecationWarning,
                    stacklevel=4,
                )

            self.id_warning_issued = True

        dt_in_s = self.configuration().dt

        circuits_copy = copy.deepcopy(circuits)
        for circuit in circuits_copy:
            if isinstance(circuit, Schedule):
                continue

            for idx, (instr, qargs, cargs) in enumerate(circuit.data):
                if instr.name == "id":

                    sx_duration = self.properties().gate_length(
                        "sx", qargs[0].index)
                    sx_duration_in_dt = duration_in_dt(sx_duration, dt_in_s)

                    delay_instr = Delay(sx_duration_in_dt)

                    circuit.data[idx] = (delay_instr, qargs, cargs)
        return circuits_copy