Skip to content

cz_decomposer

CZDecomposer

Bases: Decomposer

Decomposes 2-qubit controlled unitary gates to CZ + Rx/Ry. Applying single-qubit gate fusion after this pass might be beneficial.

Source of the math: https://threeplusone.com/pubs/on_gates.pdf, chapter 7.5 "ABC decomposition"

Source code in opensquirrel/passes/decomposer/cz_decomposer.py
class CZDecomposer(Decomposer):
    """
    Decomposes 2-qubit controlled unitary gates to CZ + Rx/Ry.
    Applying single-qubit gate fusion after this pass might be beneficial.

    Source of the math: https://threeplusone.com/pubs/on_gates.pdf, chapter 7.5 "ABC decomposition"
    """

    def decompose(self, gate: Gate) -> list[Gate]:
        """Decomposes a controlled two-qubit gate into a sequence of (at most 2) CZ gates and
        single-qubit gates. It decomposes the CR, CRk, and CNOT controlled two-qubit gates.

        Uses the ABC decomposition procedure described in
        [Quantum Gates by G.E. Crooks (2024), Section 7.5](https://threeplusone.com/pubs/on_gates.pdf).

        Note:
            The SWAP gate is not a controlled two-qubit gate and is not decomposed by this pass.
            To decompose SWAP gates, use the
            [SWAP2CZDecomposer][opensquirrel.passes.decomposer.swap2cz_decomposer.SWAP2CZDecomposer]
            or the [SWAP2CNOTDecomposer][opensquirrel.passes.decomposer.swap2cnot_decomposer.SWAP2CNOTDecomposer].

        Args:
            gate (Gate): Two-qubit controlled gate to decompose.

        Returns:
            A sequence of (at most 2) CZ gates and single-qubit gates.

        """
        if not isinstance(gate, TwoQubitGate):
            return [gate]

        if not gate.controlled:
            # Do nothing:
            # - BlochSphereRotation's are only single-qubit,
            # - decomposing MatrixGate is currently not supported.
            return [gate]
        control_qubit, target_qubit = gate.qubit_operands
        target_gate = SingleQubitGate(qubit=target_qubit, gate_semantic=gate.controlled.target_bsr)

        # Perform XYX decomposition on the target gate.
        # This gives us an ABC decomposition (U = exp(i phase) * AZBZC, ABC = I) of the target gate.
        # See https://threeplusone.com/pubs/on_gates.pdf

        # Try special case first, see https://arxiv.org/pdf/quant-ph/9503016.pdf lemma 5.5
        # Note that here V = Rx(a) * Ry(th) * Rx(a) * Z to create V = AZBZ, with AB = I
        controlled_rotation_times_z = target_gate * Z(target_qubit)
        theta0_with_z, theta1_with_z, theta2_with_z = XYXDecomposer()._determine_rotation_angles(  # noqa: SLF001
            controlled_rotation_times_z.bsr.axis,
            controlled_rotation_times_z.bsr.angle,
        )
        if abs((theta0_with_z - theta2_with_z) % (2 * pi)) < ATOL:
            # The decomposition can use a single CZ according to the lemma.
            A = [Ry(target_qubit, theta1_with_z / 2), Rx(target_qubit, theta2_with_z)]  # noqa: N806
            B = [Rx(target_qubit, -theta2_with_z), Ry(target_qubit, -theta1_with_z / 2)]  # noqa: N806
            return filter_out_identities(
                [
                    *B,
                    CZ(control_qubit, target_qubit),
                    *A,
                    Rz(control_qubit, target_gate.bsr.phase - pi / 2),
                ],
            )

        theta0, theta1, theta2 = XYXDecomposer()._determine_rotation_angles(target_gate.bsr.axis, target_gate.bsr.angle)  # noqa: SLF001

        A = [Ry(target_qubit, theta1 / 2), Rx(target_qubit, theta2)]  # noqa: N806
        B = [Rx(target_qubit, -(theta0 + theta2) / 2), Ry(target_qubit, -theta1 / 2)]  # noqa: N806
        C = [Rx(target_qubit, (theta0 - theta2) / 2)]  # noqa: N806

        return filter_out_identities(
            [
                *C,
                CZ(control_qubit, target_qubit),
                *B,
                CZ(control_qubit, target_qubit),
                *A,
                Rz(control_qubit, target_gate.bsr.phase),
            ],
        )

decompose

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

Decomposes a controlled two-qubit gate into a sequence of (at most 2) CZ gates and single-qubit gates. It decomposes the CR, CRk, and CNOT controlled two-qubit gates.

Uses the ABC decomposition procedure described in Quantum Gates by G.E. Crooks (2024), Section 7.5.

Note

The SWAP gate is not a controlled two-qubit gate and is not decomposed by this pass. To decompose SWAP gates, use the SWAP2CZDecomposer or the SWAP2CNOTDecomposer.

Parameters:

Name Type Description Default
gate Gate

Two-qubit controlled gate to decompose.

required

Returns:

Type Description
list[Gate]

A sequence of (at most 2) CZ gates and single-qubit gates.

Source code in opensquirrel/passes/decomposer/cz_decomposer.py
def decompose(self, gate: Gate) -> list[Gate]:
    """Decomposes a controlled two-qubit gate into a sequence of (at most 2) CZ gates and
    single-qubit gates. It decomposes the CR, CRk, and CNOT controlled two-qubit gates.

    Uses the ABC decomposition procedure described in
    [Quantum Gates by G.E. Crooks (2024), Section 7.5](https://threeplusone.com/pubs/on_gates.pdf).

    Note:
        The SWAP gate is not a controlled two-qubit gate and is not decomposed by this pass.
        To decompose SWAP gates, use the
        [SWAP2CZDecomposer][opensquirrel.passes.decomposer.swap2cz_decomposer.SWAP2CZDecomposer]
        or the [SWAP2CNOTDecomposer][opensquirrel.passes.decomposer.swap2cnot_decomposer.SWAP2CNOTDecomposer].

    Args:
        gate (Gate): Two-qubit controlled gate to decompose.

    Returns:
        A sequence of (at most 2) CZ gates and single-qubit gates.

    """
    if not isinstance(gate, TwoQubitGate):
        return [gate]

    if not gate.controlled:
        # Do nothing:
        # - BlochSphereRotation's are only single-qubit,
        # - decomposing MatrixGate is currently not supported.
        return [gate]
    control_qubit, target_qubit = gate.qubit_operands
    target_gate = SingleQubitGate(qubit=target_qubit, gate_semantic=gate.controlled.target_bsr)

    # Perform XYX decomposition on the target gate.
    # This gives us an ABC decomposition (U = exp(i phase) * AZBZC, ABC = I) of the target gate.
    # See https://threeplusone.com/pubs/on_gates.pdf

    # Try special case first, see https://arxiv.org/pdf/quant-ph/9503016.pdf lemma 5.5
    # Note that here V = Rx(a) * Ry(th) * Rx(a) * Z to create V = AZBZ, with AB = I
    controlled_rotation_times_z = target_gate * Z(target_qubit)
    theta0_with_z, theta1_with_z, theta2_with_z = XYXDecomposer()._determine_rotation_angles(  # noqa: SLF001
        controlled_rotation_times_z.bsr.axis,
        controlled_rotation_times_z.bsr.angle,
    )
    if abs((theta0_with_z - theta2_with_z) % (2 * pi)) < ATOL:
        # The decomposition can use a single CZ according to the lemma.
        A = [Ry(target_qubit, theta1_with_z / 2), Rx(target_qubit, theta2_with_z)]  # noqa: N806
        B = [Rx(target_qubit, -theta2_with_z), Ry(target_qubit, -theta1_with_z / 2)]  # noqa: N806
        return filter_out_identities(
            [
                *B,
                CZ(control_qubit, target_qubit),
                *A,
                Rz(control_qubit, target_gate.bsr.phase - pi / 2),
            ],
        )

    theta0, theta1, theta2 = XYXDecomposer()._determine_rotation_angles(target_gate.bsr.axis, target_gate.bsr.angle)  # noqa: SLF001

    A = [Ry(target_qubit, theta1 / 2), Rx(target_qubit, theta2)]  # noqa: N806
    B = [Rx(target_qubit, -(theta0 + theta2) / 2), Ry(target_qubit, -theta1 / 2)]  # noqa: N806
    C = [Rx(target_qubit, (theta0 - theta2) / 2)]  # noqa: N806

    return filter_out_identities(
        [
            *C,
            CZ(control_qubit, target_qubit),
            *B,
            CZ(control_qubit, target_qubit),
            *A,
            Rz(control_qubit, target_gate.bsr.phase),
        ],
    )