Skip to content

swap2cz_decomposer

SWAP2CZDecomposer

Bases: Decomposer

Source code in opensquirrel/passes/decomposer/swap2cz_decomposer.py
class SWAP2CZDecomposer(Decomposer):
    def decompose(self, gate: Gate) -> list[Gate]:
        """Predefined decomposition of SWAP gate to 3 CZ gates and Ry rotations.

        ![image](../../../_static/swap2cz.png#only-light)
        ![image](../../../_static/swap2cz_dm.png#only-dark)

        Note:
            This decomposition preserves the global phase of the SWAP gate.

        Args:
            gate: SWAP gate to decompose.

        Returns:
            A sequence of 3 CZ gates and Ry rotations that decompose the SWAP gate.

        """
        if gate.name != "SWAP":
            return [gate]
        qubit0, qubit1 = gate.qubit_operands
        return [
            Ry(qubit1, -pi / 2),
            CZ(qubit0, qubit1),
            Ry(qubit1, pi / 2),
            Ry(qubit0, -pi / 2),
            CZ(qubit1, qubit0),
            Ry(qubit0, pi / 2),
            Ry(qubit1, -pi / 2),
            CZ(qubit0, qubit1),
            Ry(qubit1, pi / 2),
        ]

decompose

decompose(gate: Gate) -> list[Gate]

Predefined decomposition of SWAP gate to 3 CZ gates and Ry rotations.

image image

Note

This decomposition preserves the global phase of the SWAP gate.

Parameters:

Name Type Description Default
gate Gate

SWAP gate to decompose.

required

Returns:

Type Description
list[Gate]

A sequence of 3 CZ gates and Ry rotations that decompose the SWAP gate.

Source code in opensquirrel/passes/decomposer/swap2cz_decomposer.py
def decompose(self, gate: Gate) -> list[Gate]:
    """Predefined decomposition of SWAP gate to 3 CZ gates and Ry rotations.

    ![image](../../../_static/swap2cz.png#only-light)
    ![image](../../../_static/swap2cz_dm.png#only-dark)

    Note:
        This decomposition preserves the global phase of the SWAP gate.

    Args:
        gate: SWAP gate to decompose.

    Returns:
        A sequence of 3 CZ gates and Ry rotations that decompose the SWAP gate.

    """
    if gate.name != "SWAP":
        return [gate]
    qubit0, qubit1 = gate.qubit_operands
    return [
        Ry(qubit1, -pi / 2),
        CZ(qubit0, qubit1),
        Ry(qubit1, pi / 2),
        Ry(qubit0, -pi / 2),
        CZ(qubit1, qubit0),
        Ry(qubit0, pi / 2),
        Ry(qubit1, -pi / 2),
        CZ(qubit0, qubit1),
        Ry(qubit1, pi / 2),
    ]