Skip to content

swap2cnot_decomposer

SWAP2CNOTDecomposer

Bases: Decomposer

Source code in opensquirrel/passes/decomposer/swap2cnot_decomposer.py
class SWAP2CNOTDecomposer(Decomposer):
    def decompose(self, gate: Gate) -> list[Gate]:
        """Predefined decomposition of SWAP gate to 3 CNOT gates.

        ![image](../../../_static/swap2cnot.png#only-light)
        ![image](../../../_static/swap2cnot_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 CNOT gates that decompose the SWAP gate.

        """
        if gate.name != "SWAP":
            return [gate]
        qubit0, qubit1 = gate.qubit_operands
        return [
            CNOT(qubit0, qubit1),
            CNOT(qubit1, qubit0),
            CNOT(qubit0, qubit1),
        ]

decompose

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

Predefined decomposition of SWAP gate to 3 CNOT gates.

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 CNOT gates that decompose the SWAP gate.

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

    ![image](../../../_static/swap2cnot.png#only-light)
    ![image](../../../_static/swap2cnot_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 CNOT gates that decompose the SWAP gate.

    """
    if gate.name != "SWAP":
        return [gate]
    qubit0, qubit1 = gate.qubit_operands
    return [
        CNOT(qubit0, qubit1),
        CNOT(qubit1, qubit0),
        CNOT(qubit0, qubit1),
    ]