Example #1
0
def gen_timeslot(bit: types.Bits,
                 formatter: Dict[str, Any]) -> List[drawings.BoxData]:
    """Generate time slot of associated bit.

    Stylesheet:
        - `timeslot` style is applied.

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

    Returns:
        List of `BoxData` drawings.
    """
    styles = {
        "zorder": formatter["layer.timeslot"],
        "alpha": formatter["alpha.timeslot"],
        "linewidth": formatter["line_width.timeslot"],
        "facecolor": formatter["color.timeslot"],
    }

    drawing = drawings.BoxData(
        data_type=types.BoxType.TIMELINE,
        xvals=[types.AbstractCoordinate.LEFT, types.AbstractCoordinate.RIGHT],
        yvals=[
            -0.5 * formatter["box_height.timeslot"],
            0.5 * formatter["box_height.timeslot"]
        ],
        bit=bit,
        styles=styles,
    )

    return [drawing]
    def test_box_data_equivalent(self):
        """Test BoxData equivalent check."""
        xs = [0, 1]
        ys = [0, 1]

        obj1 = drawings.BoxData(data_type=types.BoxType.SCHED_GATE,
                                bit=self.qubits[0],
                                xvals=xs,
                                yvals=ys,
                                meta=self.meta1,
                                styles=self.style1)

        obj2 = drawings.BoxData(data_type=types.BoxType.SCHED_GATE,
                                bit=self.qubits[0],
                                xvals=xs,
                                yvals=ys,
                                meta=self.meta2,
                                styles=self.style2)

        self.assertEqual(obj1, obj2)
    def test_box_data_equivalent_abstract_coord(self):
        """Test BoxData equivalent check with abstract coordinate."""
        xs = [types.AbstractCoordinate.LEFT, types.AbstractCoordinate.RIGHT]
        ys = [types.AbstractCoordinate.BOTTOM, types.AbstractCoordinate.TOP]

        obj1 = drawings.BoxData(data_type=types.BoxType.SCHED_GATE,
                                bit=self.qubits[0],
                                xvals=xs,
                                yvals=ys,
                                meta=self.meta1,
                                styles=self.style1)

        obj2 = drawings.BoxData(data_type=types.BoxType.SCHED_GATE,
                                bit=self.qubits[0],
                                xvals=xs,
                                yvals=ys,
                                meta=self.meta2,
                                styles=self.style2)

        self.assertEqual(obj1, obj2)
Example #4
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]
Example #5
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]