def test_acquire(self): """Test converted qobj from AcquireInstruction.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) instruction = Acquire(10, AcquireChannel(0), MemorySlot(0), RegisterSlot(0)) valid_qobj = PulseQobjInstruction(name='acquire', t0=0, duration=10, qubits=[0], memory_slot=[0], register_slot=[0]) self.assertEqual(converter(0, instruction), valid_qobj) # without register instruction = Acquire(10, AcquireChannel(0), MemorySlot(0)) valid_qobj = PulseQobjInstruction(name='acquire', t0=0, duration=10, qubits=[0], memory_slot=[0]) self.assertEqual(converter(0, instruction), valid_qobj)
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_sequenced_parameterized_schedule(self): """Test parameterized schedule consists of multiple instruction.""" converter = QobjToInstructionConverter([], buffer=0) qobjs = [ PulseQobjInstruction(name="fc", ch="d0", t0=10, phase="P1"), PulseQobjInstruction(name="fc", ch="d0", t0=20, phase="P2"), PulseQobjInstruction(name="fc", ch="d0", t0=30, phase="P3"), ] converted_instruction = [converter(qobj) for qobj in qobjs] inst_map = InstructionScheduleMap() inst_map.add("inst_seq", 0, Schedule(*converted_instruction, name="inst_seq")) with self.assertRaises(PulseError): inst_map.get("inst_seq", 0, P1=1, P2=2, P3=3, P4=4, P5=5) with self.assertRaises(PulseError): inst_map.get("inst_seq", 0, 1, 2, 3, 4, 5, 6, 7, 8) p3_expr = Parameter("p3") p3_expr = p3_expr.bind({p3_expr: 3}) sched = inst_map.get("inst_seq", 0, 1, 2, p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3) sched = inst_map.get("inst_seq", 0, P1=1, P2=2, P3=p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3) sched = inst_map.get("inst_seq", 0, 1, 2, P3=p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3)
def test_sequenced_parameterized_schedule(self): """Test parametrized schedule consist of multiple instruction. """ cmd_def = CmdDef() converter = QobjToInstructionConverter([]) qobjs = [ PulseQobjInstruction(name='fc', ch='d0', t0=10, phase='P1'), PulseQobjInstruction(name='fc', ch='d0', t0=20, phase='P2'), PulseQobjInstruction(name='fc', ch='d0', t0=30, phase='P3') ] converted_instruction = [converter(qobj) for qobj in qobjs] cmd_def.add( 'inst_seq', 0, ParameterizedSchedule(*converted_instruction, name='inst_seq')) with self.assertRaises(PulseError): cmd_def.get('inst_seq', 0, P1=1, P2=2, P3=3, P4=4, P5=5) with self.assertRaises(PulseError): cmd_def.get('inst_seq', 0, P1=1) with self.assertRaises(PulseError): cmd_def.get('inst_seq', 0, 1, 2, 3, P1=1) sched = cmd_def.get('inst_seq', 0, 1, 2, 3) self.assertEqual(sched.instructions[0][-1].command.phase, 1) self.assertEqual(sched.instructions[1][-1].command.phase, 2) self.assertEqual(sched.instructions[2][-1].command.phase, 3) sched = cmd_def.get('inst_seq', 0, P1=1, P2=2, P3=3) self.assertEqual(sched.instructions[0][-1].command.phase, 1) self.assertEqual(sched.instructions[1][-1].command.phase, 2) self.assertEqual(sched.instructions[2][-1].command.phase, 3) sched = cmd_def.get('inst_seq', 0, 1, 2, P3=3) self.assertEqual(sched.instructions[0][-1].command.phase, 1) self.assertEqual(sched.instructions[1][-1].command.phase, 2) self.assertEqual(sched.instructions[2][-1].command.phase, 3)
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_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)
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_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)
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)
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(0, instruction), valid_qobj)
def test_snapshot(self): """Test converted qobj from SnapShot.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) instruction = Snapshot(label='label', snapshot_type='type', name='snapshot') valid_qobj = PulseQobjInstruction(name='snapshot', t0=0, label='label', type='type') self.assertEqual(converter(0, instruction), valid_qobj)
def test_snapshot(self): """Test converted qobj from SnapShot.""" cmd = Snapshot(name='label', snap_type='type') instruction = cmd << 10 qobj = PulseQobjInstruction(name='snapshot', t0=10, label='label', type='type') converted_instruction = self.converter(qobj) self.assertEqual(converted_instruction.timeslots, instruction.timeslots) self.assertEqual(converted_instruction.instructions[0][-1], cmd)
def test_persistent_value(self): """Test converted qobj from PersistentValueInstruction.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) command = PersistentValue(value=0.1j) instruction = command(self.device.q[0].drive) valid_qobj = PulseQobjInstruction( name='pv', ch='d0', t0=0, val=0.1j ) self.assertEqual(converter(0, instruction), valid_qobj)
def test_frame_change(self): """Test converted qobj from FrameChangeInstruction.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) command = FrameChange(phase=0.1) instruction = command(self.device.q[0].drive) valid_qobj = PulseQobjInstruction( name='fc', ch='d0', t0=0, phase=0.1 ) self.assertEqual(converter(0, instruction), valid_qobj)
def test_parameterized_persistent_value(self): """Test converted qobj from PersistentValueInstruction.""" cmd = PersistentValue(value=0.5+0.j) 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) 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_deprecated_drive_instruction(self): """Test converted qobj from PulseInstruction.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) command = SamplePulse(np.arange(0, 0.01), name='linear') with self.assertWarns(DeprecationWarning): instruction = command(DriveChannel(0)) valid_qobj = PulseQobjInstruction( name='linear', ch='d0', t0=0 ) self.assertEqual(converter(0, instruction), valid_qobj)
def test_parameterized_frame_change(self): """Test converted qobj from FrameChangeInstruction.""" 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)
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, P1='0', P2=-1) self.assertEqual(sched.instructions[0][-1].command.value, -1)
def test_parameterized_set_phase(self): """Test converted qobj from SetPhase, with parameterized phase.""" qobj = PulseQobjInstruction(name="setp", ch="m0", t0=0, phase="p/2") converted_instruction = self.converter(qobj) self.assertIsInstance(converted_instruction, Schedule) bind_dict = {converted_instruction.get_parameters("p")[0]: 3.14} evaluated_instruction = converted_instruction.assign_parameters( bind_dict) instruction = SetPhase(3.14 / 2, MeasureChannel(0)) self.assertEqual(evaluated_instruction.start_time, 0) self.assertEqual(evaluated_instruction.duration, 0) self.assertEqual(evaluated_instruction.instructions[0][-1], instruction)
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)
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)
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)
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')
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_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)
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)
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)
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_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.start_time, instruction.start_time) self.assertEqual(evaluated_instruction.duration, instruction.duration) self.assertEqual(evaluated_instruction.instructions[0][-1].command, cmd)
def from_dict(cls, data): """Create a new Command object from a dictionary. Args: data (dict): A dictionary representing the ``Command`` to create. It will be in the same format as output by :meth:`to_dict`. Returns: qiskit.providers.model.Command: The ``Command`` from the input dictionary. """ in_data = copy.copy(data) if "sequence" in in_data: in_data["sequence"] = [ PulseQobjInstruction.from_dict(x) for x in in_data.pop("sequence") ] return cls(**in_data)