qiskit.transpiler.instruction_durations의 소스 코드

# This code is part of Qiskit.
# (C) Copyright IBM 2020.
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Durations of instructions, one of transpiler configurations."""
from typing import Optional, List, Tuple, Union, Iterable, Set

from qiskit.circuit import Barrier, Delay
from qiskit.circuit import Instruction, Qubit
from qiskit.providers import BaseBackend
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.util import apply_prefix
from qiskit.circuit.duration import duration_in_dt

[문서]class InstructionDurations: """Helper class to provide durations of instructions for scheduling. It stores durations (gate lengths) and dt to be used at the scheduling stage of transpiling. It can be constructed from ``backend`` or ``instruction_durations``, which is an argument of :func:`transpile`. """
[문서] def __init__(self, instruction_durations: Optional['InstructionDurationsType'] = None, dt: float = None): self.duration_by_name = {} self.duration_by_name_qubits = {} self.dt = dt # pylint: disable=invalid-name if instruction_durations: self.update(instruction_durations)
def __str__(self): """Return a string representation of all stored durations.""" string = "" for k, v in self.duration_by_name.items(): string += k string += ': ' string += str(v[0]) + ' ' + v[1] string += '\n' for k, v in self.duration_by_name_qubits.items(): string += k[0] + str(k[1]) string += ': ' string += str(v[0]) + ' ' + v[1] string += '\n' return string
[문서] @classmethod def from_backend(cls, backend: BaseBackend): """Construct an :class:`InstructionDurations` object from the backend. Args: backend: backend from which durations (gate lengths) and dt are extracted. Returns: InstructionDurations: The InstructionDurations constructed from backend. Raises: TranspilerError: If dt and dtm is different in the backend. """ # All durations in seconds in gate_length instruction_durations = [] for gate, insts in for qubits, props in insts.items(): if 'gate_length' in props: gate_length = props['gate_length'][0] # Throw away datetime at index 1 instruction_durations.append((gate, qubits, gate_length, 's')) try: dt = backend.configuration().dt # pylint: disable=invalid-name except AttributeError: dt = None # TODO: should tell us durations of measurements # TODO: Remove the following lines after that try: inst_map = backend.defaults().instruction_schedule_map all_qubits = tuple(range(backend.configuration().num_qubits)) meas_duration = inst_map.get('measure', all_qubits).duration for q in all_qubits: instruction_durations.append(('measure', [q], meas_duration, 'dt')) except AttributeError: pass return InstructionDurations(instruction_durations, dt=dt)
[문서] def update(self, inst_durations: Optional['InstructionDurationsType'], dt: float = None): """Update self with inst_durations (inst_durations overwrite self). Args: inst_durations: Instruction durations to be merged into self (overwriting self). dt: Sampling duration in seconds of the target backend. Returns: InstructionDurations: The updated InstructionDurations. Raises: TranspilerError: If the format of instruction_durations is invalid. """ if dt: self.dt = dt if inst_durations is None: return self if isinstance(inst_durations, InstructionDurations): self.duration_by_name.update(inst_durations.duration_by_name) self.duration_by_name_qubits.update(inst_durations.duration_by_name_qubits) else: for i, items in enumerate(inst_durations): if len(items) == 3: inst_durations[i] = (*items, 'dt') # set default unit elif len(items) != 4: raise TranspilerError("Each entry of inst_durations dictionary must be " "(inst_name, qubits, duration) or " "(inst_name, qubits, duration, unit)") for name, qubits, duration, unit in inst_durations: if isinstance(qubits, int): qubits = [qubits] if qubits is None: self.duration_by_name[name] = duration, unit else: self.duration_by_name_qubits[(name, tuple(qubits))] = duration, unit return self
[문서] def get(self, inst: Union[str, Instruction], qubits: Union[int, List[int], Qubit, List[Qubit]], unit: str = 'dt') -> Union[float, int]: """Get the duration of the instruction with the name and the qubits. Args: inst: An instruction or its name to be queried. qubits: Qubits or its indices that the instruction acts on. unit: The unit of duration to be returned. It must be 's' or 'dt'. Returns: float|int: The duration of the instruction on the qubits. Raises: TranspilerError: No duration is defined for the instruction. """ if isinstance(inst, Barrier): return 0 elif isinstance(inst, Delay): return self._convert_unit(inst.duration, inst.unit, unit) if isinstance(inst, Instruction): inst_name = else: inst_name = inst if isinstance(qubits, (int, Qubit)): qubits = [qubits] if isinstance(qubits[0], Qubit): qubits = [q.index for q in qubits] try: return self._get(inst_name, qubits, unit) except TranspilerError: raise TranspilerError("Duration of {} on qubits {} is not found." .format(inst_name, qubits))
def _get(self, name: str, qubits: List[int], to_unit: str) -> Union[float, int]: """Get the duration of the instruction with the name and the qubits.""" if name == 'barrier': return 0 key = (name, tuple(qubits)) if key in self.duration_by_name_qubits: duration, unit = self.duration_by_name_qubits[key] elif name in self.duration_by_name: duration, unit = self.duration_by_name[name] else: raise TranspilerError("No value is found for key={}".format(key)) return self._convert_unit(duration, unit, to_unit) 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))
[문서] def units_used(self) -> Set[str]: """Get the set of all units used in this instruction durations. Returns: Set of units used in this instruction durations. """ units_used = set() for _, unit in self.duration_by_name_qubits.values(): units_used.add(unit) for _, unit in self.duration_by_name.values(): units_used.add(unit) return units_used
InstructionDurationsType = Union[List[Tuple[str, Optional[Iterable[int]], Union[float, int], str]], List[Tuple[str, Optional[Iterable[int]], Union[float, int]]], InstructionDurations] """List of tuples representing (instruction name, qubits indices, duration)."""

© Copyright 2020, Qiskit Development Team. 최종 업데이트: 2021/01/17

Built with Sphinx using a theme provided by Read the Docs.
당신은 다음 언어로 문서를 보고 있다 lang: Korean