def test_nested_sequence_persistence1(): # Test writing a nested pulse sequence to a ConfigObj. root = RootSequence() context = BaseContext() root.context = context root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10', kind='Analogical', shape=SquareShape()) seq = Sequence(items=[Pulse(def_1='{2_stop} + 0.5', def_2='10', kind='Analogical', shape=SquareShape())]) root.items.extend([pulse1, pulse2, pulse3, seq]) pref = root.preferences_from_members() assert_items_equal(pref.keys(), ['name', 'local_vars', 'time_constrained', 'enabled', 'item_class', 'sequence_duration', 'item_0', 'item_1', 'item_2', 'item_3', 'context', 'def_1', 'def_2', 'def_mode', 'external_vars']) assert_items_equal(pref['item_3'].keys(), ['item_class', 'enabled', 'name', 'item_0', 'def_1', 'def_2', 'def_mode', 'local_vars', 'time_constrained'])
def test_build_from_config2(self): # Test rebuilding a sequence including a template sequence. # Channel mapping of template_vars values are known. conf = {'template_id': 'test', 'name': 'Template', 'template_vars': "{'b': '25'}"} seq = TemplateSequence.build_from_config(conf, self.dependecies) seq.context.channel_mapping = {'A': 'Ch1_L', 'B': 'Ch2_L', 'Ch1': 'Ch2_A', 'Ch2': 'Ch1_A'} root = RootSequence() context = TestContext(sampling=0.5) root.context = context root.items = [seq] pref = root.preferences_from_members() new = RootSequence.build_from_config(pref, self.dependecies) assert_equal(new.items[0].index, 1) seq = new.items[0] assert_equal(seq.name, 'Template') assert_equal(seq.template_id, 'test') assert_equal(seq.template_vars, dict(b='25')) assert_equal(seq.local_vars, dict(a='1.5')) assert_equal(len(seq.items), 4) assert_equal(seq.items[3].index, 5) assert_equal(seq.docs, 'Basic user comment\nff') context = seq.context assert_equal(context.template, seq) assert_equal(context.logical_channels, ['A', 'B']) assert_equal(context.analogical_channels, ['Ch1', 'Ch2']) assert_equal(context.channel_mapping, {'A': 'Ch1_L', 'B': 'Ch2_L', 'Ch1': 'Ch2_A', 'Ch2': 'Ch1_A'})
def test_sequence_indexing1(): # Test adding, moving, deleting pulse in a sequence. root = RootSequence() root.time_constrained = True root.sequence_duration = '1.0' context = TestContext() root.context = context pulse1 = Pulse() pulse2 = Pulse() pulse3 = Pulse() root.items.append(pulse1) assert_equal(pulse1.index, 1) assert_is(pulse1.root, root) assert_items_equal(root.linkable_vars, ['sequence_end', '1_start', '1_stop', '1_duration']) root.items.append(pulse2) assert_equal(pulse1.index, 1) assert_equal(pulse2.index, 2) assert_is(pulse2.root, root) assert_items_equal(root.linkable_vars, ['sequence_end', '1_start', '1_stop', '1_duration', '2_start', '2_stop', '2_duration']) root.items.append(pulse3) assert_equal(pulse1.index, 1) assert_equal(pulse2.index, 2) assert_equal(pulse3.index, 3) assert_is(pulse3.root, root) assert_items_equal(root.linkable_vars, ['sequence_end', '1_start', '1_stop', '1_duration', '2_start', '2_stop', '2_duration', '3_start', '3_stop', '3_duration']) root.time_constrained = False root.items.remove(pulse2) assert_equal(pulse1.index, 1) assert_equal(pulse2.index, 0) assert_equal(pulse3.index, 2) assert_is(pulse2.root, None) assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration', '2_start', '2_stop', '2_duration']) root.items.insert(1, pulse2) assert_equal(pulse1.index, 1) assert_equal(pulse2.index, 2) assert_equal(pulse3.index, 3) assert_is(pulse2.root, root) assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration', '2_start', '2_stop', '2_duration', '3_start', '3_stop', '3_duration'])
def setup(self): pref = create_template_sequence() conf = ConfigObj() conf.update(pref) dep = {'Sequence': Sequence, 'Pulse': Pulse, 'TemplateSequence': TemplateSequence, 'shapes': {'SquareShape': SquareShape}, 'contexts': {'TemplateContext': TemplateContext, 'TestContext': TestContext}, 'templates': {'test': ('', conf, 'Basic user comment\nff')}} self.dependecies = {'pulses': dep} self.root = RootSequence() self.context = TestContext(sampling=0.5) self.root.context = self.context conf = {'template_id': 'test', 'name': 'Template', 'template_vars': "{'b': '19'}"} seq = TemplateSequence.build_from_config(conf, self.dependecies) seq.context.channel_mapping = {'A': 'Ch1_L', 'B': 'Ch2_L', 'Ch1': 'Ch2_A', 'Ch2': 'Ch1_A'} seq.def_1 = '1.0' seq.def_2 = '20.0' self.template = seq self.root.items = [seq]
def test_sequence_time_constaints_observation(): # Test adding, moving, deleting pulse in a sequence. root = RootSequence() context = TestContext() root.context = context sequence = Sequence() root.items = [sequence] assert_equal(root.linkable_vars, []) sequence.time_constrained = True assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration']) sequence.time_constrained = False assert_equal(root.linkable_vars, [])
def test_build_from_config(): # Test building a pulse sequence. root = RootSequence() context = BaseContext() root.context = context root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10', kind='Analogical', shape=SquareShape()) seq = Sequence(items=[Pulse(def_1='{2_stop} + 0.5', def_2='10', kind='Analogical', shape=SquareShape())]) root.items.extend([pulse1, pulse2, pulse3, seq]) pref = root.preferences_from_members() dependecies = {'pulses': {'Sequence': Sequence, 'Pulse': Pulse, 'shapes': {'SquareShape': SquareShape}, 'contexts': {'BaseContext': BaseContext}}} aux = RootSequence.build_from_config(pref, dependecies) assert_equal(aux.external_vars, {'a': 1.5}) assert_equal(len(aux.items), 4) assert_is_instance(aux.context, BaseContext) pulse1 = aux.items[0] assert_equal(pulse1.def_1, '1.0') assert_equal(pulse1.def_2, '{a}') pulse2 = aux.items[1] assert_equal(pulse2.def_1, '{a} + 1.0') assert_equal(pulse2.def_2, '3.0') pulse3 = aux.items[2] assert_equal(pulse3.def_1, '{2_stop} + 0.5') assert_equal(pulse3.def_2, '10') assert_equal(pulse3.kind, 'Analogical') assert_is_instance(pulse3.shape, SquareShape) seq = aux.items[3] assert_equal(len(seq.items), 1)
def test_collect_dependencies(self): # Test collecting build dependencies. self.workbench.register(PulsesManagerManifest()) from hqc_meas.pulses.base_sequences import RootSequence, Sequence from hqc_meas.pulses.pulse import Pulse from hqc_meas.pulses.shapes.base_shapes import SquareShape from hqc_meas.pulses.contexts.awg_context import AWGContext root = RootSequence(context=AWGContext()) pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='{6_start} + 1.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='{1_stop}', def_2='0.5', def_mode='Start/Duration') pulse5.shape = SquareShape(amplitude='0.5') pulse5.kind = 'Analogical' pulse5.modulation.frequency = '1.0**' pulse5.modulation.phase = '1.0' pulse5.modulation.activated = True sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) root.items = [pulse1, sequence1, pulse5] core = self.workbench.get_plugin(u'enaml.workbench.core') com = u'hqc_meas.dependencies.collect_dependencies' res, build, run = core.invoke_command(com, {'obj': root}, core) assert_true(res) assert_in('pulses', build) assert_items_equal(['Sequence', 'Pulse', 'RootSequence', 'shapes', 'contexts', 'templates', 'sequences'], build['pulses'].keys()) assert_equal(['SquareShape'], build['pulses']['shapes'].keys()) assert_equal(['AWGContext'], build['pulses']['contexts'].keys()) assert_false(run)
def test_walk_sequence(): # Test walking a pulse sequence. root = RootSequence() context = BaseContext() root.context = context root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10', kind='Analogical', shape=SquareShape()) seq = Sequence(items=[Pulse(def_1='{2_stop} + 0.5', def_2='10', kind='Analogical', shape=SquareShape())]) root.items.extend([pulse1, pulse2, pulse3, seq]) walk = root.walk(['item_class', 'shape_class'], {}) flat = flatten_walk(walk, ['item_class', 'shape_class']) assert_in('item_class', flat) assert_equal(flat['item_class'], set(['Pulse', 'RootSequence', 'Sequence'])) assert_in('shape_class', flat) assert_equal(flat['shape_class'], set(['SquareShape']))
def create_template_sequence(): root = RootSequence() context = TemplateContext(logical_channels=['A', 'B'], analogical_channels=['Ch1', 'Ch2'], channel_mapping={'A': '', 'B': '', 'Ch1': '', 'Ch2': ''}) root.context = context root.local_vars = {'a': '1.5'} pulse1 = Pulse(channel='A', def_1='1.0', def_2='{a}') pulse2 = Pulse(channel='B', def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(channel='Ch1', def_1='{2_stop} + 0.5', def_2='{b}', kind='Analogical', shape=SquareShape()) seq = Sequence(items=[Pulse(channel='Ch2', def_1='{2_stop} + 0.5', def_2='{sequence_end}', kind='Analogical', shape=SquareShape())]) root.items.extend([pulse1, pulse2, seq, pulse3]) pref = root.preferences_from_members() pref['template_vars'] = repr(dict(b='')) del pref['item_class'] del pref['external_vars'] del pref['time_constrained'] return pref
def setup(self): self.root = RootSequence() self.context = AWGContext() self.root.context = self.context
class BenchmarkCompilation(object): def setup(self): self.root = RootSequence() self.context = AWGContext() self.root.context = self.context def benchmark_sequence_compilation1(self): # Test compiling a flat sequence. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{a}') pulse2 = Pulse(channel='Ch1_M1', def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(channel='Ch1_M1', def_1='{2_stop} + 0.5', def_2='10') self.root.items.extend([pulse1, pulse2, pulse3]) assert_true(self.root.compile_sequence()[0]) print 'Sequence 1', time(partial(self.root.compile_sequence)) def benchmark_sequence_compilation2(self): # Test compiling a flat sequence of fixed duration. self.root.external_vars = {'a': 1.5} self.root.fix_sequence_duration = True self.root.sequence_duration = '10.0' pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{a}') pulse2 = Pulse(channel='Ch1_M1', def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(channel='Ch1_M1', def_1='{2_stop} + 0.5', def_2='{sequence_end}') self.root.items.extend([pulse1, pulse2, pulse3]) assert_true(self.root.compile_sequence()[0]) print 'Sequence 2', time(partial(self.root.compile_sequence)) def benchmark_sequence_compilation3(self): # Test compiling a flat sequence in two passes. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{2_start} - 1.0') pulse2 = Pulse(channel='Ch1_M1', def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(channel='Ch1_M1', def_1='{2_stop} + 0.5', def_2='10') self.root.items.extend([pulse1, pulse2, pulse3]) assert_true(self.root.compile_sequence()[0]) print 'Sequence 3', time(partial(self.root.compile_sequence)) def benchmark_sequence_compilation7(self): # Test compiling a nested sequence. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{a}') pulse2 = Pulse(channel='Ch1_M1', def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(channel='Ch1_M1', def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(channel='Ch1_M1', def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(channel='Ch1_M1', def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] assert_true(self.root.compile_sequence()[0]) print 'Sequence 7', time(partial(self.root.compile_sequence)) def benchmark_sequence_compilation8(self): # Test compiling a nested sequence in two passes on the external # sequence. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(channel='Ch1_M1', def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(channel='Ch1_M1', def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(channel='Ch1_M1', def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(channel='Ch1_M1', def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] assert_true(self.root.compile_sequence()[0]) print 'Sequence 8', time(partial(self.root.compile_sequence)) def benchmark_sequence_compilation9(self): # Test compiling a nested sequence in multi passes. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(channel='Ch1_M1', def_1='{a} + 1.0', def_2='{6_start} + 1.0') pulse3 = Pulse(channel='Ch1_M1', def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(channel='Ch1_M1', def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(channel='Ch1_M1', def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] assert_true(self.root.compile_sequence()[0]) print 'Sequence 9', time(partial(self.root.compile_sequence)) def benchmark_conditional_sequence_compilation1(self): # Test compiling a conditional sequence whose condition evaluates to # False. self.root.external_vars = {'a': 1.5, 'include': True} pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(channel='Ch1_M1', def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(channel='Ch1_M1', def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(channel='Ch1_M1', def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(channel='Ch1_M1', def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = ConditionalSequence(items=[pulse2, sequence2, pulse4], condition='{include}') self.root.items = [pulse1, sequence1, pulse5] assert_true(self.root.compile_sequence()[0]) print 'Conditional seq 1', time(partial(self.root.compile_sequence)) def benchmark_conditional_sequence_compilation2(self): # Test compiling a conditional sequence whose condition evaluates to # True. self.root.external_vars = {'a': 1.5, 'include': False} pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(channel='Ch1_M1', def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(channel='Ch1_M1', def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(channel='Ch1_M1', def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(channel='Ch1_M1', def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = ConditionalSequence(items=[pulse2, sequence2, pulse4], condition='{include}') self.root.items = [pulse1, sequence1, pulse5] assert_true(self.root.compile_sequence()[0]) print 'Conditional seq 2', time(partial(self.root.compile_sequence))
def test_sequence_indexing2(): # Test adding, moving, deleting a sequence in a sequence. root = RootSequence() context = TestContext() root.context = context pulse1 = Pulse() pulse2 = Pulse() pulse3 = Pulse() pulse4 = Pulse() sequence1 = Sequence() sequence2 = Sequence() root.items.append(pulse1) root.items.append(sequence1) root.items.append(pulse2) assert_is(sequence1.parent, root) assert_is(sequence1.root, root) sequence1.items.append(sequence2) assert_is(sequence2.parent, sequence1) assert_is(sequence2.root, root) assert_equal(pulse1.index, 1) assert_equal(pulse2.index, 4) assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration', '4_start', '4_stop', '4_duration']) pulse1.index = 200 sequence2.items.append(pulse3) assert_is(pulse3.parent, sequence2) assert_is(pulse3.root, root) assert_equal(pulse2.index, 5) assert_equal(pulse3.index, 4) assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration', '4_start', '4_stop', '4_duration', '5_start', '5_stop', '5_duration']) # Check that only the pulse below the modified sequence are updated. assert_equal(pulse1.index, 200) pulse1.index = 1 sequence1.items.insert(0, pulse4) assert_equal(pulse4.index, 3) assert_equal(sequence2.index, 4) assert_equal(pulse3.index, 5) assert_equal(pulse2.index, 6) assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration', '3_start', '3_stop', '3_duration', '5_start', '5_stop', '5_duration', '6_start', '6_stop', '6_duration']) sequence1.items = [pulse4] assert_is(sequence2.parent, None) assert_equal(sequence2.index, 0) assert_equal(pulse2.index, 4) assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration', '3_start', '3_stop', '3_duration', '4_start', '4_stop', '4_duration']) sequence1.index = 200 root2 = RootSequence() sequence2.root = root2 sequence2.items = [] # Check the observer was properly removed assert_equal(sequence1.index, 200)
def setup(self): self.root = RootSequence() self.context = TestContext(sampling=0.5) self.root.context = self.context
class TestCompilation(object): def setup(self): self.root = RootSequence() self.context = TestContext(sampling=0.5) self.root.context = self.context def test_sequence_compilation1(self): # Test compiling a flat sequence. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10') self.root.items.extend([pulse1, pulse2, pulse3]) res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 3) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 1.5) assert_equal(pulses[0].duration, 0.5) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_equal(pulses[2].start, 3.5) assert_equal(pulses[2].stop, 10.0) assert_equal(pulses[2].duration, 6.5) def test_sequence_compilation1bis(self): # Compiles two times a sequence while changing a parameter to make # sure the cache is cleaned in between self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='4.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10') self.root.items.extend([pulse1, pulse2, pulse3]) res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 3) assert_equal(pulses[0].stop, 1.5) self.root.external_vars = {'a': 2.} res, pulses = self.root.compile_sequence(False) print res, pulses, pulse1.stop assert_true(res) assert_equal(len(pulses), 3) assert_equal(pulses[0].stop, 2.) def test_sequence_compilation2(self): # Test compiling a flat sequence of fixed duration. self.root.external_vars = {'a': 1.5} self.root.time_constrained = True self.root.sequence_duration = '10.0' pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='{sequence_end}') self.root.items.extend([pulse1, pulse2, pulse3]) res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 3) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 1.5) assert_equal(pulses[0].duration, 0.5) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_equal(pulses[2].start, 3.5) assert_equal(pulses[2].stop, 10.0) assert_equal(pulses[2].duration, 6.5) def test_sequence_compilation3(self): # Test compiling a flat sequence in two passes. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{2_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10') self.root.items.extend([pulse1, pulse2, pulse3]) res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 3) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 1.5) assert_equal(pulses[0].duration, 0.5) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_equal(pulses[2].start, 3.5) assert_equal(pulses[2].stop, 10.0) assert_equal(pulses[2].duration, 6.5) def test_sequence_compilation4(self): # Test compiling a flat sequence with circular references. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{2_start} - 1.0') pulse2 = Pulse(def_1='{1_stop} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10') self.root.items.extend([pulse1, pulse2, pulse3]) res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_equal(len(missings), 2) assert_in('1_stop', missings) assert_in('2_start', missings) assert_equal(len(errors), 0) def test_sequence_compilation5(self): # Test compiling a flat sequence with evaluation errors. # missing global self.root.time_constrained = True self.root.sequence_duration = '10.0' pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='{sequence_end}') self.root.items.extend([pulse1, pulse2, pulse3]) res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_equal(len(missings), 1) assert_in('a', missings) assert_equal(len(errors), 0) def test_sequence_compilation6(self): # Test compiling a flat sequence with evaluation errors. # wrong string value self.root.external_vars = {'a': 1.5} self.root.time_constrained = True self.root.sequence_duration = '*10.0*' pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} +* 1.0', def_2='3.0') pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10.0') self.root.items.extend([pulse1, pulse2, pulse3]) res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_false(missings) assert_equal(len(errors), 2) assert_in('2_start', errors) assert_in('root_seq_duration', errors) def test_sequence_compilation7(self): # Test compiling a nested sequence. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 5) assert_is(pulses[0], pulse1) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 1.5) assert_equal(pulses[0].duration, 0.5) assert_is(pulses[1], pulse2) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_is(pulses[2], pulse3) assert_equal(pulses[2].start, 3.5) assert_equal(pulses[2].stop, 10.0) assert_equal(pulses[2].duration, 6.5) assert_is(pulses[3], pulse4) assert_equal(pulses[3].start, 2.0) assert_equal(pulses[3].stop, 2.5) assert_equal(pulses[3].duration, 0.5) assert_is(pulses[4], pulse5) assert_equal(pulses[4].start, 3.0) assert_equal(pulses[4].stop, 3.5) assert_equal(pulses[4].duration, 0.5) def test_sequence_compilation8(self): # Test compiling a nested sequence in two passes on the external # sequence. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 5) assert_is(pulses[0], pulse1) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 2.0) assert_equal(pulses[0].duration, 1.0) assert_is(pulses[1], pulse2) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_is(pulses[2], pulse3) assert_equal(pulses[2].start, 3.5) assert_equal(pulses[2].stop, 10.0) assert_equal(pulses[2].duration, 6.5) assert_is(pulses[3], pulse4) assert_equal(pulses[3].start, 2.0) assert_equal(pulses[3].stop, 2.5) assert_equal(pulses[3].duration, 0.5) assert_is(pulses[4], pulse5) assert_equal(pulses[4].start, 3.0) assert_equal(pulses[4].stop, 3.5) assert_equal(pulses[4].duration, 0.5) def test_sequence_compilation9(self): # Test compiling a nested sequence in multi passes. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='{6_start} + 1.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 5) assert_is(pulses[0], pulse1) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 2.0) assert_equal(pulses[0].duration, 1.0) assert_is(pulses[1], pulse2) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_is(pulses[2], pulse3) assert_equal(pulses[2].start, 3.5) assert_equal(pulses[2].stop, 10.0) assert_equal(pulses[2].duration, 6.5) assert_is(pulses[3], pulse4) assert_equal(pulses[3].start, 2.0) assert_equal(pulses[3].stop, 2.5) assert_equal(pulses[3].duration, 0.5) assert_is(pulses[4], pulse5) assert_equal(pulses[4].start, 3.0) assert_equal(pulses[4].stop, 3.5) assert_equal(pulses[4].duration, 0.5) def test_sequence_compilation10(self): # Test compiling a nested sequence with circular reference in the deep # one. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='{6_start} + 1.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='{1_stop}', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_equal(len(missings), 2) assert_in('7_start', missings) assert_in('1_stop', missings) assert_false(errors) def test_sequence_compilation11(self): # Test compiling a nested sequence with circular reference in the deep # one. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='{6_start} + 1.0') pulse3 = Pulse(def_1='{3_stop} + *0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_equal(len(errors), 1) assert_in('5_start', errors) def test_sequence_compilation12(self): # Test compiling a nested sequence using local vars. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='{b}') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3], local_vars={'b': '2**2'}) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 5) assert_is(pulses[0], pulse1) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 1.5) assert_equal(pulses[0].duration, 0.5) assert_is(pulses[1], pulse2) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_is(pulses[2], pulse3) assert_equal(pulses[2].start, 3.5) assert_equal(pulses[2].stop, 4) assert_equal(pulses[2].duration, 0.5) assert_is(pulses[3], pulse4) assert_equal(pulses[3].start, 2.0) assert_equal(pulses[3].stop, 2.5) assert_equal(pulses[3].duration, 0.5) assert_is(pulses[4], pulse5) assert_equal(pulses[4].start, 3.0) assert_equal(pulses[4].stop, 3.5) assert_equal(pulses[4].duration, 0.5) def test_sequence_compilation13(self): # Test compiling a nested sequence with wrong local vars definitions. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='{b}') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3], local_vars={'b': '2**2*'}) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_equal(len(missings), 1) assert_in('b', missings) assert_in('4_b', errors) def test_sequence_compilation14(self): # Test the locality of local vars. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='{b}') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='{b}', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3], local_vars={'b': '2**2'}) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_equal(len(missings), 1) assert_in('b', missings) assert_false(errors) # Here I don't test the evaluation errors on the defs as this is handled # at the Item level and tested in TestPulse. def test_sequence_compilation15(self): # Test compiling a nested sequence with internal fixed length. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{4_start} + 0.5', def_2='{4_start}+{4_duration}-0.5') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3], time_constrained=True, def_1='{3_stop} + 0.5', def_2='6') sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 5) assert_is(pulses[0], pulse1) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 1.5) assert_equal(pulses[0].duration, 0.5) assert_is(pulses[1], pulse2) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_is(pulses[2], pulse3) assert_equal(pulses[2].start, 4) assert_equal(pulses[2].stop, 5.5) assert_equal(pulses[2].duration, 1.5) assert_is(pulses[3], pulse4) assert_equal(pulses[3].start, 2.0) assert_equal(pulses[3].stop, 2.5) assert_equal(pulses[3].duration, 0.5) assert_is(pulses[4], pulse5) assert_equal(pulses[4].start, 3.0) assert_equal(pulses[4].stop, 3.5) assert_equal(pulses[4].duration, 0.5) def test_sequence_compilation16(self): # Test compiling a nested sequence with internal fixed length but # incoherent pulse start. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{4_start} - 0.5', def_2='{4_start}+{4_duration}-0.5') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3], time_constrained=True, def_1='{3_stop} + 0.5', def_2='6', name='test') sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_false(missings) assert_in('test-start', errors) def test_sequence_compilation17(self): # Test compiling a nested sequence with internal fixed length but # incoherent pulse stop. self.root.external_vars = {'a': 1.5} pulse1 = Pulse(def_1='1.0', def_2='{a}') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{4_start} + 0.5', def_2='{4_start}+{4_duration}+0.5') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3], time_constrained=True, def_1='{3_stop} + 0.5', def_2='6', name='test') sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_false(missings) assert_in('test-stop', errors) def test_conditional_sequence_compilation1(self): # Test compiling a conditional sequence whose condition evaluates to # False. self.root.external_vars = {'a': 1.5, 'include': True} pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = ConditionalSequence(items=[pulse2, sequence2, pulse4], condition='{include}') self.root.items = [pulse1, sequence1, pulse5] res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 5) assert_is(pulses[0], pulse1) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 2.0) assert_equal(pulses[0].duration, 1.0) assert_is(pulses[1], pulse2) assert_equal(pulses[1].start, 2.5) assert_equal(pulses[1].stop, 3.0) assert_equal(pulses[1].duration, 0.5) assert_is(pulses[2], pulse3) assert_equal(pulses[2].start, 3.5) assert_equal(pulses[2].stop, 10.0) assert_equal(pulses[2].duration, 6.5) assert_is(pulses[3], pulse4) assert_equal(pulses[3].start, 2.0) assert_equal(pulses[3].stop, 2.5) assert_equal(pulses[3].duration, 0.5) assert_is(pulses[4], pulse5) assert_equal(pulses[4].start, 3.0) assert_equal(pulses[4].stop, 3.5) assert_equal(pulses[4].duration, 0.5) def test_conditional_sequence_compilation2(self): # Test compiling a conditional sequence whose condition evaluates to # True. self.root.external_vars = {'a': 1.5, 'include': False} pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = ConditionalSequence(items=[pulse2, sequence2, pulse4], condition='{include}') self.root.items = [pulse1, sequence1, pulse5] res, pulses = self.root.compile_sequence(False) assert_true(res) assert_equal(len(pulses), 2) assert_is(pulses[0], pulse1) assert_equal(pulses[0].start, 1.0) assert_equal(pulses[0].stop, 2.0) assert_equal(pulses[0].duration, 1.0) assert_is(pulses[1], pulse5) assert_equal(pulses[1].start, 3.0) assert_equal(pulses[1].stop, 3.5) assert_equal(pulses[1].duration, 0.5) def test_conditional_sequence_compilation3(self): # Test compiling a conditional sequence with a wrong condition. self.root.external_vars = {'a': 1.5, 'include': False} pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0') pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = ConditionalSequence(items=[pulse2, sequence2, pulse4], condition='{include}*/') self.root.items = [pulse1, sequence1, pulse5] res, (missings, errors) = self.root.compile_sequence(False) assert_false(res) assert_in('2_condition', errors)
class TestAWGContext(object): """ """ def setup(self): self.root = RootSequence() self.context = AWGContext() self.root.context = self.context def test_compiling_A_pulse(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse = Pulse(kind='Analogical', shape=SquareShape(amplitude='1.0'), def_1='0.1', def_2='0.5', channel='Ch1_A') self.root.items = [pulse] res, arrays = self.root.compile_sequence() assert_true(res) assert_in(1, arrays) assert_equal(len(arrays), 1) sequence = np.zeros(2000, dtype=np.uint8) sequence[1::2] = 2**5 sequence[201:1001:2] += 2**4 + 2**3 + 4 + 2 + 1 sequence[200:1000:2] += 255 assert_sequence_equal(arrays[1], bytearray(sequence)) def test_compiling_M1_pulse(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M1') self.root.items = [pulse] res, arrays = self.root.compile_sequence() assert_true(res) assert_in(1, arrays) sequence = np.zeros(2000, dtype=np.uint8) sequence[1::2] = 2**5 sequence[201:1001:2] += 2**6 assert_sequence_equal(arrays[1], bytearray(sequence)) def test_compiling_M2_pulse(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M2') self.root.items = [pulse] res, arrays = self.root.compile_sequence() assert_true(res) assert_in(1, arrays) def test_compiling_inverted_logical_pulses(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M2') self.root.items = [pulse] self.context.inverted_log_channels = ['Ch1_M1', 'Ch1_M2'] res, arrays = self.root.compile_sequence() assert_true(res) assert_in(1, arrays) sequence = np.zeros(2000, dtype=np.uint8) sequence[1::2] = 2**7 + 2**6 + 2**5 sequence[201:1001:2] -= 2**7 assert_sequence_equal(arrays[1], bytearray(sequence)) def test_compiling_variable_length(self): pulse = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M1') self.root.items = [pulse] self.context.sampling_frequency = 1e8 res, arrays = self.root.compile_sequence() assert_true(res) assert_in(1, arrays) sequence = np.zeros(100, dtype=np.uint8) sequence[1::2] = 2**5 sequence[21:101:2] += 2**6 assert_sequence_equal(arrays[1], bytearray(sequence)) def test_too_short_fixed_length(self): self.root.time_constrained = True self.root.sequence_duration = '0.3' pulse = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M1') self.root.items = [pulse] res, traceback = self.root.compile_sequence() assert_false(res) def test_channel_kind_mixing(self): self.root.time_constrained = True self.root.sequence_duration = '0.3' pulse = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_A') self.root.items = [pulse] res, traceback = self.root.compile_sequence() assert_false(res) def test_overlapping_pulses(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse1 = Pulse(kind='Analogical', def_1='0.1', def_2='0.5', channel='Ch1_A', shape=SquareShape(amplitude='1.0'), modulation=Modulation(frequency='2.5', kind='sin', activated=True)) pulse2 = Pulse(kind='Analogical', def_1='0.1', def_2='0.5', channel='Ch1_A', shape=SquareShape(amplitude='1.0'), modulation=Modulation(frequency='2.5', kind='sin', phase='Pi', activated=True)) self.root.items = [pulse1, pulse2] res, arrays = self.root.compile_sequence() assert_true(res) assert_in(1, arrays) def test_nearly_overlapping_M2(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse1 = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M2') pulse2 = Pulse(kind='Logical', def_1='0.5', def_2='0.6', channel='Ch1_M2') self.root.items = [pulse1, pulse2] res, arrays = self.root.compile_sequence() assert_true(res) assert_in(1, arrays) sequence = np.zeros(2000, dtype=np.uint8) sequence[1::2] = 2**5 sequence[201:1201:2] += 2**7 assert_sequence_equal(arrays[1], bytearray(sequence)) def test_overflow_check_A(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse1 = Pulse(kind='Analogical', def_1='0.1', def_2='0.5', channel='Ch1_A', shape=SquareShape(amplitude='1.0'), modulation=Modulation(frequency='2.5', kind='sin', activated=True)) pulse2 = Pulse(kind='Analogical', def_1='0.1', def_2='0.5', channel='Ch1_A', shape=SquareShape(amplitude='1.0'), modulation=Modulation(frequency='2.5', kind='sin', activated=True)) self.root.items = [pulse1, pulse2] res, traceback = self.root.compile_sequence() assert_false(res) assert_in('Ch1_A', traceback) def test_overflow_check_M1(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse1 = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M1') pulse2 = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M1') self.root.items = [pulse1, pulse2] res, traceback = self.root.compile_sequence() assert_false(res) assert_in('Ch1_M1', traceback) def test_overflow_check_M2(self): self.root.time_constrained = True self.root.sequence_duration = '1' pulse1 = Pulse(kind='Logical', def_1='0.1', def_2='0.5', channel='Ch1_M2') pulse2 = Pulse(kind='Logical', def_1='0.4', def_2='0.6', channel='Ch1_M2') self.root.items = [pulse1, pulse2] res, traceback = self.root.compile_sequence() assert_false(res) assert_in('Ch1_M2', traceback) def test_compiling_sequence1(self): self.root.external_vars = {'a': 1.5} pulse1 = Pulse(channel='Ch1_M1', def_1='1.0', def_2='{7_start} - 1.0') pulse2 = Pulse(channel='Ch1_M2', def_1='{a} + 1.0', def_2='{6_start} + 1.0') pulse3 = Pulse(channel='Ch2_M1', def_1='{3_stop} + 0.5', def_2='10.0') pulse4 = Pulse(channel='Ch2_M2', def_1='2.0', def_2='0.5', def_mode='Start/Duration') pulse5 = Pulse(channel='Ch3_M1', def_1='3.0', def_2='0.5', def_mode='Start/Duration') sequence2 = Sequence(items=[pulse3]) sequence1 = Sequence(items=[pulse2, sequence2, pulse4]) self.root.items = [pulse1, sequence1, pulse5] res, arrays = self.root.compile_sequence() assert_true(res) assert_equal(len(arrays), 3) assert_equal(sorted(arrays.keys()), sorted([1, 2, 3]))
class TestCompilation(object): def setup(self): pref = create_template_sequence() conf = ConfigObj() conf.update(pref) dep = {'Sequence': Sequence, 'Pulse': Pulse, 'TemplateSequence': TemplateSequence, 'shapes': {'SquareShape': SquareShape}, 'contexts': {'TemplateContext': TemplateContext, 'TestContext': TestContext}, 'templates': {'test': ('', conf, 'Basic user comment\nff')}} self.dependecies = {'pulses': dep} self.root = RootSequence() self.context = TestContext(sampling=0.5) self.root.context = self.context conf = {'template_id': 'test', 'name': 'Template', 'template_vars': "{'b': '19'}"} seq = TemplateSequence.build_from_config(conf, self.dependecies) seq.context.channel_mapping = {'A': 'Ch1_L', 'B': 'Ch2_L', 'Ch1': 'Ch2_A', 'Ch2': 'Ch1_A'} seq.def_1 = '1.0' seq.def_2 = '20.0' self.template = seq self.root.items = [seq] def test_sequence_compilation1(self): # Test compiling a template when everything is ok. res, pulses = self.root.compile_sequence(False) assert_true(res, '{}'.format(pulses)) assert_equal(len(pulses), 4) pulse = pulses[0] assert_equal(pulse.index, 1) assert_equal(pulse.start, 2.0) assert_equal(pulse.stop, 2.5) assert_equal(pulse.duration, 0.5) assert_equal(pulse.channel, 'Ch1_L') pulse = pulses[1] assert_equal(pulse.index, 2) assert_equal(pulse.start, 3.5) assert_equal(pulse.stop, 4) assert_equal(pulse.duration, 0.5) assert_equal(pulse.channel, 'Ch2_L') pulse = pulses[2] assert_equal(pulse.index, 4) assert_equal(pulse.start, 4.5) assert_equal(pulse.stop, 20) assert_equal(pulse.duration, 15.5) assert_equal(pulse.channel, 'Ch1_A') pulse = pulses[3] assert_equal(pulse.index, 5) assert_equal(pulse.start, 4.5) assert_equal(pulse.stop, 20) assert_equal(pulse.duration, 15.5) assert_equal(pulse.channel, 'Ch2_A') def test_sequence_compilation2(self): # Test compiling a template : issue in context, incomplete mapping. self.template.context.channel_mapping = {'A': 'Ch1_L', 'B': 'Ch2_L', 'Ch1': 'Ch2_A'} res, (miss, errors) = self.root.compile_sequence(False) assert_false(res) assert_false(miss) assert_in('Template-context', errors) assert_in('Ch2', errors['Template-context']) def test_sequence_compilation3(self): # Test compiling a template : issue in context, erroneous mapping. self.template.context.channel_mapping = {'A': 'Ch1_L', 'B': 'Ch2_L', 'Ch1': 'Ch2_A', 'Ch2': 'A'} res, (miss, errors) = self.root.compile_sequence(False) assert_false(res) assert_false(miss) assert_in('Template-context', errors) assert_in('Ch2', errors['Template-context']) def test_sequence_compilation3bis(self): # Test compiling a template : pulse as umapped channel. self.template.items[0].channel = '__' res, (miss, errors) = self.root.compile_sequence(False) assert_false(res) assert_false(miss) assert_in('Template-channels', errors) assert_in('__', errors['Template-channels']) def test_sequence_compilation4(self): # Test compiling a template : issue in defs. self.template.def_1 = 'r*' res, (miss, errors) = self.root.compile_sequence(False) assert_false(res) assert_false(miss) assert_in('1_start', errors) def test_sequence_compilation5(self): # Test compiling a template : issue in template_vars. self.template.template_vars = {'b': '*1'} res, (miss, errors) = self.root.compile_sequence(False) assert_false(res) assert_in('1_b', errors) def test_sequence_compilation6(self): # Test compiling a template : issue in local_vars. self.template.local_vars = {'a': '*1'} res, (miss, errors) = self.root.compile_sequence(False) assert_false(res) assert_in('1_a', errors) def test_sequence_compilation7(self): # Test compiling a template : issue in stop time. self.template.items[0].def_2 = '200' res, (miss, errors) = self.root.compile_sequence(False) assert_false(res) assert_in('Template-stop', errors)