Skip to content

Compiler

compiler

QuantumTranspiler

Gate-level transpiler: decomposes non-native gates into the native {H, T, Tdg, CX, CZ} basis and performs basic circuit optimisations.

decompose_toffoli staticmethod

decompose_toffoli(c1: int, c2: int, t: int) -> List[Tuple]

Decompose CCX (Toffoli) into 15 native gates using the standard T/Tdg/CX Barenco decomposition.

Gate count: 6 CX + 7 single-qubit (H, T, Tdg) = 15 total.

Source code in dense_evolution/compiler.py
@staticmethod
def decompose_toffoli(c1: int, c2: int, t: int) -> List[Tuple]:
    """
    Decompose CCX (Toffoli) into 15 native gates using the
    standard T/Tdg/CX Barenco decomposition.

    Gate count: 6 CX + 7 single-qubit (H, T, Tdg) = 15 total.
    """
    return [
        ('h',   t),
        ('cx',  c2, t),  ('tdg', t),
        ('cx',  c1, t),  ('t',   t),
        ('cx',  c2, t),  ('tdg', t),
        ('cx',  c1, t),
        ('t',   c2),     ('t',   t),
        ('cx',  c1, c2), ('h',   t),
        ('t',   c1),     ('tdg', c2),
        ('cx',  c1, c2),
    ]

decompose_swap staticmethod

decompose_swap(q1: int, q2: int) -> List[Tuple]

Decompose SWAP into 3 CX gates.

Source code in dense_evolution/compiler.py
@staticmethod
def decompose_swap(q1: int, q2: int) -> List[Tuple]:
    """Decompose SWAP into 3 CX gates."""
    return [('cx', q1, q2), ('cx', q2, q1), ('cx', q1, q2)]

transpile classmethod

transpile(circuit: List[Tuple]) -> List[Tuple]

Expand CCX → 15 native gates and SWAP → 3 CX. All other gates are passed through unchanged.

Parameters

circuit : list of tuples (gate_name, qubit, ...)

Returns

Expanded circuit as a list of tuples.

Source code in dense_evolution/compiler.py
@classmethod
def transpile(cls, circuit: List[Tuple]) -> List[Tuple]:
    """
    Expand CCX → 15 native gates and SWAP → 3 CX.
    All other gates are passed through unchanged.

    Parameters
    ----------
    circuit : list of tuples (gate_name, qubit, ...)

    Returns
    -------
    Expanded circuit as a list of tuples.
    """
    out: List[Tuple] = []
    for cmd in circuit:
        name = cmd[0].lower()            # BUG FIX: was cmd.lower() on a tuple
        if name == 'ccx':
            out.extend(cls.decompose_toffoli(*cmd[1:4]))
        elif name == 'swap':
            out.extend(cls.decompose_swap(*cmd[1:3]))
        else:
            out.append(cmd)
    return out