Exemplo n.º 1
0
def _parse_instruction_durations(backend, inst_durations, dt, circuits):
    """Create a list of ``InstructionDuration``s. If ``inst_durations`` is provided,
    the backend will be ignored, otherwise, the durations will be populated from the
    backend. If any circuits have gate calibrations, those calibration durations would
    take precedence over backend durations, but be superceded by ``inst_duration``s.
    """
    if not inst_durations:
        backend_durations = InstructionDurations()
        try:
            backend_durations = InstructionDurations.from_backend(backend)
        except AttributeError:
            pass

    durations = []
    for circ in circuits:
        circ_durations = InstructionDurations()
        if not inst_durations:
            circ_durations.update(backend_durations, dt or backend_durations.dt)

        if circ.calibrations:
            cal_durations = []
            for gate, gate_cals in circ.calibrations.items():
                for (qubits, _), schedule in gate_cals.items():
                    cal_durations.append((gate, qubits, schedule.duration))
            circ_durations.update(cal_durations, circ_durations.dt)

        if inst_durations:
            circ_durations.update(inst_durations, dt or getattr(inst_durations, 'dt', None))

        durations.append(circ_durations)
    return durations
Exemplo n.º 2
0
    def from_backend(cls, backend, **pass_manager_options):
        """Construct a configuration based on a backend and user input.

        This method automatically gererates a PassManagerConfig object based on the backend's
        features. User options can be used to overwrite the configuration.

        Args:
            backend (BackendV1): The backend that provides the configuration.
            pass_manager_options: User-defined option-value pairs.

        Returns:
            PassManagerConfig: The configuration generated based on the arguments.

        Raises:
            AttributeError: If the backend does not support a `configuration()` method.
        """
        res = cls(**pass_manager_options)
        config = backend.configuration()

        if res.basis_gates is None:
            res.basis_gates = getattr(config, "basis_gates", None)
        if res.inst_map is None and hasattr(backend, "defaults"):
            res.inst_map = backend.defaults().instruction_schedule_map
        if res.coupling_map is None:
            res.coupling_map = CouplingMap(
                getattr(config, "coupling_map", None))
        if res.instruction_durations is None:
            res.instruction_durations = InstructionDurations.from_backend(
                backend)
        if res.backend_properties is None:
            res.backend_properties = backend.properties()

        return res
 def test_from_backend_for_backend_without_dt(self):
     backend = FakeTokyo()
     gate = self._find_gate_with_length(backend)
     durations = InstructionDurations.from_backend(backend)
     self.assertIsNone(durations.dt)
     self.assertGreater(durations.get(gate, 0, "s"), 0)
     with self.assertRaises(TranspilerError):
         durations.get(gate, 0)
Exemplo n.º 4
0
def _parse_instruction_durations(backend, inst_durations, dt,
                                 scheduling_method, num_circuits):
    durations = None
    if scheduling_method is not None:
        from qiskit.transpiler.instruction_durations import InstructionDurations
        if backend:
            durations = InstructionDurations.from_backend(backend).update(
                inst_durations, dt)
        else:
            durations = InstructionDurations(inst_durations, dt)

    if not isinstance(durations, list):
        durations = [durations] * num_circuits
    return durations
Exemplo n.º 5
0
def _parse_instruction_durations(backend, inst_durations, dt, num_circuits):
    durations = None
    if inst_durations is None and backend:
        try:
            backend_durations = InstructionDurations.from_backend(backend)
        except AttributeError:
            backend_durations = InstructionDurations()
        durations = backend_durations.update(None, dt)
    else:
        durations = InstructionDurations(
            inst_durations, dt or getattr(inst_durations, 'dt', None))

    if not isinstance(durations, list):
        durations = [durations] * num_circuits
    return durations
Exemplo n.º 6
0
    def test_unit_seconds_when_using_backend_durations(self):
        qc = QuantumCircuit(2)
        qc.h(0)
        qc.delay(500 * self.dt, 1, 's')
        qc.cx(0, 1)
        # usual case
        scheduled = transpile(qc,
                              backend=self.backend_with_dt,
                              scheduling_method='alap')
        self.assertEqual(scheduled.duration, 1908)

        # update durations
        durations = InstructionDurations.from_backend(self.backend_with_dt)
        durations.update([('cx', [0, 1], 1000 * self.dt, 's')])
        scheduled = transpile(qc,
                              backend=self.backend_with_dt,
                              scheduling_method='alap',
                              instruction_durations=durations)
        self.assertEqual(scheduled.duration, 1500)
    def test_unit_seconds_when_using_backend_durations(self):
        qc = QuantumCircuit(2)
        qc.h(0)
        qc.delay(500 * self.dt, 1, "s")
        qc.cx(0, 1)
        # usual case
        scheduled = transpile(
            qc, backend=self.backend_with_dt, scheduling_method="alap", layout_method="trivial"
        )
        self.assertEqual(scheduled.duration, 2132)

        # update durations
        durations = InstructionDurations.from_backend(self.backend_with_dt)
        durations.update([("cx", [0, 1], 1000 * self.dt, "s")])
        scheduled = transpile(
            qc,
            backend=self.backend_with_dt,
            scheduling_method="alap",
            instruction_durations=durations,
            layout_method="trivial",
        )
        self.assertEqual(scheduled.duration, 1500)
 def test_from_backend_for_backend_without_dt(self):
     durations = InstructionDurations.from_backend(FakeVigo())
     self.assertIsNone(durations.dt)
     self.assertGreater(durations.get('u2', 0, 's'), 0)
     with self.assertRaises(TranspilerError):
         durations.get('u2', 0)
 def test_from_backend_for_backend_with_dt(self):
     durations = InstructionDurations.from_backend(FakeParis())
     self.assertGreater(durations.dt, 0)
     self.assertGreater(durations.get('u2', 0), 0)
 def test_from_backend_for_backend_with_dt(self):
     backend = FakeParis()
     gate = self._find_gate_with_length(backend)
     durations = InstructionDurations.from_backend(backend)
     self.assertGreater(durations.dt, 0)
     self.assertGreater(durations.get(gate, 0), 0)