def _try_merge_single_qubit_ops_of_two_moments(
    m1: circuits.Moment, m2: circuits.Moment
) -> tuple[circuits.Moment, ...]:
    """Merge single qubit ops of 2 moments if possible, returns 2 moments otherwise."""
    for q in m1.qubits & m2.qubits:
        op1 = m1.operation_at(q)
        op2 = m2.operation_at(q)
        if any(
            not (_is_single_qubit_operation(op) and has_unitary(op))
            for op in [op1, op2]
            if op is not None
        ):
            return (m1, m2)
    merged_ops: set[ops.Operation] = set()
    # Merge all operators on q to a single op.
    for q in m1.qubits | m2.qubits:
        # ops_on_q may contain 1 op or 2 ops.
        ops_on_q = [op for op in [m.operation_at(q) for m in [m1, m2]] if op is not None]
        merged_ops.add(_merge_single_qubit_ops_to_phxz(q, tuple(ops_on_q)))
    return (circuits.Moment(merged_ops),)
Merge single qubit ops of 2 moments if possible, returns 2 moments otherwise.
cirq-core/cirq/transformers/dynamical_decoupling.py