def test_from_sequence(self): dwf = DummyWaveform(duration=1.1, defined_channels={'A'}) self.assertIs(dwf, SequenceWaveform.from_sequence((dwf,))) swf1 = SequenceWaveform.from_sequence((dwf, dwf)) swf2 = SequenceWaveform.from_sequence((swf1, dwf)) assert_constant_consistent(self, swf1) assert_constant_consistent(self, swf2) self.assertEqual(3*(dwf,), swf2.sequenced_waveforms) cwf_2_a = ConstantWaveform(duration=1.1, amplitude=2.2, channel='A') cwf_3 = ConstantWaveform(duration=1.1, amplitude=3.3, channel='A') cwf_2_b = ConstantWaveform(duration=1.1, amplitude=2.2, channel='A') with mock.patch.object(ConstantWaveform, 'from_mapping', return_value=mock.sentinel) as from_mapping: new_constant = SequenceWaveform.from_sequence((cwf_2_a, cwf_2_b)) self.assertIs(from_mapping.return_value, new_constant) from_mapping.assert_called_once_with(2*TimeType.from_float(1.1), {'A': 2.2}) swf3 = SequenceWaveform.from_sequence((cwf_2_a, dwf)) self.assertEqual((cwf_2_a, dwf), swf3.sequenced_waveforms) self.assertIsNone(swf3.constant_value('A')) assert_constant_consistent(self, swf3) swf3 = SequenceWaveform.from_sequence((cwf_2_a, cwf_3)) self.assertEqual((cwf_2_a, cwf_3), swf3.sequenced_waveforms) self.assertIsNone(swf3.constant_value('A')) assert_constant_consistent(self, swf3)
def test_from_mapping(self): from_single = ConstantWaveform.from_mapping(1., {'A': 2.}) expected_single = ConstantWaveform(duration=1., amplitude=2., channel='A') self.assertEqual(expected_single, from_single) from_multi = ConstantWaveform.from_mapping(1., {'A': 2., 'B': 3.}) expected_from_multi = MultiChannelWaveform([ConstantWaveform(duration=1., amplitude=2., channel='A'), ConstantWaveform(duration=1., amplitude=3., channel='B')]) self.assertEqual(expected_from_multi, from_multi)
def test_upload_exceptions(self): wv = ConstantWaveform(channel=1, duration=192, amplitude=0.1) channel_pair = TaborChannelPair(self.instrument, identifier='asd', channels=(1, 2)) program = Loop(waveform=wv) with self.assertRaises(ValueError): channel_pair.upload('test', program, (1, 2, 3), (5, 6), (lambda x: x, lambda x: x)) with self.assertRaises(ValueError): channel_pair.upload('test', program, (1, 2), (5, 6, 'a'), (lambda x: x, lambda x: x)) with self.assertRaises(ValueError): channel_pair.upload('test', program, (1, 2), (3, 4), (lambda x: x, )) old = channel_pair._amplitude_offset_handling with self.assertRaises(ValueError): channel_pair._amplitude_offset_handling = 'invalid' channel_pair.upload('test', program, (1, None), (None, None), (lambda x: x, lambda x: x)) channel_pair._amplitude_offset_handling = old channel_pair._known_programs['test'] = TaborProgramMemory( np.array([0]), None) with self.assertRaises(ValueError): channel_pair.upload('test', program, (1, 2), (3, 4), (lambda x: x, lambda x: x))
def test_waveform_sample(self): waveform = ConstantWaveform(10, .1, 'P1') sample_times = [-1, 0, 1, 2] result = waveform.unsafe_sample('P1', sample_times) self.assertTrue(np.all(result == .1)) self.assertIs(waveform, waveform.unsafe_get_subset_for_channels({'A'}))
def test_from_table(self): expected = ConstantWaveform(0.1, 0.2, 'A') for interp in (HoldInterpolationStrategy(), JumpInterpolationStrategy(), LinearInterpolationStrategy()): wf = TableWaveform.from_table('A', [TableWaveformEntry(0.0, 0.2, interp), TableWaveformEntry(0.1, 0.2, interp)]) self.assertEqual(expected, wf)
def test_from_repetition_count(self): dwf = DummyWaveform() self.assertEqual(RepetitionWaveform(dwf, 3), RepetitionWaveform.from_repetition_count(dwf, 3)) cwf = ConstantWaveform(duration=3, amplitude=2.2, channel='A') with mock.patch.object(ConstantWaveform, 'from_mapping', return_value=mock.sentinel) as from_mapping: self.assertIs(from_mapping.return_value, RepetitionWaveform.from_repetition_count(cwf, 5)) from_mapping.assert_called_once_with(15, {'A': 2.2})
def test_constant_default_impl(self): wf_non_const_a = DummyWaveform(defined_channels={'A'}, duration=3) wf_non_const_b = DummyWaveform(defined_channels={'B'}, duration=3) wf_const_c = ConstantWaveform(channel='C', amplitude=2.2, duration=3) wf_const_d = ConstantWaveform(channel='D', amplitude=3.3, duration=3) wf_const = MultiChannelWaveform.from_parallel((wf_const_c, wf_const_d)) wf_non_const = MultiChannelWaveform.from_parallel((wf_non_const_b, wf_non_const_a)) wf_mixed = MultiChannelWaveform.from_parallel((wf_non_const_a, wf_const_c)) assert_constant_consistent(self, wf_const) assert_constant_consistent(self, wf_non_const) assert_constant_consistent(self, wf_mixed) self.assertEqual(wf_const.constant_value_dict(), {'C': 2.2, 'D': 3.3}) self.assertIsNone(wf_non_const.constant_value_dict()) self.assertIsNone(wf_mixed.constant_value_dict()) self.assertEqual(wf_mixed.constant_value('C'), 2.2)
def test_construction(self): with self.assertRaises(ValueError): FunctionWaveform(ExpressionScalar('sin(omega*t)'), duration=5, channel='A') const = FunctionWaveform.from_expression(ExpressionScalar('4.'), duration=5, channel='A') expected_const = ConstantWaveform(duration=5, amplitude=4., channel='A') self.assertEqual(expected_const, const) linear = FunctionWaveform.from_expression(ExpressionScalar('4.*t'), 5, 'A') expected_linear = FunctionWaveform(ExpressionScalar('4.*t'), 5, 'A') self.assertEqual(expected_linear, linear)
def build_waveform( self, parameters: Dict[str, numbers.Real], channel_mapping: Dict[ChannelID, Optional[ChannelID]] ) -> Optional[Union[ConstantWaveform, MultiChannelWaveform]]: logging.debug( f'build_waveform of ConstantPulse: channel_mapping {channel_mapping}, defined_channels {self.defined_channels} ' ) if all( channel_mapping.get(channel, None) is None for channel in self.defined_channels): return None waveforms = [ ConstantWaveform(self.duration, amplitude, channel) for channel, amplitude in self._amplitude_dict.items() if channel_mapping[channel] is not None ] if len(waveforms) == 1: return waveforms.pop() else: return MultiChannelWaveform(waveforms)
def test_upload_offset_handling(self): program = Loop( waveform=ConstantWaveform(channel=1, duration=192, amplitude=0.1)) channel_pair = TaborChannelPair(self.instrument, identifier='asd', channels=(1, 2)) channels = (1, None) markers = (None, None) tabor_program_kwargs = dict( channels=channels, markers=markers, device_properties=channel_pair.device.dev_properties) amplitudes = (0.5, 0.3) test_sample_rate = TimeType.from_fraction(1, 1) test_amplitudes = (0.5 / 2, 0.3 / 2) test_offset = 0.1 test_transform = (lambda x: x, lambda x: x) with patch('qupulse.hardware.awgs.tabor.TaborProgram', wraps=TaborProgram) as tabor_program_mock: with patch.object(self.instrument, 'offset', return_value=test_offset) as offset_mock: tabor_program_mock.get_sampled_segments = mock.Mock( wraps=tabor_program_mock.get_sampled_segments) self.instrument.amplitude = mock.Mock(side_effect=amplitudes) self.instrument.sample_rate = mock.Mock(return_value=10**9) channel_pair.amplitude_offset_handling = AWGAmplitudeOffsetHandling.CONSIDER_OFFSET channel_pair.upload('test1', program, channels, markers, test_transform) tabor_program_mock.assert_called_once_with( program, **tabor_program_kwargs, sample_rate=test_sample_rate, amplitudes=test_amplitudes, offsets=(test_offset, test_offset), voltage_transformations=test_transform) self.assertEqual([mock.call(1), mock.call(2)], offset_mock.call_args_list) offset_mock.reset_mock() tabor_program_mock.reset_mock() self.instrument.amplitude = mock.Mock(side_effect=amplitudes) self.instrument.sample_rate = mock.Mock(return_value=10**9) channel_pair.amplitude_offset_handling = AWGAmplitudeOffsetHandling.IGNORE_OFFSET channel_pair.upload('test2', program, (1, None), (None, None), test_transform) tabor_program_mock.assert_called_once_with( program, **tabor_program_kwargs, sample_rate=test_sample_rate, amplitudes=test_amplitudes, offsets=(0., 0.), voltage_transformations=test_transform) self.assertEqual([], offset_mock.call_args_list)
def test_waveform_sample(self): waveform = ConstantWaveform(10, .1, 'P1') sample_times = [-1, 0, 1, 2] result = waveform.unsafe_sample('P1', sample_times) self.assertTrue(np.all(result == .1))
def test_waveform_duration(self): waveform = ConstantWaveform(10, 1., 'P1') self.assertEqual(waveform.duration, 10)
def test_constness(self): waveform = ConstantWaveform(10, .1, 'P1') self.assertTrue(waveform.is_constant()) assert_constant_consistent(self, waveform)
def test_repr(self): cwf_2_a = ConstantWaveform(duration=1.1, amplitude=2.2, channel='A') cwf_3 = ConstantWaveform(duration=1.1, amplitude=3.3, channel='A') swf = SequenceWaveform([cwf_2_a, cwf_3]) r = repr(swf) self.assertEqual(swf, eval(r))
def test_repr(self): body_wf = ConstantWaveform(amplitude=1.1, duration=1.3, channel='3') wf = RepetitionWaveform(body_wf, 3) r = repr(wf) self.assertEqual(wf, eval(r))
def test_const_value(self): mixed_wf = MultiChannelWaveform([DummyWaveform(1.5, defined_channels={'A'}), ConstantWaveform(1.5, 1.1, 'B')]) wf = FunctorWaveform(mixed_wf, {'A': np.negative, 'B': np.negative}) self.assertIsNone(wf.constant_value('A')) self.assertEqual(-1.1, wf.constant_value('B'))