Esempio n. 1
0
    def test_const_propagation(self):
        lhs = MultiChannelWaveform([
            DummyWaveform(duration=1.5, defined_channels={'a', 'c', 'd', 'i'}),
            ConstantWaveform.from_mapping(1.5, {'e': 1.2, 'f': 1.3, 'h': 4.6})
        ])
        rhs = MultiChannelWaveform([
            DummyWaveform(duration=1.5, defined_channels={'a', 'b', 'e'}),
            ConstantWaveform.from_mapping(1.5, {'f': 2.5, 'g': 3.5, 'i': 6.4})
        ])

        wf = ArithmeticWaveform(lhs, '-', rhs)

        assert_constant_consistent(self, wf)

        expected = {'a': None,
                    'b': None,
                    'c': None,
                    'd': None,
                    'e': None,
                    'f': 1.3-2.5,
                    'g': -3.5,
                    'h': 4.6,
                    'i': None}

        actual = {ch: wf.constant_value(ch) for ch in wf.defined_channels}
        self.assertEqual(expected, actual)
Esempio n. 2
0
    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)
Esempio n. 3
0
    def test_from_functor(self):
        dummy_wf = DummyWaveform(1.5, defined_channels={'A', 'B'})
        const_wf = ConstantWaveform.from_mapping(1.5, {'A': 1.1, 'B': 2.2})

        wf = FunctorWaveform.from_functor(dummy_wf, {'A': np.negative, 'B': np.positive})
        self.assertEqual(FunctorWaveform(dummy_wf, {'A': np.negative, 'B': np.positive}), wf)
        self.assertFalse(wf.is_constant())
        assert_constant_consistent(self, wf)

        wf = FunctorWaveform.from_functor(const_wf, {'A': np.negative, 'B': np.positive})
        self.assertEqual(ConstantWaveform.from_mapping(1.5, {'A': -1.1, 'B': 2.2}), wf)
        assert_constant_consistent(self, wf)
Esempio n. 4
0
    def test_from_operator(self):
        lhs = DummyWaveform(duration=1.5, defined_channels={'a', 'b', 'c'})
        rhs = DummyWaveform(duration=1.5, defined_channels={'a', 'b', 'd'})

        lhs_const = ConstantWaveform.from_mapping(1.5, {'a': 1.1, 'b': 2.2, 'c': 3.3})
        rhs_const = ConstantWaveform.from_mapping(1.5, {'a': 1.2, 'b': 2.4, 'd': 3.4})

        self.assertEqual(ArithmeticWaveform(lhs, '+', rhs), ArithmeticWaveform.from_operator(lhs, '+', rhs))
        self.assertEqual(ArithmeticWaveform(lhs_const, '+', rhs), ArithmeticWaveform.from_operator(lhs_const, '+', rhs))
        self.assertEqual(ArithmeticWaveform(lhs, '+', rhs_const), ArithmeticWaveform.from_operator(lhs, '+', rhs_const))

        expected = ConstantWaveform.from_mapping(1.5, {'a': 1.1-1.2, 'b': 2.2-2.4, 'c': 3.3, 'd': -3.4})
        consted = ArithmeticWaveform.from_operator(lhs_const, '-', rhs_const)
        self.assertEqual(expected, consted)
Esempio n. 5
0
def roll_constant_waveforms(program: Loop, minimal_waveform_quanta: int, waveform_quantum: int, sample_rate: TimeType):
    """This function finds waveforms in program that can be replaced with repetitions of shorter waveforms and replaces
    them. Complexity O(N_waveforms)

    This is possible if:
     - The waveform is constant on all channels
     - waveform.duration * sample_rate / waveform_quantum has a factor that is bigger than minimal_waveform_quanta

    Args:
        program:
        minimal_waveform_quanta:
        waveform_quantum:
        sample_rate:
    """
    waveform = program.waveform

    if waveform is None:
        for child in program:
            roll_constant_waveforms(child, minimal_waveform_quanta, waveform_quantum, sample_rate)
    else:
        waveform_quanta = (waveform.duration * sample_rate) // waveform_quantum

        # example
        # waveform_quanta = 15
        # minimal_waveform_quanta = 2
        # => repetition_count = 5, new_waveform_quanta = 3
        if waveform_quanta < minimal_waveform_quanta * 2:
            # there is no way to roll this waveform because it is too short
            return

        const_values = waveform.constant_value_dict()
        if const_values is None:
            # The waveform is not constant
            return

        new_waveform_quanta = smallest_factor_ge(waveform_quanta, min_factor=minimal_waveform_quanta)
        if new_waveform_quanta == waveform_quanta:
            # the waveform duration in samples has no suitable factor
            # TODO: Option to insert multiple Loop objects
            return

        additional_repetition_count = waveform_quanta // new_waveform_quanta

        new_waveform = ConstantWaveform.from_mapping(
            duration=waveform_quantum * new_waveform_quanta / sample_rate,
            constant_values=const_values)

        # use the private properties to avoid invalidating the duration cache of the parent loop
        program._repetition_definition = program.repetition_definition * additional_repetition_count
        program._waveform = new_waveform
Esempio n. 6
0
    def test_build_waveform(self):
        tpt = ConstantPulseTemplate(200, {'C1': 2, 'C2': 3})

        wf_id = tpt.build_waveform({}, {'C1': 'C1', 'C2': 'C2'})
        self.assertEqual(
            ConstantWaveform.from_mapping(200, {
                'C1': 2,
                'C2': 3
            }), wf_id)

        wf_1 = tpt.build_waveform({}, {'C1': 'C1', 'C2': None})
        self.assertEqual(ConstantWaveform.from_mapping(200, {'C1': 2}), wf_1)

        wf_2 = tpt.build_waveform({}, {'C1': None, 'C2': 'A'})
        self.assertEqual(ConstantWaveform.from_mapping(200, {'A': 3}), wf_2)

        wf_all = tpt.build_waveform({}, {'C1': 'B', 'C2': 'A'})
        self.assertEqual(ConstantWaveform.from_mapping(200, {
            'A': 3,
            'B': 2
        }), wf_all)

        self.assertIsNone(tpt.build_waveform({}, {'C1': None, 'C2': None}))
Esempio n. 7
0
    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}, '
            f'defined_channels {self.defined_channels}')

        constant_values = {}
        for channel, value in self._amplitude_dict.items():
            mapped_channel = channel_mapping[channel]
            if mapped_channel is not None:
                constant_values[mapped_channel] = value

        if constant_values:
            return ConstantWaveform.from_mapping(self.duration,
                                                 constant_values)
        else:
            return None
Esempio n. 8
0
    def test_from_transformation(self):
        const_output = {'c': 4.4, 'd': 5.5, 'e': 6.6}
        trafo = TransformationDummy(output_channels=const_output.keys(), constant_invariant=False)
        const_trafo = TransformationDummy(output_channels=const_output.keys(), constant_invariant=True,
                                          transformed=const_output)
        dummy_wf = DummyWaveform(duration=1.5, defined_channels={'a', 'b'})
        const_wf = ConstantWaveform.from_mapping(3, {'a': 2.2, 'b': 3.3})

        self.assertEqual(TransformingWaveform(inner_waveform=dummy_wf, transformation=trafo),
                         TransformingWaveform.from_transformation(inner_waveform=dummy_wf, transformation=trafo))

        self.assertEqual(TransformingWaveform(inner_waveform=dummy_wf, transformation=const_trafo),
                         TransformingWaveform.from_transformation(inner_waveform=dummy_wf, transformation=const_trafo))

        self.assertEqual(TransformingWaveform(inner_waveform=const_wf, transformation=trafo),
                         TransformingWaveform.from_transformation(inner_waveform=const_wf, transformation=trafo))

        with mock.patch.object(ConstantWaveform, 'from_mapping', return_value=mock.sentinel) as from_mapping:
            self.assertIs(from_mapping.return_value,
                          TransformingWaveform.from_transformation(inner_waveform=const_wf, transformation=const_trafo))
            from_mapping.assert_called_once_with(const_wf.duration, const_output)
Esempio n. 9
0
def get_constant_unique_wfs(n=10000,
                            duration=192,
                            defined_channels=frozenset(['A'])):
    if not hasattr(get_unique_wfs, 'cache'):
        get_unique_wfs.cache = {}

    key = (n, duration, defined_channels)

    if key not in get_unique_wfs.cache:
        bit_gen = np.random.PCG64(_key_to_int(n, duration, defined_channels))
        rng = np.random.Generator(bit_gen)

        random_values = rng.random(size=(n, len(defined_channels)))

        sorted_channels = sorted(defined_channels)
        get_unique_wfs.cache[key] = [
            ConstantWaveform.from_mapping(duration, {
                ch: ch_value
                for ch, ch_value in zip(sorted_channels, wf_values)
            }) for wf_values in random_values
        ]
    return get_unique_wfs.cache[key]
Esempio n. 10
0
 def test_slot(self):
     wf = ConstantWaveform.from_mapping(1, {'f': 3})
     with self.assertRaises(AttributeError):
         wf.asd = 5