예제 #1
0
def _parse_waveform(
    data: types.PulseInstruction,
) -> Union[types.ParsedInstruction, types.OpaqueShape]:
    """A helper function that generates an array for the waveform with
    instruction metadata.

    Args:
        data: Instruction data set

    Raises:
        VisualizationError: When invalid instruction type is loaded.

    Returns:
        A data source to generate a drawing.
    """
    inst = data.inst

    meta = {}
    if isinstance(inst, instructions.Play):
        # pulse
        operand = inst.pulse
        if isinstance(operand, (pulse.ParametricPulse, pulse.SymbolicPulse)):
            # parametric pulse
            params = operand.parameters
            duration = params.pop("duration", None)
            if isinstance(duration, circuit.Parameter):
                duration = None

            if hasattr(operand, "pulse_type"):
                # Symbolic pulse
                meta["waveform shape"] = operand.pulse_type
            else:
                meta["waveform"] = operand.__class__.__name__

            meta.update({
                key: val.name if isinstance(val, circuit.Parameter) else val
                for key, val in params.items()
            })
            if data.is_opaque:
                # parametric pulse with unbound parameter
                if duration:
                    meta.update({
                        "duration (cycle time)":
                        inst.duration,
                        "duration (sec)":
                        inst.duration * data.dt if data.dt else "N/A",
                    })
                else:
                    meta.update({
                        "duration (cycle time)": "N/A",
                        "duration (sec)": "N/A"
                    })

                meta.update({
                    "t0 (cycle time)": data.t0,
                    "t0 (sec)": data.t0 * data.dt if data.dt else "N/A",
                    "phase": data.frame.phase,
                    "frequency": data.frame.freq,
                    "name": inst.name,
                })

                return types.OpaqueShape(duration=duration, meta=meta)
            else:
                # fixed shape parametric pulse
                pulse_data = operand.get_waveform()
        else:
            # waveform
            pulse_data = operand
        xdata = np.arange(pulse_data.duration) + data.t0
        ydata = pulse_data.samples
    elif isinstance(inst, instructions.Delay):
        # delay
        xdata = np.arange(inst.duration) + data.t0
        ydata = np.zeros(inst.duration)
    elif isinstance(inst, instructions.Acquire):
        # acquire
        xdata = np.arange(inst.duration) + data.t0
        ydata = np.ones(inst.duration)
        acq_data = {
            "memory slot": inst.mem_slot.name,
            "register slot": inst.reg_slot.name if inst.reg_slot else "N/A",
            "discriminator":
            inst.discriminator.name if inst.discriminator else "N/A",
            "kernel": inst.kernel.name if inst.kernel else "N/A",
        }
        meta.update(acq_data)
    else:
        raise VisualizationError(
            "Unsupported instruction {inst} by "
            "filled envelope.".format(inst=inst.__class__.__name__))

    meta.update({
        "duration (cycle time)": inst.duration,
        "duration (sec)": inst.duration * data.dt if data.dt else "N/A",
        "t0 (cycle time)": data.t0,
        "t0 (sec)": data.t0 * data.dt if data.dt else "N/A",
        "phase": data.frame.phase,
        "frequency": data.frame.freq,
        "name": inst.name,
    })

    return types.ParsedInstruction(xvals=xdata, yvals=ydata, meta=meta)
예제 #2
0
def _parse_waveform(
    data: types.PulseInstruction
) -> Union[types.ParsedInstruction, types.OpaqueShape]:
    """A helper function that generates an array for the waveform with
    instruction metadata.

    Args:
        data: Instruction data set

    Raises:
        VisualizationError: When invalid instruction type is loaded.

    Returns:
        A data source to generate a drawing.
    """
    inst = data.inst

    meta = dict()
    if isinstance(inst, instructions.Play):
        # pulse
        operand = inst.pulse
        if isinstance(operand, pulse.ParametricPulse):
            # parametric pulse
            params = operand.parameters
            duration = params.pop('duration', None)
            if isinstance(duration, circuit.Parameter):
                duration = None

            meta.update({'waveform shape': operand.__class__.__name__})
            meta.update({
                key: val.name if isinstance(val, circuit.Parameter) else val
                for key, val in params.items()
            })
            if data.is_opaque:
                # parametric pulse with unbound parameter
                if duration:
                    meta.update({
                        'duration (cycle time)':
                        inst.duration,
                        'duration (sec)':
                        inst.duration * data.dt if data.dt else 'N/A'
                    })
                else:
                    meta.update({
                        'duration (cycle time)': 'N/A',
                        'duration (sec)': 'N/A'
                    })

                meta.update({
                    't0 (cycle time)': data.t0,
                    't0 (sec)': data.t0 * data.dt if data.dt else 'N/A',
                    'phase': data.frame.phase,
                    'frequency': data.frame.freq,
                    'name': inst.name
                })

                return types.OpaqueShape(duration=duration, meta=meta)
            else:
                # fixed shape parametric pulse
                pulse_data = operand.get_waveform()
        else:
            # waveform
            pulse_data = operand
        xdata = np.arange(pulse_data.duration) + data.t0
        ydata = pulse_data.samples
    elif isinstance(inst, instructions.Delay):
        # delay
        xdata = np.arange(inst.duration) + data.t0
        ydata = np.zeros(inst.duration)
    elif isinstance(inst, instructions.Acquire):
        # acquire
        xdata = np.arange(inst.duration) + data.t0
        ydata = np.ones(inst.duration)
        acq_data = {
            'memory slot': inst.mem_slot.name,
            'register slot': inst.reg_slot.name if inst.reg_slot else 'N/A',
            'discriminator':
            inst.discriminator.name if inst.discriminator else 'N/A',
            'kernel': inst.kernel.name if inst.kernel else 'N/A'
        }
        meta.update(acq_data)
    else:
        raise VisualizationError(
            'Unsupported instruction {inst} by '
            'filled envelope.'.format(inst=inst.__class__.__name__))

    meta.update({
        'duration (cycle time)': inst.duration,
        'duration (sec)': inst.duration * data.dt if data.dt else 'N/A',
        't0 (cycle time)': data.t0,
        't0 (sec)': data.t0 * data.dt if data.dt else 'N/A',
        'phase': data.frame.phase,
        'frequency': data.frame.freq,
        'name': inst.name
    })

    return types.ParsedInstruction(xvals=xdata, yvals=ydata, meta=meta)