def generate_schedule_blocks(): """Standard QPY testcase for schedule blocks.""" from qiskit.pulse import builder, channels, library from qiskit.utils import optionals # Parameterized schedule test is avoided. # Generated reference and loaded QPY object may induce parameter uuid mismatch. # As workaround, we need test with bounded parameters, however, schedule.parameters # are returned as Set and thus its order is random. # Since schedule parameters are validated, we cannot assign random numbers. # We need to upgrade testing framework. schedule_blocks = [] # Instructions without parameters with builder.build() as block: with builder.align_sequential(): builder.set_frequency(5e9, channels.DriveChannel(0)) builder.shift_frequency(10e6, channels.DriveChannel(1)) builder.set_phase(1.57, channels.DriveChannel(0)) builder.shift_phase(0.1, channels.DriveChannel(1)) builder.barrier(channels.DriveChannel(0), channels.DriveChannel(1)) builder.play(library.Gaussian(160, 0.1, 40), channels.DriveChannel(0)) builder.play(library.GaussianSquare(800, 0.1, 64, 544), channels.ControlChannel(0)) builder.play(library.Drag(160, 0.1, 40, 1.5), channels.DriveChannel(1)) builder.play(library.Constant(800, 0.1), channels.MeasureChannel(0)) builder.acquire(1000, channels.AcquireChannel(0), channels.MemorySlot(0)) schedule_blocks.append(block) # Raw symbolic pulse if optionals.HAS_SYMENGINE: import symengine as sym else: import sympy as sym duration, amp, t = sym.symbols("duration amp t") # pylint: disable=invalid-name expr = amp * sym.sin(2 * sym.pi * t / duration) my_pulse = library.SymbolicPulse( pulse_type="Sinusoidal", duration=100, parameters={"amp": 0.1}, envelope=expr, valid_amp_conditions=sym.Abs(amp) <= 1.0, ) with builder.build() as block: builder.play(my_pulse, channels.DriveChannel(0)) schedule_blocks.append(block) # Raw waveform my_waveform = 0.1 * np.sin(2 * np.pi * np.linspace(0, 1, 100)) with builder.build() as block: builder.play(my_waveform, channels.DriveChannel(0)) schedule_blocks.append(block) return schedule_blocks
def test_default(self): """Test basic SetPhase.""" set_phase = instructions.SetPhase(1.57, channels.DriveChannel(0)) self.assertIsInstance(set_phase.id, int) self.assertEqual(set_phase.name, None) self.assertEqual(set_phase.duration, 0) self.assertEqual(set_phase.phase, 1.57) self.assertEqual(set_phase.operands, (1.57, channels.DriveChannel(0))) self.assertEqual( set_phase, instructions.SetPhase(1.57, channels.DriveChannel(0), name="test")) self.assertNotEqual( set_phase, instructions.SetPhase(1.57j, channels.DriveChannel(0), name="test")) self.assertEqual(repr(set_phase), "SetPhase(1.57, DriveChannel(0))")
def test_freq(self): """Test set frequency basic functionality.""" set_freq = instructions.SetFrequency(4.5e9, channels.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, channels.DriveChannel(1))) self.assertEqual(set_freq, instructions.SetFrequency(4.5e9, channels.DriveChannel(1), name='test')) self.assertNotEqual(set_freq, instructions.SetFrequency(4.5e8, channels.DriveChannel(1), name='test')) self.assertEqual(repr(set_freq), "SetFrequency(4500000000.0, DriveChannel(1), name='test')")
def test_delay(self): """Test delay.""" delay = instructions.Delay(10, channels.DriveChannel(0), name='test_name') self.assertIsInstance(delay.id, int) self.assertEqual(delay.name, 'test_name') self.assertEqual(delay.duration, 10) self.assertIsInstance(delay.duration, int) self.assertEqual(delay.operands, (10, channels.DriveChannel(0))) self.assertEqual(delay, instructions.Delay(10, channels.DriveChannel(0))) self.assertNotEqual(delay, instructions.Delay(11, channels.DriveChannel(1))) self.assertEqual(repr(delay), "Delay(10, DriveChannel(0), name='test_name')") # Test numpy int for duration delay = instructions.Delay(np.int32(10), channels.DriveChannel(0), name='test_name2') self.assertEqual(delay.duration, 10) self.assertIsInstance(delay.duration, np.integer)
def test_default(self): """Test default snapshot.""" snapshot = instructions.Snapshot(label='test_name', snapshot_type='state') self.assertIsInstance(snapshot.id, int) self.assertEqual(snapshot.name, 'test_name') self.assertEqual(snapshot.type, 'state') self.assertEqual(snapshot.duration, 0) self.assertNotEqual(snapshot, instructions.Delay(10, channels.DriveChannel(0))) self.assertEqual(repr(snapshot), "Snapshot(test_name, state, name='test_name')")
def test_play(self): """Test basic play instruction.""" play = instructions.Play(self.pulse_op, channels.DriveChannel(1)) self.assertIsInstance(play.id, int) self.assertEqual(play.name, self.pulse_op.name) self.assertEqual(play.duration, self.duration) self.assertEqual(repr(play), "Play(Waveform(array([1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]), name='test')," " DriveChannel(1), name='test')")
def test_play(self): """Test basic play instruction.""" duration = 4 pulse = library.Waveform([1.0] * duration, name='test') play = instructions.Play(pulse, channels.DriveChannel(1)) self.assertIsInstance(play.id, int) self.assertEqual(play.name, pulse.name) self.assertEqual(play.duration, duration) self.assertEqual(repr(play), "Play(Waveform(array([1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]), name='test')," " DriveChannel(1), name='test')")
def test_relative_barrier(self): """Test the relative barrier directive.""" a0 = channels.AcquireChannel(0) d0 = channels.DriveChannel(0) m0 = channels.MeasureChannel(0) u0 = channels.ControlChannel(0) mem0 = channels.MemorySlot(0) reg0 = channels.RegisterSlot(0) chans = (a0, d0, m0, u0, mem0, reg0) name = "barrier" barrier = instructions.RelativeBarrier(*chans, name=name) self.assertEqual(barrier.name, name) self.assertEqual(barrier.duration, 0) self.assertEqual(barrier.channels, chans) self.assertEqual(barrier.operands, chans)
def get_channel(self, channel): """Parse and retrieve channel from ch string. Args: channel (str): Channel to match Returns: (Channel, int): Matched channel Raises: PulseError: Is raised if valid channel is not matched """ match = self.chan_regex.match(channel) if match: prefix, index = match.group(1), int(match.group(2)) if prefix == channels.DriveChannel.prefix: return channels.DriveChannel(index) elif prefix == channels.MeasureChannel.prefix: return channels.MeasureChannel(index) elif prefix == channels.ControlChannel.prefix: return channels.ControlChannel(index) raise PulseError('Channel %s is not valid' % channel)