def test_freq(self): """Test set frequency basic functionality.""" set_freq = SetFrequency(4.5e9, DriveChannel(1), name='test') self.assertIsInstance(set_freq.id, int) self.assertEqual(set_freq.duration, 0) self.assertEqual(set_freq.frequency, 4.5e9) self.assertEqual(set_freq.operands, (4.5e9, DriveChannel(1))) self.assertEqual(set_freq, SetFrequency(4.5e9, DriveChannel(1), name='test')) self.assertNotEqual(set_freq, SetFrequency(4.5e8, DriveChannel(1), name='test')) self.assertEqual(repr(set_freq), "SetFrequency(4500000000.0, DriveChannel(1), name='test')")
def test_filter_inst_types(self): """Test filtering on instruction types.""" lp0 = self.linear(duration=3, slope=0.2, intercept=0.1) sched = Schedule(name="fake_experiment") sched = sched.insert(0, Play(lp0, self.config.drive(0))) sched = sched.insert(10, Play(lp0, self.config.drive(1))) sched = sched.insert(30, ShiftPhase(-1.57, self.config.drive(0))) sched = sched.insert(40, SetFrequency(8.0, self.config.drive(0))) sched = sched.insert(50, ShiftFrequency(4.0e6, self.config.drive(0))) sched = sched.insert(55, SetPhase(3.14, self.config.drive(0))) for i in range(2): sched = sched.insert( 60, Acquire(5, self.config.acquire(i), MemorySlot(i))) sched = sched.insert(90, Play(lp0, self.config.drive(0))) # test on Acquire only_acquire, no_acquire = self._filter_and_test_consistency( sched, instruction_types=[Acquire]) for _, inst in only_acquire.instructions: self.assertIsInstance(inst, Acquire) for _, inst in no_acquire.instructions: self.assertFalse(isinstance(inst, Acquire)) # test two instruction types only_pulse_and_fc, no_pulse_and_fc = self._filter_and_test_consistency( sched, instruction_types=[Play, ShiftPhase]) for _, inst in only_pulse_and_fc.instructions: self.assertIsInstance(inst, (Play, ShiftPhase)) for _, inst in no_pulse_and_fc.instructions: self.assertFalse(isinstance(inst, (Play, ShiftPhase))) self.assertEqual(len(only_pulse_and_fc.instructions), 4) self.assertEqual(len(no_pulse_and_fc.instructions), 5) # test on ShiftPhase only_fc, no_fc = self._filter_and_test_consistency( sched, instruction_types={ShiftPhase}) self.assertEqual(len(only_fc.instructions), 1) self.assertEqual(len(no_fc.instructions), 8) # test on SetPhase only_setp, no_setp = self._filter_and_test_consistency( sched, instruction_types={SetPhase}) self.assertEqual(len(only_setp.instructions), 1) self.assertEqual(len(no_setp.instructions), 8) # test on SetFrequency only_setf, no_setf = self._filter_and_test_consistency( sched, instruction_types=[SetFrequency]) for _, inst in only_setf.instructions: self.assertTrue(isinstance(inst, SetFrequency)) self.assertEqual(len(only_setf.instructions), 1) self.assertEqual(len(no_setf.instructions), 8) # test on ShiftFrequency only_shiftf, no_shiftf = self._filter_and_test_consistency( sched, instruction_types=[ShiftFrequency]) for _, inst in only_shiftf.instructions: self.assertTrue(isinstance(inst, ShiftFrequency)) self.assertEqual(len(only_shiftf.instructions), 1) self.assertEqual(len(no_shiftf.instructions), 8)
def pulsemaker_to_qiskit(instructions): '''Translate pulsemaker to qiskit-schedule format''' try: from qiskit import pulse from qiskit.pulse import Schedule, Play, DriveChannel, ControlChannel, Waveform, ShiftPhase, SetFrequency except ImportError: return Schedule() schedule = Schedule() for c, data in instructions.items(): channel_type = c[0] channel_index = int(c[1:]) if channel_type == 'd': channel = DriveChannel(channel_index) elif channel_type == 'u': channel = ControlChannel(channel_index) current_sample_count = 0 for type, payload in data: if type == InstructionType.PULSE: sample_count = len(payload) if sample_count == 1: continue schedule |= Play(Waveform(payload), channel).shift(current_sample_count) current_sample_count += sample_count elif type == InstructionType.PHASE: schedule |= ShiftPhase(payload, channel).shift(current_sample_count) elif type == InstructionType.FREQ: schedule |= SetFrequency(payload, channel).shift(current_sample_count) return schedule
def test_set_frequency(self): """Test that SetFrequency is properly converted.""" sched = Schedule() sched += SetFrequency(4.0, DriveChannel(0)) sched += Play(Constant(duration=10, amp=1.0), DriveChannel(0)) converter = InstructionToSignals(dt=0.222, carriers=[5.0]) signals = converter.get_signals(sched) for idx in range(10): self.assertEqual(signals[0].samples[idx], np.exp(2.0j * idx * np.pi * -1.0 * 0.222))
def test_filter_inst_types(self): """Test filtering on instruction types.""" lp0 = self.linear(duration=3, slope=0.2, intercept=0.1) acquire = Acquire(5) sched = Schedule(name='fake_experiment') sched = sched.insert(0, lp0(self.config.drive(0))) sched = sched.insert(10, lp0(self.config.drive(1))) sched = sched.insert(30, ShiftPhase(-1.57, self.config.drive(0))) sched = sched.insert(40, SetFrequency(8.0, self.config.drive(0))) for i in range(2): sched = sched.insert( 60, acquire(self.config.acquire(i), MemorySlot(i))) sched = sched.insert(90, lp0(self.config.drive(0))) # test on Acquire only_acquire, no_acquire = \ self._filter_and_test_consistency(sched, instruction_types=[Acquire]) for _, inst in only_acquire.instructions: self.assertIsInstance(inst, Acquire) for _, inst in no_acquire.instructions: self.assertFalse(isinstance(inst, Acquire)) # test two instruction types only_pulse_and_fc, no_pulse_and_fc = \ self._filter_and_test_consistency(sched, instruction_types=[PulseInstruction, ShiftPhase]) for _, inst in only_pulse_and_fc.instructions: self.assertIsInstance(inst, (PulseInstruction, ShiftPhase)) for _, inst in no_pulse_and_fc.instructions: self.assertFalse(isinstance(inst, (PulseInstruction, ShiftPhase))) self.assertEqual(len(only_pulse_and_fc.instructions), 4) self.assertEqual(len(no_pulse_and_fc.instructions), 3) # test on ShiftPhase only_fc, no_fc = \ self._filter_and_test_consistency(sched, instruction_types={ShiftPhase}) self.assertEqual(len(only_fc.instructions), 1) self.assertEqual(len(no_fc.instructions), 6) # test on SetFrequency only_sf, no_sf = \ self._filter_and_test_consistency(sched, instruction_types=[SetFrequency]) for _, inst in only_sf.instructions: self.assertTrue(isinstance(inst, SetFrequency)) self.assertEqual(len(only_sf.instructions), 1) self.assertEqual(len(no_sf.instructions), 6)