def test_repeat_extern_no_advance(self): node = mock.Mock() node.to_source_code = mock.Mock(return_value=['asd', 'jkl']) node._get_single_indexed_playback = mock.Mock(return_value=None) node.samples = mock.Mock(return_value=64) repeat = Repeat(12, node) body_prefix = self.line_prefix + repeat.INDENTATION expected = [' repeat(12) {', 'asd', 'jkl', ' }'] result_no_advance = list( repeat.to_source_code(self.waveform_manager, node_name_generator=self.node_name_generator, line_prefix=self.line_prefix, pos_var_name=self.pos_var_name, advance_pos_var=False)) self.assertEqual(expected, result_no_advance) node.to_source_code.assert_called_once_with( self.waveform_manager, node_name_generator=self.node_name_generator, line_prefix=body_prefix, pos_var_name=self.pos_var_name, advance_pos_var=False) node._get_single_indexed_playback.assert_not_called() node.samples.assert_not_called()
def test_loop_to_seqc_leaf(self): """Test the translation of leaves""" # we use None because it is not used in this test user_registers = None wf = DummyWaveform(duration=32) loop = Loop(waveform=wf) # with wrapping repetition loop.repetition_count = 15 waveform_to_bin = mock.Mock(wraps=make_binary_waveform) expected = Repeat(loop.repetition_count, WaveformPlayback(waveform=make_binary_waveform(wf))) result = loop_to_seqc(loop, 1, 1, waveform_to_bin, user_registers=user_registers) waveform_to_bin.assert_called_once_with(wf) self.assertEqual(expected, result) # without wrapping repetition loop.repetition_count = 1 waveform_to_bin = mock.Mock(wraps=make_binary_waveform) expected = WaveformPlayback(waveform=make_binary_waveform(wf)) result = loop_to_seqc(loop, 1, 1, waveform_to_bin, user_registers=user_registers) waveform_to_bin.assert_called_once_with(wf) self.assertEqual(expected, result)
def test_loop_to_seqc_len_1(self): """Test the translation of loops with len(loop) == 1""" loop = Loop(children=[Loop()]) waveform_to_bin = mock.Mock(wraps=make_binary_waveform) loop_to_seqc_kwargs = dict(min_repetitions_for_for_loop=2, min_repetitions_for_shared_wf=3, waveform_to_bin=waveform_to_bin) expected = 'asdf' with mock.patch('qupulse._program.seqc.loop_to_seqc', return_value=expected) as mocked_loop_to_seqc: result = loop_to_seqc(loop, **loop_to_seqc_kwargs) self.assertEqual(result, expected) mocked_loop_to_seqc.assert_called_once_with( loop[0], **loop_to_seqc_kwargs) loop.repetition_count = 14 expected = Repeat(14, 'asdfg') with mock.patch('qupulse._program.seqc.loop_to_seqc', return_value=expected.scope) as mocked_loop_to_seqc: result = loop_to_seqc(loop, **loop_to_seqc_kwargs) self.assertEqual(result, expected) mocked_loop_to_seqc.assert_called_once_with( loop[0], **loop_to_seqc_kwargs) waveform_to_bin.assert_not_called()
def test_loop_to_seqc_cluster_handling(self): """Test handling of clusters""" with self.assertRaises(AssertionError): loop_to_seqc(Loop(repetition_count=12, children=[Loop()]), min_repetitions_for_for_loop=3, min_repetitions_for_shared_wf=2, waveform_to_bin=make_binary_waveform) loop_to_seqc_kwargs = dict(min_repetitions_for_for_loop=3, min_repetitions_for_shared_wf=4, waveform_to_bin=make_binary_waveform) wf_same = map(WaveformPlayback, map(make_binary_waveform, get_unique_wfs(100000, 32))) wf_sep, = map(WaveformPlayback, map(make_binary_waveform, get_unique_wfs(1, 64))) node_clusters = [ take(2, wf_same), [wf_sep], take(3, wf_same), [wf_sep], take(4, wf_same), take(4, wf_same) ] root = Loop( repetition_count=12, children=[Loop() for _ in range(2 + 1 + 3 + 1 + 4 + 1 + 4)]) expected = Repeat( 12, Scope([ *node_clusters[0], wf_sep, SteppingRepeat(node_clusters[2]), wf_sep, SteppingRepeat(node_clusters[4]), SteppingRepeat(node_clusters[5]) ])) def dummy_find_sharable_waveforms(cluster): if cluster is node_clusters[4]: return [True] else: return None p1 = mock.patch('qupulse._program.seqc.to_node_clusters', return_value=node_clusters) p2 = mock.patch('qupulse._program.seqc.find_sharable_waveforms', wraps=dummy_find_sharable_waveforms) p3 = mock.patch('qupulse._program.seqc.mark_sharable_waveforms') with p1 as to_node_clusters_mock, p2 as find_share_mock, p3 as mark_share_mock: result = loop_to_seqc(root, **loop_to_seqc_kwargs) self.assertEqual(expected, result) to_node_clusters_mock.assert_called_once_with( root, loop_to_seqc_kwargs) self.assertEqual( find_share_mock.mock_calls, [mock.call(node_clusters[4]), mock.call(node_clusters[5])]) mark_share_mock.assert_called_once_with(node_clusters[4], [True])
def test_iter_waveform_playback(self): wf1, wf2 = map(WaveformPlayback, map(make_binary_waveform, get_unique_wfs(2, 32))) wf3, wf_shared = map(WaveformPlayback, map(make_binary_waveform, get_unique_wfs(2, 64))) for wf in (wf1, wf2, wf3, wf_shared): pb, = wf.iter_waveform_playbacks() self.assertIs(pb, wf) repeat = Repeat(13, wf1) self.assertEqual(list(repeat.iter_waveform_playbacks()), [wf1]) scope = Scope([wf1, repeat, wf2, wf3, wf_shared]) self.assertEqual(list(scope.iter_waveform_playbacks()), [wf1, wf1, wf2, wf3, wf_shared]) stepping_repeat = SteppingRepeat([wf1, repeat, wf2, wf3, wf_shared]) self.assertEqual(list(stepping_repeat.iter_waveform_playbacks()), [wf1, wf1, wf2, wf3, wf_shared])
def test_visit_nodes(self): wf, wf_shared = map(WaveformPlayback, map(make_binary_waveform, get_unique_wfs(2))) wf_shared.shared = True waveform_manager = mock.Mock(wraps=DummyWfManager()) wf._visit_nodes(waveform_manager) waveform_manager.request_concatenated.assert_called_once_with( wf.waveform) waveform_manager = mock.Mock(wraps=DummyWfManager()) wf_shared._visit_nodes(waveform_manager) waveform_manager.request_concatenated.assert_not_called() scope = Scope([mock.Mock(wraps=wf), mock.Mock(wraps=wf_shared)]) scope._visit_nodes(waveform_manager) scope.nodes[0]._visit_nodes.assert_called_once_with(waveform_manager) scope.nodes[1]._visit_nodes.assert_called_once_with(waveform_manager) waveform_manager.request_concatenated.assert_called_once_with( wf.waveform) waveform_manager = mock.Mock(wraps=DummyWfManager()) repeat = Repeat(12, mock.Mock(wraps=wf)) repeat._visit_nodes(waveform_manager) repeat.scope._visit_nodes.assert_called_once_with(waveform_manager) waveform_manager.request_concatenated.assert_called_once_with( wf.waveform) waveform_manager = mock.Mock(wraps=DummyWfManager()) stepping_repeat = SteppingRepeat( [mock.Mock(wraps=wf), mock.Mock(wraps=wf), mock.Mock(wraps=wf)]) stepping_repeat._visit_nodes(waveform_manager) for node in stepping_repeat.node_cluster: node._visit_nodes.assert_called_once_with(waveform_manager)
def test_same_stepping(self): wf1, wf2 = map(WaveformPlayback, map(make_binary_waveform, get_unique_wfs(2, 32))) wf3, wf_shared = map(WaveformPlayback, map(make_binary_waveform, get_unique_wfs(2, 64))) wf_shared.shared = True scope1 = Scope([wf1, wf1, wf2]) scope2 = Scope([wf1, wf2, wf2]) scope3 = Scope([wf1, wf2, wf3]) scope4 = Scope([wf1, wf2, wf2, wf2]) repeat1 = Repeat(13, wf1) repeat2 = Repeat(13, wf2) repeat3 = Repeat(15, wf2) repeat4 = Repeat(13, wf3) stepping_repeat1 = SteppingRepeat([wf1, wf1, wf2]) stepping_repeat2 = SteppingRepeat([wf2, wf2, wf2]) stepping_repeat3 = SteppingRepeat([wf3, wf3, wf3]) stepping_repeat4 = SteppingRepeat([wf1, wf1, wf2, wf1]) self.assertTrue(wf1.same_stepping(wf1)) self.assertTrue(wf1.same_stepping(wf2)) self.assertFalse(wf1.same_stepping(wf3)) self.assertFalse(wf3.same_stepping(wf_shared)) self.assertFalse(wf_shared.same_stepping(wf3)) self.assertFalse(scope1.same_stepping(wf1)) self.assertTrue(scope1.same_stepping(scope2)) self.assertFalse(scope1.same_stepping(scope3)) self.assertFalse(scope1.same_stepping(scope4)) self.assertFalse(repeat1.same_stepping(scope1)) self.assertTrue(repeat1.same_stepping(repeat2)) self.assertFalse(repeat1.same_stepping(repeat3)) self.assertFalse(repeat1.same_stepping(repeat4)) self.assertFalse(stepping_repeat1.same_stepping(scope1)) self.assertTrue(stepping_repeat1.same_stepping(stepping_repeat2)) self.assertFalse(stepping_repeat1.same_stepping(stepping_repeat3)) self.assertFalse(stepping_repeat1.same_stepping(stepping_repeat4))
def complex_program_as_seqc(unique_wfs, wf_same): return Repeat( 12, Scope([ SteppingRepeat([ Repeat(repetition_count=10, scope=Scope([ Repeat( 42, WaveformPlayback( make_binary_waveform(unique_wf))), Repeat( 98, WaveformPlayback(make_binary_waveform(wf_same), shared=True)), ])) for unique_wf in unique_wfs ]), Repeat(21, WaveformPlayback(make_binary_waveform(unique_wfs[0]))), Repeat(23, WaveformPlayback(make_binary_waveform(wf_same))), Repeat('test_14', WaveformPlayback(make_binary_waveform(wf_same))) ]))
def test_get_position_advance_strategy(self): node = mock.Mock() node.samples.return_value = 0 node._get_single_indexed_playback.return_value.samples.return_value = 128 repeat = Repeat(10, node) # no samples at all self.assertIs(repeat._get_position_advance_strategy(), repeat._AdvanceStrategy.IGNORE) node.samples.assert_called_once_with() node._get_single_indexed_playback.assert_not_called() node.reset_mock() node.samples.return_value = 64 # samples do differ self.assertIs(repeat._get_position_advance_strategy(), repeat._AdvanceStrategy.INITIAL_RESET) node.samples.assert_called_once_with() node._get_single_indexed_playback.assert_called_once_with() node._get_single_indexed_playback.return_value.samples.assert_called_once_with( ) node.reset_mock() node.samples.return_value = 128 # samples are the same self.assertIs(repeat._get_position_advance_strategy(), repeat._AdvanceStrategy.POST_ADVANCE) node.samples.assert_called_once_with() node._get_single_indexed_playback.assert_called_once_with() node._get_single_indexed_playback.return_value.samples.assert_called_once_with( ) node.reset_mock() node._get_single_indexed_playback.return_value = None # multiple indexed playbacks self.assertIs(repeat._get_position_advance_strategy(), repeat._AdvanceStrategy.INITIAL_RESET) node.samples.assert_called_once_with() node._get_single_indexed_playback.assert_called_once_with()