Пример #1
0
    def test_persistent_value(self):
        """Test converted qobj from PersistentValueInstruction."""
        with self.assertWarns(DeprecationWarning):
            cmd = PersistentValue(value=0.1j)
        with self.assertWarns(DeprecationWarning):
            instruction = cmd(ControlChannel(1))

        qobj = PulseQobjInstruction(name='pv', ch='u1', t0=0, val=0.1j)
        with self.assertWarns(DeprecationWarning):
            converted_instruction = self.converter(qobj)

        self.assertEqual(converted_instruction.start_time, 0)
        self.assertEqual(converted_instruction.duration, 0)
        self.assertEqual(converted_instruction.instructions[0][-1],
                         instruction)
Пример #2
0
    def test_snapshot(self):
        """Test converted qobj from SnapShot."""
        instruction = Snapshot(label='label', snapshot_type='type')
        shifted = instruction << 10

        qobj = PulseQobjInstruction(name='snapshot',
                                    t0=10,
                                    label='label',
                                    type='type')
        converted_instruction = self.converter(qobj)

        self.assertEqual(converted_instruction.start_time, shifted.start_time)
        self.assertEqual(converted_instruction.duration, shifted.duration)
        self.assertEqual(converted_instruction.instructions[0][-1],
                         instruction)
Пример #3
0
    def test_constant_pulse_instruction(self):
        """Test that parametric pulses are correctly converted to PulseQobjInstructions."""
        converter = InstructionToQobjConverter(PulseQobjInstruction,
                                               meas_level=2)
        instruction = Play(Constant(duration=25, amp=1), ControlChannel(2))

        valid_qobj = PulseQobjInstruction(name='parametric_pulse',
                                          pulse_shape='constant',
                                          ch='u2',
                                          t0=20,
                                          parameters={
                                              'duration': 25,
                                              'amp': 1
                                          })
        self.assertEqual(converter(20, instruction), valid_qobj)
Пример #4
0
    def test_shift_frequency(self):
        """Test converted qobj from ShiftFrequency."""
        instruction = ShiftFrequency(8.0e9, DriveChannel(0))

        qobj = PulseQobjInstruction(name='shiftf',
                                    ch='d0',
                                    t0=0,
                                    frequency=8.0)
        converted_instruction = self.converter(qobj)

        self.assertEqual(converted_instruction.start_time, 0)
        self.assertEqual(converted_instruction.duration, 0)
        self.assertEqual(converted_instruction.instructions[0][-1],
                         instruction)
        self.assertTrue('frequency' in qobj.to_dict())
Пример #5
0
 def test_gaussian_pulse_instruction(self):
     """Test that parametric pulses are correctly converted to PulseQobjInstructions."""
     converter = InstructionToQobjConverter(PulseQobjInstruction,
                                            meas_level=2)
     instruction = Play(Gaussian(duration=25, sigma=15, amp=-0.5 + 0.2j),
                        DriveChannel(0))
     valid_qobj = PulseQobjInstruction(name='parametric_pulse',
                                       pulse_shape='gaussian',
                                       ch='d0',
                                       t0=0,
                                       parameters={
                                           'duration': 25,
                                           'sigma': 15,
                                           'amp': -0.5 + 0.2j
                                       })
     self.assertEqual(converter(0, instruction), valid_qobj)
Пример #6
0
    def test_parameterized_set_frequency(self):
        """Test converted qobj from SetFrequency, when passing a parameterized frequency."""
        qobj = PulseQobjInstruction(name='setf', ch='d0', t0=2, frequency='f')
        self.assertTrue('frequency' in qobj.to_dict())

        converted_instruction = self.converter(qobj)
        self.assertIsInstance(converted_instruction, ParameterizedSchedule)

        evaluated_instruction = converted_instruction.bind_parameters(2.)

        instruction = SetFrequency(2.e9, DriveChannel(0))

        self.assertEqual(evaluated_instruction.start_time, 2)
        self.assertEqual(evaluated_instruction.duration, 2)
        self.assertEqual(evaluated_instruction.instructions[0][-1],
                         instruction)
    def test_parameterized_frame_change(self):
        """Test converted qobj from FrameChangeInstruction."""
        cmd = FrameChange(phase=4.)
        instruction = cmd(MeasureChannel(0)) << 10

        qobj = PulseQobjInstruction(name='fc', ch='m0', t0=10, phase='P1**2')
        converted_instruction = self.converter(qobj)

        self.assertIsInstance(converted_instruction, ParameterizedSchedule)

        evaluated_instruction = converted_instruction.bind_parameters(2.)

        self.assertEqual(evaluated_instruction.timeslots,
                         instruction.timeslots)
        self.assertEqual(evaluated_instruction.instructions[0][-1].command,
                         cmd)
Пример #8
0
    def test_parameterized_frame_change(self):
        """Test converted qobj from ShiftPhase."""
        instruction = ShiftPhase(4., MeasureChannel(0))
        shifted = instruction << 10

        qobj = PulseQobjInstruction(name='fc', ch='m0', t0=10, phase='P1**2')
        converted_instruction = self.converter(qobj)

        self.assertIsInstance(converted_instruction, ParameterizedSchedule)

        evaluated_instruction = converted_instruction.bind_parameters(2.)

        self.assertEqual(evaluated_instruction.start_time, shifted.start_time)
        self.assertEqual(evaluated_instruction.duration, shifted.duration)
        self.assertEqual(evaluated_instruction.instructions[0][-1],
                         instruction)
Пример #9
0
    def test_acquire(self):
        """Test converted qobj from AcquireInstruction."""
        cmd = Acquire(10, Discriminator(name='test_disc', params={'test_params': 1.0}),
                      Kernel(name='test_kern', params={'test_params': 'test'}))
        instruction = cmd(self.device.q, self.device.mem, self.device.c)

        qobj = PulseQobjInstruction(name='acquire', t0=0, duration=10, qubits=[0, 1],
                                    memory_slot=[0, 1], register_slot=[0, 1],
                                    kernels=[QobjMeasurementOption(
                                        name='test_kern', params={'test_params': 'test'})],
                                    discriminators=[QobjMeasurementOption(
                                        name='test_disc', params={'test_params': 1.0})])
        converted_instruction = self.converter(qobj)

        self.assertEqual(converted_instruction.timeslots, instruction.timeslots)
        self.assertEqual(converted_instruction.instructions[0][-1].command, cmd)
Пример #10
0
    def test_acquire(self):
        """Test converted qobj from AcquireInstruction."""
        converter = PulseQobjConverter(PulseQobjInstruction, meas_level=2)
        command = Acquire(duration=10)
        instruction = command(self.device.q, self.device.mem, self.device.c)

        valid_qobj = PulseQobjInstruction(
            name='acquire',
            t0=0,
            duration=10,
            qubits=[0],
            memory_slot=[0],
            register_slot=[0]
        )

        self.assertEqual(converter(instruction), valid_qobj)
Пример #11
0
 def test_parametric_pulses(self):
     """Test converted qobj from ParametricInstruction."""
     instruction = Play(Gaussian(duration=25, sigma=15, amp=-0.5 + 0.2j, name='pulse1'),
                        DriveChannel(0))
     qobj = PulseQobjInstruction(
         name='parametric_pulse',
         label='pulse1',
         pulse_shape='gaussian',
         ch='d0',
         t0=0,
         parameters={'duration': 25, 'sigma': 15, 'amp': -0.5 + 0.2j})
     converted_instruction = self.converter(qobj)
     self.assertEqual(converted_instruction.start_time, 0)
     self.assertEqual(converted_instruction.duration, 25)
     self.assertEqual(converted_instruction.instructions[0][-1], instruction)
     self.assertEqual(converted_instruction.instructions[0][-1].pulse.name, 'pulse1')
Пример #12
0
    def test_persistent_value(self):
        """Test converted qobj from PersistentValueInstruction."""
        converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2)
        with self.assertWarns(DeprecationWarning):
            command = PersistentValue(value=0.1j)
        with self.assertWarns(DeprecationWarning):
            instruction = command(DriveChannel(0))

        valid_qobj = PulseQobjInstruction(
            name='pv',
            ch='d0',
            t0=0,
            val=0.1j
        )

        with self.assertWarns(DeprecationWarning):
            self.assertEqual(converter(0, instruction), valid_qobj)
    def test_constant_pulse_instruction(self):
        """Test that parametric pulses are correctly converted to PulseQobjInstructions."""
        converter = InstructionToQobjConverter(PulseQobjInstruction,
                                               meas_level=2)
        instruction = Play(Constant(duration=25, amp=1), ControlChannel(2))

        valid_qobj = PulseQobjInstruction(
            name="parametric_pulse",
            pulse_shape="constant",
            ch="u2",
            t0=20,
            parameters={
                "duration": 25,
                "amp": 1
            },
        )
        self.assertEqual(converter(20, instruction), valid_qobj)
Пример #14
0
    def test_parameterized_schedule(self):
        """Test adding parameterized schedule."""
        converter = QobjToInstructionConverter([], buffer=0)
        qobj = PulseQobjInstruction(name='pv', ch='u1', t0=10, val='P2*cos(np.pi*P1)')
        converted_instruction = converter(qobj)

        inst_map = InstructionScheduleMap()

        inst_map.add('pv_test', 0, converted_instruction)
        self.assertEqual(inst_map.get_parameters('pv_test', 0), ('P1', 'P2'))

        sched = inst_map.get('pv_test', 0, P1=0, P2=-1)
        self.assertEqual(sched.instructions[0][-1].command.value, -1)
        with self.assertRaises(PulseError):
            inst_map.get('pv_test', 0, 0, P1=-1)
        with self.assertRaises(PulseError):
            inst_map.get('pv_test', 0, P1=1, P2=2, P3=3)
Пример #15
0
    def test_parameterized_shift_frequency(self):
        """Test converted qobj from ShiftFrequency, with a parameterized frequency."""
        instruction = ShiftFrequency(8.0e9, DriveChannel(0))

        qobj = PulseQobjInstruction(name='shiftf', ch='d0', t0=1, frequency='f / 1000')
        self.assertTrue('frequency' in qobj.to_dict())

        converted_instruction = self.converter(qobj)
        self.assertIsInstance(converted_instruction, ParameterizedSchedule)

        evaluated_instruction = converted_instruction.bind_parameters(3.14)

        instruction = ShiftFrequency(3.14e6, DriveChannel(0))

        self.assertEqual(evaluated_instruction.start_time, 1)
        self.assertEqual(evaluated_instruction.duration, 1)
        self.assertEqual(evaluated_instruction.instructions[0][-1], instruction)
Пример #16
0
    def test_parameterized_persistent_value(self):
        """Test converted qobj from PersistentValueInstruction."""
        with self.assertWarns(DeprecationWarning):
            cmd = PersistentValue(value=0.5+0.j)
        with self.assertWarns(DeprecationWarning):
            instruction = cmd(ControlChannel(1)) << 10

        qobj = PulseQobjInstruction(name='pv', ch='u1', t0=10, val='P1*cos(np.pi*P2)')
        converted_instruction = self.converter(qobj)

        self.assertIsInstance(converted_instruction, ParameterizedSchedule)

        with self.assertWarns(DeprecationWarning):
            evaluated_instruction = converted_instruction.bind_parameters(P1=0.5, P2=0.)

        self.assertEqual(evaluated_instruction.timeslots, instruction.timeslots)
        self.assertEqual(evaluated_instruction.instructions[0][-1].command, cmd)
    def test_parameterized_set_frequency(self):
        """Test converted qobj from SetFrequency, when passing a parameterized frequency."""
        qobj = PulseQobjInstruction(name="setf", ch="d0", t0=2, frequency="f")
        self.assertTrue("frequency" in qobj.to_dict())

        converted_instruction = self.converter(qobj)
        self.assertIsInstance(converted_instruction, Schedule)

        bind_dict = {converted_instruction.get_parameters("f")[0]: 2.0}
        evaluated_instruction = converted_instruction.assign_parameters(
            bind_dict)

        instruction = SetFrequency(2.0e9, DriveChannel(0))

        self.assertEqual(evaluated_instruction.start_time, 2)
        self.assertEqual(evaluated_instruction.duration, 2)
        self.assertEqual(evaluated_instruction.instructions[0][-1],
                         instruction)
Пример #18
0
    def test_parametric_pulses_no_label(self):
        """Test converted qobj from ParametricInstruction without label."""
        base_str = "gaussian_[('amp', (-0.5+0.2j)), ('duration', 25), ('sigma', 15)]"
        short_pulse_id = hashlib.md5(base_str.encode('utf-8')).hexdigest()[:4]
        pulse_name = 'gaussian_{}'.format(short_pulse_id)

        qobj = PulseQobjInstruction(name='parametric_pulse',
                                    pulse_shape='gaussian',
                                    ch='d0',
                                    t0=0,
                                    parameters={
                                        'duration': 25,
                                        'sigma': 15,
                                        'amp': -0.5 + 0.2j
                                    })
        converted_instruction = self.converter(qobj)
        self.assertEqual(converted_instruction.instructions[0][-1].pulse.name,
                         pulse_name)
 def test_gaussian_pulse_instruction(self):
     """Test that parametric pulses are correctly converted to PulseQobjInstructions."""
     converter = InstructionToQobjConverter(PulseQobjInstruction,
                                            meas_level=2)
     instruction = Play(Gaussian(duration=25, sigma=15, amp=-0.5 + 0.2j),
                        DriveChannel(0))
     valid_qobj = PulseQobjInstruction(
         name="parametric_pulse",
         pulse_shape="gaussian",
         ch="d0",
         t0=0,
         parameters={
             "duration": 25,
             "sigma": 15,
             "amp": -0.5 + 0.2j
         },
     )
     self.assertEqual(converter(0, instruction), valid_qobj)
    def test_parameterized_frame_change(self):
        """Test converted qobj from ShiftPhase."""
        instruction = ShiftPhase(4.0, MeasureChannel(0))
        shifted = instruction << 10

        qobj = PulseQobjInstruction(name="fc", ch="m0", t0=10, phase="P1*2")
        converted_instruction = self.converter(qobj)

        self.assertIsInstance(converted_instruction, Schedule)

        bind_dict = {converted_instruction.get_parameters("P1")[0]: 2.0}
        evaluated_instruction = converted_instruction.assign_parameters(
            bind_dict)

        self.assertEqual(evaluated_instruction.start_time, shifted.start_time)
        self.assertEqual(evaluated_instruction.duration, shifted.duration)
        self.assertEqual(evaluated_instruction.instructions[0][-1],
                         instruction)
Пример #21
0
    def test_frame_change(self):
        """Test converted qobj from FrameChangeInstruction."""
        converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2)
        with self.assertWarns(DeprecationWarning):
            command = FrameChange(phase=0.1)
        with self.assertWarns(DeprecationWarning):
            instruction = command(DriveChannel(0))

        valid_qobj = PulseQobjInstruction(
            name='fc',
            ch='d0',
            t0=0,
            phase=0.1
        )

        self.assertEqual(converter(0, instruction), valid_qobj)
        instruction = ShiftPhase(0.1, DriveChannel(0))
        self.assertEqual(converter(0, instruction), valid_qobj)
Пример #22
0
 def test_parametric_pulses(self):
     """Test converted qobj from ParametricInstruction."""
     instruction = Gaussian(duration=25, sigma=15,
                            amp=-0.5 + 0.2j)(DriveChannel(0))
     qobj = PulseQobjInstruction(name='parametric_pulse',
                                 pulse_shape='gaussian',
                                 ch='d0',
                                 t0=0,
                                 parameters={
                                     'duration': 25,
                                     'sigma': 15,
                                     'amp': -0.5 + 0.2j
                                 })
     converted_instruction = self.converter(qobj)
     self.assertEqual(converted_instruction.timeslots,
                      instruction.timeslots)
     self.assertEqual(converted_instruction.instructions[0][-1].command,
                      instruction.command)
Пример #23
0
    def test_drag_pulse_instruction(self):
        """Test that parametric pulses are correctly converted to PulseQobjInstructions."""
        converter = InstructionToQobjConverter(PulseQobjInstruction,
                                               meas_level=2)
        instruction = Drag(duration=25, sigma=15, amp=-0.5 + 0.2j,
                           beta=0.5)(DriveChannel(0))

        valid_qobj = PulseQobjInstruction(name='parametric_pulse',
                                          pulse_shape='drag',
                                          ch='d0',
                                          t0=30,
                                          parameters={
                                              'duration': 25,
                                              'sigma': 15,
                                              'amp': -0.5 + 0.2j,
                                              'beta': 0.5
                                          })
        self.assertEqual(converter(30, instruction), valid_qobj)
    def test_parametric_pulses_no_label(self):
        """Test converted qobj from ParametricInstruction without label."""
        base_str = "gaussian_[('amp', (-0.5+0.2j)), ('duration', 25), ('sigma', 15)]"
        short_pulse_id = hashlib.md5(base_str.encode("utf-8")).hexdigest()[:4]
        pulse_name = f"gaussian_{short_pulse_id}"

        qobj = PulseQobjInstruction(
            name="parametric_pulse",
            pulse_shape="gaussian",
            ch="d0",
            t0=0,
            parameters={
                "duration": 25,
                "sigma": 15,
                "amp": -0.5 + 0.2j
            },
        )
        converted_instruction = self.converter(qobj)
        self.assertEqual(converted_instruction.instructions[0][-1].pulse.name,
                         pulse_name)
Пример #25
0
    def test_parameterized_schedule(self):
        """Test building parameterized schedule."""
        cmd_def = CmdDef()
        converter = QobjToInstructionConverter([], buffer=0)
        qobj = PulseQobjInstruction(name='pv', ch='u1', t0=10, val='P2*cos(np.pi*P1)')
        converted_instruction = converter(qobj)

        cmd_def.add('pv_test', 0, converted_instruction)
        self.assertEqual(cmd_def.get_parameters('pv_test', 0), ('P1', 'P2'))

        sched = cmd_def.get('pv_test', 0, '0', P2=-1)
        self.assertEqual(sched.instructions[0][-1].command.value, -1)

        with self.assertRaises(PulseError):
            cmd_def.get('pv_test', 0, '0', P1=-1)

        sched = cmd_def.pop('pv_test', 0, '0', P2=-1)
        self.assertEqual(sched.instructions[0][-1].command.value, -1)

        self.assertFalse(cmd_def.has('pv_test', 0))
Пример #26
0
    def test_gaussian_square_pulse_instruction(self):
        """Test that parametric pulses are correctly converted to PulseQobjInstructions."""
        converter = InstructionToQobjConverter(PulseQobjInstruction,
                                               meas_level=2)
        instruction = GaussianSquare(duration=1500,
                                     sigma=15,
                                     amp=-0.5 + 0.2j,
                                     width=1300)(MeasureChannel(1))

        valid_qobj = PulseQobjInstruction(name='parametric_pulse',
                                          pulse_shape='gaussian_square',
                                          ch='m1',
                                          t0=10,
                                          parameters={
                                              'duration': 1500,
                                              'sigma': 15,
                                              'amp': -0.5 + 0.2j,
                                              'width': 1300
                                          })
        self.assertEqual(converter(10, instruction), valid_qobj)
    def test_acquire(self):
        """Test converted qobj from Acquire."""
        schedule = Schedule()
        for i in range(self.num_qubits):
            schedule |= Acquire(
                10,
                AcquireChannel(i),
                MemorySlot(i),
                RegisterSlot(i),
                kernel=Kernel(name="test_kern", test_params="test"),
                discriminator=Discriminator(name="test_disc", test_params=1.0),
            )

        qobj = PulseQobjInstruction(
            name="acquire",
            t0=0,
            duration=10,
            qubits=[0, 1],
            memory_slot=[0, 1],
            register_slot=[0, 1],
            kernels=[
                QobjMeasurementOption(name="test_kern",
                                      params={"test_params": "test"})
            ],
            discriminators=[
                QobjMeasurementOption(name="test_disc",
                                      params={"test_params": 1.0})
            ],
        )
        converted_instruction = self.converter(qobj)

        self.assertEqual(converted_instruction.start_time, 0)
        self.assertEqual(converted_instruction.duration, 10)
        self.assertEqual(converted_instruction.instructions[0][-1].duration,
                         10)
        self.assertEqual(
            converted_instruction.instructions[0][-1].kernel.params,
            {"test_params": "test"})
        self.assertEqual(converted_instruction.instructions[1][-1].channel,
                         AcquireChannel(1))
Пример #28
0
    def test_acquire(self):
        """Test converted qobj from Acquire."""
        schedule = Schedule()
        for i in range(self.num_qubits):
            schedule |= Acquire(10, AcquireChannel(i), MemorySlot(i), RegisterSlot(i),
                                kernel=Kernel(name='test_kern', test_params='test'),
                                discriminator=Discriminator(name='test_disc',
                                                            test_params=1.0))

        qobj = PulseQobjInstruction(name='acquire', t0=0, duration=10, qubits=[0, 1],
                                    memory_slot=[0, 1], register_slot=[0, 1],
                                    kernels=[QobjMeasurementOption(
                                        name='test_kern', params={'test_params': 'test'})],
                                    discriminators=[QobjMeasurementOption(
                                        name='test_disc', params={'test_params': 1.0})])
        converted_instruction = self.converter(qobj)

        self.assertEqual(converted_instruction.timeslots, schedule.timeslots)
        self.assertEqual(converted_instruction.instructions[0][-1].duration, 10)
        self.assertEqual(converted_instruction.instructions[0][-1].kernel.params,
                         {'test_params': 'test'})
        self.assertEqual(converted_instruction.instructions[1][-1].channel, AcquireChannel(1))
    def test_parameterized_shift_frequency(self):
        """Test converted qobj from ShiftFrequency, with a parameterized frequency."""
        instruction = ShiftFrequency(8.0e9, DriveChannel(0))

        qobj = PulseQobjInstruction(name="shiftf",
                                    ch="d0",
                                    t0=1,
                                    frequency="f / 1000")
        self.assertTrue("frequency" in qobj.to_dict())

        converted_instruction = self.converter(qobj)
        self.assertIsInstance(converted_instruction, Schedule)

        bind_dict = {converted_instruction.get_parameters("f")[0]: 3.14}
        evaluated_instruction = converted_instruction.assign_parameters(
            bind_dict)

        instruction = ShiftFrequency(3.14e6, DriveChannel(0))

        self.assertEqual(evaluated_instruction.start_time, 1)
        self.assertEqual(evaluated_instruction.duration, 1)
        self.assertEqual(evaluated_instruction.instructions[0][-1],
                         instruction)
    def test_gaussian_square_pulse_instruction(self):
        """Test that parametric pulses are correctly converted to PulseQobjInstructions."""
        converter = InstructionToQobjConverter(PulseQobjInstruction,
                                               meas_level=2)
        instruction = Play(
            GaussianSquare(duration=1500,
                           sigma=15,
                           amp=-0.5 + 0.2j,
                           width=1300), MeasureChannel(1))

        valid_qobj = PulseQobjInstruction(
            name="parametric_pulse",
            pulse_shape="gaussian_square",
            ch="m1",
            t0=10,
            parameters={
                "duration": 1500,
                "sigma": 15,
                "amp": -0.5 + 0.2j,
                "width": 1300
            },
        )
        self.assertEqual(converter(10, instruction), valid_qobj)