Esempio n. 1
0
    def test_issue_584_uninitialized_table_sample(self):
        """issue 584"""
        d = 598.3333333333334 - 480
        tpt = TablePulseTemplate(
            entries={'P': [(0, 1.0, 'hold'), (d, 1.0, 'hold')]})
        with mock.patch('qupulse._program.waveforms.PULSE_TO_WAVEFORM_ERROR',
                        1e-6):
            wf = to_waveform(tpt.create_program())
            self.assertTrue(isclose(d, wf.duration, abs_tol=1e-6))

            start_time = 0.
            end_time = wf.duration
            sample_rate = 3.

            sample_count = (end_time - start_time) * sample_rate + 1

            times = np.linspace(float(start_time),
                                float(wf.duration),
                                num=int(sample_count),
                                dtype=float)
            times[-1] = np.nextafter(times[-1], times[-2])

            out = np.full_like(times, fill_value=np.nan)
            sampled = wf.get_sampled(channel='P',
                                     sample_times=times,
                                     output_array=out)

            expected = np.full_like(times, fill_value=1.)
            np.testing.assert_array_equal(expected, sampled)
Esempio n. 2
0
    def _create_program(self, *, scope: Scope,
                        measurement_mapping: Dict[str, Optional[str]],
                        channel_mapping: Dict[ChannelID, Optional[ChannelID]],
                        global_transformation: Optional[Transformation],
                        to_single_waveform: Set[Union[str, 'PulseTemplate']],
                        parent_loop: Loop):
        """Generic part of create program. This method handles to_single_waveform and the configuration of the
        transformer."""
        if self.identifier in to_single_waveform or self in to_single_waveform:
            root = Loop()

            if not scope.get_volatile_parameters().keys().isdisjoint(
                    self.parameter_names):
                raise NotImplementedError(
                    'A pulse template that has volatile parameters cannot be transformed into a '
                    'single waveform yet.')

            self._internal_create_program(
                scope=scope,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                global_transformation=None,
                to_single_waveform=to_single_waveform,
                parent_loop=root)

            waveform = to_waveform(root)

            if global_transformation:
                waveform = TransformingWaveform(waveform,
                                                global_transformation)

            # convert the nicely formatted measurement windows back into the old format again :(
            measurements = root.get_measurement_windows()
            measurement_window_list = []
            for measurement_name, (begins, lengths) in measurements.items():
                measurement_window_list.extend(
                    zip(itertools.repeat(measurement_name), begins, lengths))

            parent_loop.add_measurements(measurement_window_list)
            parent_loop.append_child(waveform=waveform)

        else:
            self._internal_create_program(
                scope=scope,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                to_single_waveform=to_single_waveform,
                global_transformation=global_transformation,
                parent_loop=parent_loop)
Esempio n. 3
0
def _render_loop(
    loop: Loop,
    render_measurements: bool,
) -> Tuple[Waveform, List[MeasurementWindow]]:
    """Transform program into single waveform and measurement windows.
    The specific implementation of render for Loop arguments."""
    waveform = to_waveform(loop)

    if render_measurements:
        measurement_dict = loop.get_measurement_windows()
        measurement_list = []
        for name, (begins, lengths) in measurement_dict.items():
            measurement_list.extend(
                zip(itertools.repeat(name), begins, lengths))
        measurements = sorted(measurement_list, key=operator.itemgetter(1))
    else:
        measurements = []

    return waveform, measurements
Esempio n. 4
0
    def _create_program(self, *, parameters: Dict[str, Parameter],
                        measurement_mapping: Dict[str, Optional[str]],
                        channel_mapping: Dict[ChannelID, Optional[ChannelID]],
                        global_transformation: Optional[Transformation],
                        to_single_waveform: Set[Union[str, 'PulseTemplate']],
                        parent_loop: Loop):
        """Generic part of create program. This method handles to_single_waveform and the configuration of the
        transformer."""
        if self.identifier in to_single_waveform or self in to_single_waveform:
            root = Loop()

            self._internal_create_program(
                parameters=parameters,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                global_transformation=None,
                to_single_waveform=to_single_waveform,
                parent_loop=root)

            waveform = to_waveform(root)

            if global_transformation:
                waveform = TransformingWaveform(waveform,
                                                global_transformation)

            # convert the nicely formatted measurement windows back into the old format again :(
            measurements = root.get_measurement_windows()
            measurement_window_list = []
            for measurement_name, (begins, lengths) in measurements.items():
                measurement_window_list.extend(
                    zip(itertools.repeat(measurement_name), begins, lengths))

            parent_loop.add_measurements(measurement_window_list)
            parent_loop.append_child(waveform=waveform)

        else:
            self._internal_create_program(
                parameters=parameters,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                to_single_waveform=to_single_waveform,
                global_transformation=global_transformation,
                parent_loop=parent_loop)
Esempio n. 5
0
def _render_loop(
    loop: Loop,
    sample_rate: Real,
    render_measurements: bool,
    time_slice: Tuple[Real, Real] = None
) -> Union[Tuple[np.ndarray, Dict[ChannelID, np.ndarray]], Tuple[
        np.ndarray, Dict[ChannelID, np.ndarray], List[MeasurementWindow]]]:
    """The specific implementation of render for Loop arguments."""
    waveform = to_waveform(loop)
    channels = waveform.defined_channels

    if time_slice is None:
        time_slice = (0, waveform.duration)
    elif time_slice[1] < time_slice[0] or time_slice[0] < 0 or time_slice[
            1] < 0:
        raise ValueError("time_slice is not valid.")

    sample_count = (time_slice[1] - time_slice[0]) * sample_rate + 1
    times = np.linspace(float(time_slice[0]),
                        float(time_slice[1]),
                        num=int(sample_count),
                        dtype=float)
    times[-1] = np.nextafter(times[-1], times[-2])

    voltages = {}

    for ch in channels:
        voltages[ch] = waveform.get_sampled(ch, times)

    if render_measurements:
        measurement_dict = loop.get_measurement_windows()
        measurement_list = []
        for name, (begins, lengths) in measurement_dict.items():
            measurement_list.extend(
                m for m in zip(itertools.repeat(name), begins, lengths)
                if m[1] + m[2] > time_slice[0] and m[1] < time_slice[1])
        measurements = sorted(measurement_list, key=operator.itemgetter(1))
    else:
        measurements = []

    return times, voltages, measurements