Beispiel #1
0
    def test_text_data_equivalent_abstract_coord(self):
        """Test TextData equivalent check with abstract coordinate."""
        obj1 = drawings.TextData(
            data_type=types.LabelType.GATE_NAME,
            bit=self.qubits[0],
            xval=types.AbstractCoordinate.LEFT,
            yval=types.AbstractCoordinate.BOTTOM,
            text="test",
            latex="test",
            meta=self.meta1,
            styles=self.style1,
        )

        obj2 = drawings.TextData(
            data_type=types.LabelType.GATE_NAME,
            bit=self.qubits[0],
            xval=types.AbstractCoordinate.LEFT,
            yval=types.AbstractCoordinate.BOTTOM,
            text="test",
            latex="test",
            meta=self.meta2,
            styles=self.style2,
        )

        self.assertEqual(obj1, obj2)
Beispiel #2
0
    def test_text_data_equivalent(self):
        """Test TextData equivalent check."""
        obj1 = drawings.TextData(
            data_type=types.LabelType.GATE_NAME,
            bit=self.qubits[0],
            xval=0,
            yval=0,
            text="test",
            latex="test",
            meta=self.meta1,
            styles=self.style1,
        )

        obj2 = drawings.TextData(
            data_type=types.LabelType.GATE_NAME,
            bit=self.qubits[0],
            xval=0,
            yval=0,
            text="test",
            latex="test",
            meta=self.meta2,
            styles=self.style2,
        )

        self.assertEqual(obj1, obj2)
Beispiel #3
0
def gen_bit_name(bit: types.Bits,
                 formatter: Dict[str, Any]) -> List[drawings.TextData]:
    """Generate bit label.

    Stylesheet:
        - `bit_name` style is applied.

    Args:
        bit: Bit object associated to this drawing.
        formatter: Dictionary of stylesheet settings.

    Returns:
        List of `TextData` drawings.
    """
    styles = {
        'zorder': formatter['layer.bit_name'],
        'color': formatter['color.bit_name'],
        'size': formatter['text_size.bit_name'],
        'va': 'center',
        'ha': 'right'
    }

    label_plain = '{name}'.format(name=bit.register.name)
    label_latex = r'{{\rm {register}}}_{{{index}}}'.format(
        register=bit.register.prefix, index=bit.index)

    drawing = drawings.TextData(data_type=types.LabelType.BIT_NAME,
                                xval=types.AbstractCoordinate.LEFT,
                                yval=0,
                                bit=bit,
                                text=label_plain,
                                latex=label_latex,
                                styles=styles)

    return [drawing]
Beispiel #4
0
def gen_short_gate_name(gate: types.ScheduledGate,
                        formatter: Dict[str, Any]) -> List[drawings.TextData]:
    """Generate gate name.

    Only operand name is shown.

    Stylesheet:
        - `gate_name` style is applied.
        - `gate_latex_repr` key is used to find the latex representation of the gate name.

    Args:
        gate: Gate information source.
        formatter: Dictionary of stylesheet settings.

    Returns:
        List of `TextData` drawings.
    """
    if gate.duration > 0:
        # gate with finite duration pulse
        v_align = "center"
        v_pos = 0
    else:
        # frame change
        v_align = "bottom"
        v_pos = formatter["label_offset.frame_change"]

    styles = {
        "zorder": formatter["layer.gate_name"],
        "color": formatter["color.gate_name"],
        "size": formatter["text_size.gate_name"],
        "va": v_align,
        "ha": "center",
    }
    # find latex representation
    default_name = r"{{\rm {name}}}".format(name=gate.operand.name)
    latex_name = formatter["latex_symbol.gates"].get(gate.operand.name,
                                                     default_name)

    label_plain = "{name}".format(name=gate.operand.name)
    label_latex = "{name}".format(name=latex_name)

    # assign special name for delay to filtering
    if gate.operand.name == "delay":
        data_type = types.LabelType.DELAY
    else:
        data_type = types.LabelType.GATE_NAME

    drawing = drawings.TextData(
        data_type=data_type,
        xval=gate.t0 + 0.5 * gate.duration,
        yval=v_pos,
        bit=gate.bits[gate.bit_position],
        text=label_plain,
        latex=label_latex,
        styles=styles,
    )

    return [drawing]
Beispiel #5
0
def gen_bit_name(bit: types.Bits,
                 formatter: Dict[str, Any]) -> List[drawings.TextData]:
    """Generate bit label.

    Stylesheet:
        - `bit_name` style is applied.

    Args:
        bit: Bit object associated to this drawing.
        formatter: Dictionary of stylesheet settings.

    Returns:
        List of `TextData` drawings.
    """
    styles = {
        "zorder": formatter["layer.bit_name"],
        "color": formatter["color.bit_name"],
        "size": formatter["text_size.bit_name"],
        "va": "center",
        "ha": "right",
    }

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        label_plain = "{name}".format(name=bit.register.name)
        label_latex = r"{{\rm {register}}}_{{{index}}}".format(
            register=bit.register.prefix, index=bit.index)

    drawing = drawings.TextData(
        data_type=types.LabelType.BIT_NAME,
        xval=types.AbstractCoordinate.LEFT,
        yval=0,
        bit=bit,
        text=label_plain,
        latex=label_latex,
        styles=styles,
    )

    return [drawing]
Beispiel #6
0
def gen_sched_gate(
    gate: types.ScheduledGate,
    formatter: Dict[str, Any],
) -> List[Union[drawings.TextData, drawings.BoxData]]:
    """Generate time bucket or symbol of scheduled gate.

    If gate duration is zero or frame change a symbol is generated instead of time box.
    The face color of gates depends on the operand type.

    Stylesheet:
        - The `gate` style is applied for finite duration gate.
        - The `frame_change` style is applied for zero duration gate.
        - The `gate_face_color` style is applied for face color.

    Args:
        gate: Gate information source.
        formatter: Dictionary of stylesheet settings.

    Returns:
        List of `TextData` or `BoxData` drawings.
    """
    try:
        unitary = str(gate.operand.to_matrix())
    except (AttributeError, CircuitError):
        unitary = "n/a"

    try:
        label = gate.operand.label or "n/a"
    except AttributeError:
        label = "n/a"

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        meta = {
            "name": gate.operand.name,
            "label": label,
            "bits": ", ".join([bit.register.name for bit in gate.bits]),
            "t0": gate.t0,
            "duration": gate.duration,
            "unitary": unitary,
            "parameters": ", ".join(map(str, gate.operand.params)),
        }

    # find color
    color = formatter["color.gates"].get(gate.operand.name,
                                         formatter["color.default_gate"])

    if gate.duration > 0:
        # gate with finite duration pulse
        styles = {
            "zorder": formatter["layer.gate"],
            "facecolor": color,
            "alpha": formatter["alpha.gate"],
            "linewidth": formatter["line_width.gate"],
        }

        # assign special name to delay for filtering
        if gate.operand.name == "delay":
            data_type = types.BoxType.DELAY
        else:
            data_type = types.BoxType.SCHED_GATE

        drawing = drawings.BoxData(
            data_type=data_type,
            xvals=[gate.t0, gate.t0 + gate.duration],
            yvals=[
                -0.5 * formatter["box_height.gate"],
                0.5 * formatter["box_height.gate"]
            ],
            bit=gate.bits[gate.bit_position],
            meta=meta,
            styles=styles,
        )
    else:
        # frame change
        styles = {
            "zorder": formatter["layer.frame_change"],
            "color": color,
            "size": formatter["text_size.frame_change"],
            "va": "center",
            "ha": "center",
        }
        unicode_symbol = formatter["unicode_symbol.frame_change"]
        latex_symbol = formatter["latex_symbol.frame_change"]

        drawing = drawings.TextData(
            data_type=types.SymbolType.FRAME,
            bit=gate.bits[gate.bit_position],
            xval=gate.t0,
            yval=0,
            text=unicode_symbol,
            latex=latex_symbol,
            styles=styles,
        )

    return [drawing]
Beispiel #7
0
def gen_full_gate_name(gate: types.ScheduledGate,
                       formatter: Dict[str, Any]) -> List[drawings.TextData]:
    """Generate gate name.

    Parameters and associated bits are also shown.

    Stylesheet:
        - `gate_name` style is applied.
        - `gate_latex_repr` key is used to find the latex representation of the gate name.

    Args:
        gate: Gate information source.
        formatter: Dictionary of stylesheet settings.

    Returns:
        List of `TextData` drawings.
    """
    if gate.duration > 0:
        # gate with finite duration pulse
        v_align = "center"
        v_pos = 0
    else:
        # frame change
        v_align = "bottom"
        v_pos = formatter["label_offset.frame_change"]

    styles = {
        "zorder": formatter["layer.gate_name"],
        "color": formatter["color.gate_name"],
        "size": formatter["text_size.gate_name"],
        "va": v_align,
        "ha": "center",
    }
    # find latex representation
    default_name = r"{{\rm {name}}}".format(name=gate.operand.name)
    latex_name = formatter["latex_symbol.gates"].get(gate.operand.name,
                                                     default_name)

    label_plain = "{name}".format(name=gate.operand.name)
    label_latex = r"{name}".format(name=latex_name)

    # bit index
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        if len(gate.bits) > 1:
            bits_str = ", ".join(map(str, [bit.index for bit in gate.bits]))
            label_plain += "[{bits}]".format(bits=bits_str)
            label_latex += "[{bits}]".format(bits=bits_str)

    # parameter list
    params = []
    for val in gate.operand.params:
        try:
            params.append("{val:.2f}".format(val=float(val)))
        except ValueError:
            params.append("{val}".format(val=val))
    params_str = ", ".join(params)

    if params_str and gate.operand.name != "delay":
        label_plain += "({params})".format(params=params_str)
        label_latex += "({params})".format(params=params_str)

    # duration
    if gate.duration > 0:
        label_plain += "[{dur}]".format(dur=gate.duration)
        label_latex += "[{dur}]".format(dur=gate.duration)

    # assign special name to delay for filtering
    if gate.operand.name == "delay":
        data_type = types.LabelType.DELAY
    else:
        data_type = types.LabelType.GATE_NAME

    drawing = drawings.TextData(
        data_type=data_type,
        xval=gate.t0 + 0.5 * gate.duration,
        yval=v_pos,
        bit=gate.bits[gate.bit_position],
        text=label_plain,
        latex=label_latex,
        styles=styles,
    )

    return [drawing]
Beispiel #8
0
def gen_full_gate_name(gate: types.ScheduledGate,
                       formatter: Dict[str, Any]) -> List[drawings.TextData]:
    """Generate gate name.

    Parameters and associated bits are also shown.

    Stylesheet:
        - `gate_name` style is applied.
        - `gate_latex_repr` key is used to find the latex representation of the gate name.

    Args:
        gate: Gate information source.
        formatter: Dictionary of stylesheet settings.

    Returns:
        List of `TextData` drawings.
    """
    if gate.duration > 0:
        # gate with finite duration pulse
        v_align = 'center'
        v_pos = 0
    else:
        # frame change
        v_align = 'bottom'
        v_pos = formatter['label_offset.frame_change']

    styles = {
        'zorder': formatter['layer.gate_name'],
        'color': formatter['color.gate_name'],
        'size': formatter['text_size.gate_name'],
        'va': v_align,
        'ha': 'center'
    }
    # find latex representation
    default_name = r'{{\rm {name}}}'.format(name=gate.operand.name)
    latex_name = formatter['latex_symbol.gates'].get(gate.operand.name,
                                                     default_name)

    label_plain = '{name}'.format(name=gate.operand.name)
    label_latex = r'{name}'.format(name=latex_name)

    # bit index
    if len(gate.bits) > 1:
        bits_str = ', '.join(map(str, [bit.index for bit in gate.bits]))
        label_plain += '[{bits}]'.format(bits=bits_str)
        label_latex += '[{bits}]'.format(bits=bits_str)

    # parameter list
    params = []
    for val in gate.operand.params:
        try:
            params.append('{val:.2f}'.format(val=float(val)))
        except ValueError:
            params.append('{val}'.format(val=val))
    params_str = ', '.join(params)

    if params_str and gate.operand.name != 'delay':
        label_plain += '({params})'.format(params=params_str)
        label_latex += '({params})'.format(params=params_str)

    # duration
    if gate.duration > 0:
        label_plain += '[{dur}]'.format(dur=gate.duration)
        label_latex += '[{dur}]'.format(dur=gate.duration)

    # assign special name to delay for filtering
    if gate.operand.name == 'delay':
        data_type = types.LabelType.DELAY
    else:
        data_type = types.LabelType.GATE_NAME

    drawing = drawings.TextData(data_type=data_type,
                                xval=gate.t0 + 0.5 * gate.duration,
                                yval=v_pos,
                                bit=gate.bits[gate.bit_position],
                                text=label_plain,
                                latex=label_latex,
                                styles=styles)

    return [drawing]
Beispiel #9
0
def gen_sched_gate(
    gate: types.ScheduledGate,
    formatter: Dict[str, Any],
) -> List[Union[drawings.TextData, drawings.BoxData]]:
    """Generate time bucket or symbol of scheduled gate.

    If gate duration is zero or frame change a symbol is generated instead of time box.
    The face color of gates depends on the operand type.

    Stylesheet:
        - The `gate` style is applied for finite duration gate.
        - The `frame_change` style is applied for zero duration gate.
        - The `gate_face_color` style is applied for face color.

    Args:
        gate: Gate information source.
        formatter: Dictionary of stylesheet settings.

    Returns:
        List of `TextData` or `BoxData` drawings.
    """
    try:
        unitary = str(gate.operand.to_matrix())
    except (AttributeError, CircuitError):
        unitary = 'n/a'

    try:
        label = gate.operand.label or 'n/a'
    except AttributeError:
        label = 'n/a'

    meta = {
        'name': gate.operand.name,
        'label': label,
        'bits': ', '.join([bit.register.name for bit in gate.bits]),
        't0': gate.t0,
        'duration': gate.duration,
        'unitary': unitary,
        'parameters': ', '.join(map(str, gate.operand.params))
    }

    # find color
    color = formatter['color.gates'].get(gate.operand.name,
                                         formatter['color.default_gate'])

    if gate.duration > 0:
        # gate with finite duration pulse
        styles = {
            'zorder': formatter['layer.gate'],
            'facecolor': color,
            'alpha': formatter['alpha.gate'],
            'linewidth': formatter['line_width.gate']
        }

        # assign special name to delay for filtering
        if gate.operand.name == 'delay':
            data_type = types.BoxType.DELAY
        else:
            data_type = types.BoxType.SCHED_GATE

        drawing = drawings.BoxData(data_type=data_type,
                                   xvals=[gate.t0, gate.t0 + gate.duration],
                                   yvals=[
                                       -0.5 * formatter['box_height.gate'],
                                       0.5 * formatter['box_height.gate']
                                   ],
                                   bit=gate.bits[gate.bit_position],
                                   meta=meta,
                                   styles=styles)
    else:
        # frame change
        styles = {
            'zorder': formatter['layer.frame_change'],
            'color': color,
            'size': formatter['text_size.frame_change'],
            'va': 'center',
            'ha': 'center'
        }
        unicode_symbol = formatter['unicode_symbol.frame_change']
        latex_symbol = formatter['latex_symbol.frame_change']

        drawing = drawings.TextData(data_type=types.SymbolType.FRAME,
                                    bit=gate.bits[gate.bit_position],
                                    xval=gate.t0,
                                    yval=0,
                                    text=unicode_symbol,
                                    latex=latex_symbol,
                                    styles=styles)

    return [drawing]