Ejemplo n.º 1
0
def get_two_chan_test_block(wfg=WaveformGenerator(2)):
    generate_waveform = wfg.generate_single_channel_waveform
    generate_multi_channel_waveform = wfg.generate_multi_channel_waveform

    loop_block11 = InstructionBlock()
    loop_block11.add_instruction_exec(generate_multi_channel_waveform())

    loop_block1 = InstructionBlock()
    loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(loop_block11))

    loop_block21 = InstructionBlock()
    loop_block21.add_instruction_exec(generate_multi_channel_waveform())
    loop_block21.add_instruction_exec(generate_multi_channel_waveform())

    loop_block2 = InstructionBlock()
    loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(loop_block21))
    loop_block2.add_instruction_exec(generate_multi_channel_waveform())

    loop_block3 = InstructionBlock()
    loop_block3.add_instruction_exec(generate_multi_channel_waveform())
    loop_block3.add_instruction_exec(generate_multi_channel_waveform())

    loop_block411 = InstructionBlock()
    loop_block411.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')]))
    loop_block412 = InstructionBlock()
    loop_block412.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')]))

    loop_block41 = InstructionBlock()
    loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(loop_block411))
    loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(loop_block412))

    loop_block421 = InstructionBlock()
    loop_block421.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')]))
    loop_block422 = InstructionBlock()
    loop_block422.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')]))

    loop_block42 = InstructionBlock()
    loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(loop_block421))
    loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(loop_block422))

    chan_block4A = InstructionBlock()
    chan_block4A.add_instruction_repj(6, ImmutableInstructionBlock(loop_block41))

    chan_block4B = InstructionBlock()
    chan_block4B.add_instruction_repj(9, ImmutableInstructionBlock(loop_block42))

    loop_block4 = InstructionBlock()
    loop_block4.add_instruction_chan({frozenset('A'): ImmutableInstructionBlock(chan_block4A),
                                           frozenset('B'): ImmutableInstructionBlock(chan_block4B)})

    root_block = InstructionBlock()
    root_block.add_instruction_exec(generate_multi_channel_waveform())
    root_block.add_instruction_repj(10, ImmutableInstructionBlock(loop_block1))
    root_block.add_instruction_repj(17, ImmutableInstructionBlock(loop_block2))
    root_block.add_instruction_repj(3, ImmutableInstructionBlock(loop_block3))
    root_block.add_instruction_repj(4, ImmutableInstructionBlock(loop_block4))

    return root_block
Ejemplo n.º 2
0
class MultiChannelTests(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        wf = DummyWaveform()
        self.descriptionA = \
"""\
LOOP 1 times:
  ->EXEC {} 1 times
  ->EXEC {} 50 times
  ->LOOP 17 times:
      ->LOOP 2 times:
          ->EXEC {} 1 times
          ->EXEC {} 1 times
      ->EXEC {} 1 times
  ->LOOP 3 times:
      ->EXEC {} 1 times
      ->EXEC {} 1 times
  ->LOOP 24 times:
      ->EXEC {} 7 times
      ->EXEC {} 8 times"""
        self.descriptionB = \
"""\
LOOP 1 times:
  ->EXEC {} 1 times
  ->EXEC {} 50 times
  ->LOOP 17 times:
      ->LOOP 2 times:
          ->EXEC {} 1 times
          ->EXEC {} 1 times
      ->EXEC {} 1 times
  ->LOOP 3 times:
      ->EXEC {} 1 times
      ->EXEC {} 1 times
  ->LOOP 36 times:
      ->EXEC {} 10 times
      ->EXEC {} 11 times"""

        def generate_waveform(channel):
            return DummyWaveform(sample_output=None, duration=None, defined_channels={channel})

        def generate_multi_channel_waveform():
            return MultiChannelWaveform([generate_waveform('A'), generate_waveform('B')])

        self.loop_block11 = InstructionBlock()
        self.loop_block11.add_instruction_exec(generate_multi_channel_waveform())

        self.loop_block1 = InstructionBlock()
        self.loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(self.loop_block11))

        self.loop_block21 = InstructionBlock()
        self.loop_block21.add_instruction_exec(generate_multi_channel_waveform())
        self.loop_block21.add_instruction_exec(generate_multi_channel_waveform())

        self.loop_block2 = InstructionBlock()
        self.loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(self.loop_block21))
        self.loop_block2.add_instruction_exec(generate_multi_channel_waveform())

        self.loop_block3 = InstructionBlock()
        self.loop_block3.add_instruction_exec(generate_multi_channel_waveform())
        self.loop_block3.add_instruction_exec(generate_multi_channel_waveform())

        self.loop_block411 = InstructionBlock()
        self.loop_block411.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')]))
        self.loop_block412 = InstructionBlock()
        self.loop_block412.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')]))

        self.loop_block41 = InstructionBlock()
        self.loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(self.loop_block411))
        self.loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(self.loop_block412))

        self.loop_block421 = InstructionBlock()
        self.loop_block421.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')]))
        self.loop_block422 = InstructionBlock()
        self.loop_block422.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')]))

        self.loop_block42 = InstructionBlock()
        self.loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block421))
        self.loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(self.loop_block422))

        self.chan_block4A = InstructionBlock()
        self.chan_block4A.add_instruction_repj(6, ImmutableInstructionBlock(self.loop_block41))

        self.chan_block4B = InstructionBlock()
        self.chan_block4B.add_instruction_repj(9, ImmutableInstructionBlock(self.loop_block42))

        self.loop_block4 = InstructionBlock()
        self.loop_block4.add_instruction_chan({frozenset('A'): ImmutableInstructionBlock(self.chan_block4A),
                                              frozenset('B'): ImmutableInstructionBlock(self.chan_block4B)})

        self.root_block = InstructionBlock()
        self.root_block.add_instruction_exec(generate_multi_channel_waveform())
        self.root_block.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block1))
        self.root_block.add_instruction_repj(17, ImmutableInstructionBlock(self.loop_block2))
        self.root_block.add_instruction_repj(3, ImmutableInstructionBlock(self.loop_block3))
        self.root_block.add_instruction_repj(4, ImmutableInstructionBlock(self.loop_block4))

        self.maxDiff = None

    def get_mcp(self, channels):
        program = MultiChannelProgram(self.root_block, ['A', 'B'])
        return program[channels]

    def test_init(self):
        with self.assertRaises(ValueError):
            MultiChannelProgram(InstructionBlock())

        mcp = MultiChannelProgram(self.root_block, ['A', 'B'])
        self.assertEqual(mcp.channels, {'A', 'B'})

        with self.assertRaises(KeyError):
            mcp['C']

    def test_via_repr(self):
        root_loopA = self.get_mcp('A')
        root_loopB = self.get_mcp('B')
        waveformsA = tuple(loop.waveform
                                           for loop in root_loopA.get_depth_first_iterator() if loop.is_leaf())
        reprA = self.descriptionA.format(*waveformsA)
        reprB = self.descriptionB.format(*(loop.waveform
                                           for loop in root_loopB.get_depth_first_iterator() if loop.is_leaf()))
        self.assertEqual(root_loopA.__repr__(), reprA)
        self.assertEqual(root_loopB.__repr__(), reprB)