cqlib-core 0.1.0-beta.5

The high-performance core foundation for the CQLib quantum computing ecosystem.
Documentation
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 2026
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

// Ising interaction decomposition rules.
//
// These rules own RXX/RYY/RZZ/RZX conversions and CX/CZ target paths.

// RZZ-native lowering: CZ = RZ(π/2)·RZ(π/2)·RZZ(-π/2) up to global phase.
rule decompose_cz_to_rzz {
    match { CZ 0 1 }
    rewrite { RZ(π/2) 0, RZ(π/2) 1, RZZ(-π/2) 0 1, GPhase(π/4) }
}

// CX = H·CZ·H; suitable for {H, RZ, RZZ} or intermediate lowering (not pure {RZ, RZZ}).
rule decompose_cx_to_rzz {
    match { CX 0 1 }
    rewrite { H 1, RZ(π/2) 0, RZ(π/2) 1, RZZ(-π/2) 0 1, H 1, GPhase(π/4) }
}

// CY = SDG_target · CX · S_target; CX lowered via CZ/RZZ as above.
rule decompose_cy_to_rzz {
    match { CY 0 1 }
    rewrite {
        SDG 1,
        H 1,
        RZ(π/2) 0,
        RZ(π/2) 1,
        RZZ(-π/2) 0 1,
        H 1,
        S 1,
        GPhase(π/4)
    }
}

rule decompose_rzz_to_cx {
    match { RZZ(theta) 0 1 }
    rewrite { CX 0 1, RZ(theta) 1, CX 0 1 }
}

rule decompose_rxx_to_cx {
    match { RXX(theta) 0 1 }
    rewrite { H 0, H 1, CX 0 1, RZ(theta) 1, CX 0 1, H 1, H 0 }
}

rule decompose_ryy_to_cx {
    match { RYY(theta) 0 1 }
    rewrite { RX(π/2) 0, RX(π/2) 1, CX 0 1, RZ(theta) 1, CX 0 1, RX(-π/2) 1, RX(-π/2) 0 }
}

rule decompose_rzx_to_cx {
    match { RZX(theta) 0 1 }
    rewrite { H 1, CX 0 1, RZ(theta) 1, CX 0 1, H 1 }
}

rule decompose_rxx_to_rzz {
    match { RXX(theta) 0 1 }
    rewrite { H 0, H 1, RZZ(theta) 0 1, H 1, H 0 }
}

rule decompose_rxx_to_ryy {
    match { RXX(theta) 0 1 }
    rewrite { H 0, H 1, RX(-π/2) 0, RX(-π/2) 1, RYY(theta) 0 1, RX(π/2) 1, RX(π/2) 0, H 1, H 0 }
}

rule decompose_rxx_to_rzx {
    match { RXX(theta) 0 1 }
    rewrite { H 0, RZX(theta) 0 1, H 0 }
}

rule decompose_ryy_to_rzz {
    match { RYY(theta) 0 1 }
    rewrite { RX(π/2) 0, RX(π/2) 1, RZZ(theta) 0 1, RX(-π/2) 1, RX(-π/2) 0 }
}

rule decompose_ryy_to_rxx {
    match { RYY(theta) 0 1 }
    rewrite { RX(π/2) 0, RX(π/2) 1, H 0, H 1, RXX(theta) 0 1, H 1, H 0, RX(-π/2) 1, RX(-π/2) 0 }
}

rule decompose_ryy_to_rzx {
    match { RYY(theta) 0 1 }
    rewrite { RX(π/2) 0, RX(π/2) 1, H 1, RZX(theta) 0 1, H 1, RX(-π/2) 1, RX(-π/2) 0 }
}

rule decompose_rzx_to_rzz {
    match { RZX(theta) 0 1 }
    rewrite { H 1, RZZ(theta) 0 1, H 1 }
}

rule decompose_rzx_to_rxx {
    match { RZX(theta) 0 1 }
    rewrite { H 0, RXX(theta) 0 1, H 0 }
}

rule decompose_rzx_to_ryy {
    match { RZX(theta) 0 1 }
    rewrite { H 1, RX(-π/2) 0, RX(-π/2) 1, RYY(theta) 0 1, RX(π/2) 1, RX(π/2) 0, H 1 }
}

rule decompose_rzz_to_rxx {
    match { RZZ(theta) 0 1 }
    rewrite { H 0, H 1, RXX(theta) 0 1, H 1, H 0 }
}

rule decompose_rzz_to_ryy {
    match { RZZ(theta) 0 1 }
    rewrite { RX(-π/2) 0, RX(-π/2) 1, RYY(theta) 0 1, RX(π/2) 1, RX(π/2) 0 }
}

rule decompose_rzz_to_rzx {
    match { RZZ(theta) 0 1 }
    rewrite { H 1, RZX(theta) 0 1, H 1 }
}

rule decompose_rzz_to_cz {
    match { RZZ(theta) 0 1 }
    rewrite { H 1, CZ 0 1, H 1, RZ(theta) 1, H 1, CZ 0 1, H 1 }
}

rule decompose_rxx_to_cz {
    match { RXX(theta) 0 1 }
    rewrite { H 0, CZ 0 1, H 1, RZ(theta) 1, H 1, CZ 0 1, H 0 }
}

rule decompose_ryy_to_cz {
    match { RYY(theta) 0 1 }
    rewrite { RX(π/2) 0, RX(π/2) 1, H 1, CZ 0 1, H 1, RZ(theta) 1, H 1, CZ 0 1, H 1, RX(-π/2) 1, RX(-π/2) 0 }
}

rule decompose_rzx_to_cz {
    match { RZX(theta) 0 1 }
    rewrite { CZ 0 1, H 1, RZ(theta) 1, H 1, CZ 0 1 }
}

rule decompose_cz_to_rx_ry_rzz {
    match { CZ 0 1 }
    rewrite { RX(-π/2) 0, RY(π/2) 0, RX(π/2) 0, RX(-π/2) 1, RY(π/2) 1, RX(π/2) 1, RZZ(-π/2) 0 1, GPhase(π/4) }
}

rule decompose_cx_to_rx_ry_rzz {
    match { CX 0 1 }
    rewrite { RY(π/2) 1, RX(π) 1, RX(-π/2) 0, RY(π/2) 0, RX(π/2) 0, RX(-π/2) 1, RY(π/2) 1, RX(π/2) 1, RZZ(-π/2) 0 1, RY(π/2) 1, RX(π) 1, GPhase(-3π/4) }
}

rule decompose_rxx_to_rx_ry_rzz {
    match { RXX(theta) 0 1 }
    rewrite { RY(-π/2) 0, RY(-π/2) 1, RZZ(theta) 0 1, RY(π/2) 1, RY(π/2) 0 }
}

rule decompose_ryy_to_rx_ry_rzz {
    match { RYY(theta) 0 1 }
    rewrite { RX(π/2) 0, RX(π/2) 1, RZZ(theta) 0 1, RX(-π/2) 1, RX(-π/2) 0 }
}

rule decompose_rzx_to_rx_ry_rzz {
    match { RZX(theta) 0 1 }
    rewrite { RY(-π/2) 1, RZZ(theta) 0 1, RY(π/2) 1 }
}

rule decompose_cx_to_rzx {
    match { CX 0 1 }
    rewrite { RZ(π/2) 0, RX(π/2) 1, RZX(-π/2) 0 1, GPhase(π/4) }
}

rule decompose_cz_to_rzx {
    match { CZ 0 1 }
    rewrite { H 1, RZ(π/2) 0, RX(π/2) 1, RZX(-π/2) 0 1, H 1, GPhase(π/4) }
}

rule decompose_cy_to_rzx {
    match { CY 0 1 }
    rewrite { SDG 1, RZ(π/2) 0, RX(π/2) 1, RZX(-π/2) 0 1, S 1, GPhase(π/4) }
}

rule decompose_swap_to_rzx {
    match { SWAP 0 1 }
    rewrite {
        RZ(π/2) 0, RX(π/2) 1, RZX(-π/2) 0 1,
        RZ(π/2) 1, RX(π/2) 0, RZX(-π/2) 1 0,
        RZ(π/2) 0, RX(π/2) 1, RZX(-π/2) 0 1,
        GPhase(3*π/4)
    }
}