示例#1
0
 def test_update_from_instruction_schedule_map_add_instruction(self):
     target = Target()
     inst_map = InstructionScheduleMap()
     inst_map.add("sx", 0, self.custom_sx_q0)
     inst_map.add("sx", 1, self.custom_sx_q1)
     target.update_from_instruction_schedule_map(inst_map, {"sx": SXGate()})
     self.assertEqual(inst_map, target.instruction_schedule_map())
示例#2
0
 def test_instruction_schedule_map_ideal_sim_backend(self):
     ideal_sim_target = Target(num_qubits=3)
     theta = Parameter("theta")
     phi = Parameter("phi")
     lam = Parameter("lambda")
     for inst in [
             UGate(theta, phi, lam),
             RXGate(theta),
             RYGate(theta),
             RZGate(theta),
             CXGate(),
             ECRGate(),
             CCXGate(),
             Measure(),
     ]:
         ideal_sim_target.add_instruction(inst, {None: None})
     inst_map = ideal_sim_target.instruction_schedule_map()
     self.assertEqual(InstructionScheduleMap(), inst_map)
示例#3
0
class TestPulseTarget(QiskitTestCase):
    def setUp(self):
        super().setUp()
        self.pulse_target = Target(dt=3e-7,
                                   granularity=2,
                                   min_length=4,
                                   pulse_alignment=8,
                                   aquire_alignment=8)
        with pulse.build(name="sx_q0") as self.custom_sx_q0:
            pulse.play(pulse.Constant(100, 0.1), pulse.DriveChannel(0))
        with pulse.build(name="sx_q1") as self.custom_sx_q1:
            pulse.play(pulse.Constant(100, 0.2), pulse.DriveChannel(1))
        sx_props = {
            (0, ):
            InstructionProperties(duration=35.5e-9,
                                  error=0.000413,
                                  calibration=self.custom_sx_q0),
            (1, ):
            InstructionProperties(duration=35.5e-9,
                                  error=0.000502,
                                  calibration=self.custom_sx_q1),
        }
        self.pulse_target.add_instruction(SXGate(), sx_props)

    def test_instruction_schedule_map(self):
        inst_map = self.pulse_target.instruction_schedule_map()
        self.assertIn("sx", inst_map.instructions)
        self.assertEqual(inst_map.qubits_with_instruction("sx"), [0, 1])
        self.assertTrue("sx" in inst_map.qubit_instructions(0))

    def test_instruction_schedule_map_ideal_sim_backend(self):
        ideal_sim_target = Target(num_qubits=3)
        theta = Parameter("theta")
        phi = Parameter("phi")
        lam = Parameter("lambda")
        for inst in [
                UGate(theta, phi, lam),
                RXGate(theta),
                RYGate(theta),
                RZGate(theta),
                CXGate(),
                ECRGate(),
                CCXGate(),
                Measure(),
        ]:
            ideal_sim_target.add_instruction(inst, {None: None})
        inst_map = ideal_sim_target.instruction_schedule_map()
        self.assertEqual(InstructionScheduleMap(), inst_map)

    def test_str(self):
        expected = """Target
Number of qubits: 2
Instructions:
	sx
		(0,):
			Duration: 3.55e-08 sec.
			Error Rate: 0.000413
			With pulse schedule calibration
		(1,):
			Duration: 3.55e-08 sec.
			Error Rate: 0.000502
			With pulse schedule calibration
"""
        self.assertEqual(expected, str(self.pulse_target))

    def test_update_from_instruction_schedule_map_add_instruction(self):
        target = Target()
        inst_map = InstructionScheduleMap()
        inst_map.add("sx", 0, self.custom_sx_q0)
        inst_map.add("sx", 1, self.custom_sx_q1)
        target.update_from_instruction_schedule_map(inst_map, {"sx": SXGate()})
        self.assertEqual(inst_map, target.instruction_schedule_map())

    def test_update_from_instruction_schedule_map_update_schedule(self):
        self.pulse_target.dt = None
        inst_map = InstructionScheduleMap()
        with pulse.build(name="sx_q1") as custom_sx:
            pulse.play(pulse.Constant(1000, 0.2), pulse.DriveChannel(1))

        inst_map.add("sx", 0, self.custom_sx_q0)
        inst_map.add("sx", 1, custom_sx)
        self.pulse_target.update_from_instruction_schedule_map(
            inst_map, {"sx": SXGate()})
        self.assertEqual(inst_map,
                         self.pulse_target.instruction_schedule_map())
        self.assertIsNone(self.pulse_target["sx"][(0, )].duration)
        self.assertIsNone(self.pulse_target["sx"][(0, )].error)
        self.assertIsNone(self.pulse_target["sx"][(1, )].duration)
        self.assertIsNone(self.pulse_target["sx"][(1, )].error)

    def test_update_from_instruction_schedule_map_new_instruction_no_name_map(
            self):
        target = Target()
        inst_map = InstructionScheduleMap()
        inst_map.add("sx", 0, self.custom_sx_q0)
        inst_map.add("sx", 1, self.custom_sx_q1)
        with self.assertRaises(ValueError):
            target.update_from_instruction_schedule_map(inst_map)

    def test_update_from_instruction_schedule_map_new_qarg_raises(self):
        inst_map = InstructionScheduleMap()
        inst_map.add("sx", 0, self.custom_sx_q0)
        inst_map.add("sx", 1, self.custom_sx_q1)
        inst_map.add("sx", 2, self.custom_sx_q1)
        with self.assertRaises(KeyError):
            self.pulse_target.update_from_instruction_schedule_map(inst_map)

    def test_update_from_instruction_schedule_map_with_dt_set(self):
        inst_map = InstructionScheduleMap()
        with pulse.build(name="sx_q1") as custom_sx:
            pulse.play(pulse.Constant(1000, 0.2), pulse.DriveChannel(1))

        inst_map.add("sx", 0, self.custom_sx_q0)
        inst_map.add("sx", 1, custom_sx)
        self.pulse_target.dt = 1.0
        self.pulse_target.update_from_instruction_schedule_map(
            inst_map, {"sx": SXGate()})
        self.assertEqual(inst_map,
                         self.pulse_target.instruction_schedule_map())
        self.assertEqual(self.pulse_target["sx"][(1, )].duration, 1000.0)
        self.assertIsNone(self.pulse_target["sx"][(1, )].error)
        self.assertIsNone(self.pulse_target["sx"][(0, )].error)

    def test_update_from_instruction_schedule_map_with_error_dict(self):
        inst_map = InstructionScheduleMap()
        with pulse.build(name="sx_q1") as custom_sx:
            pulse.play(pulse.Constant(1000, 0.2), pulse.DriveChannel(1))

        inst_map.add("sx", 0, self.custom_sx_q0)
        inst_map.add("sx", 1, custom_sx)
        self.pulse_target.dt = 1.0
        error_dict = {"sx": {(1, ): 1.0}}

        self.pulse_target.update_from_instruction_schedule_map(
            inst_map, {"sx": SXGate()}, error_dict=error_dict)
        self.assertEqual(self.pulse_target["sx"][(1, )].error, 1.0)
        self.assertIsNone(self.pulse_target["sx"][(0, )].error)

    def test_timing_constraints(self):
        generated_constraints = self.pulse_target.timing_constraints()
        expected_constraints = TimingConstraints(2, 4, 8, 8)
        for i in [
                "granularity", "min_length", "pulse_alignment",
                "acquire_alignment"
        ]:
            self.assertEqual(
                getattr(generated_constraints, i),
                getattr(expected_constraints, i),
                f"Generated constraints differs from expected for attribute {i}"
                f"{getattr(generated_constraints, i)}!={getattr(expected_constraints, i)}",
            )