Replace every moment containing acquaintance gates (after
    rectification) with a generalized swap network, with the partition
    given by the acquaintance gates in that moment (and singletons for the
    free qubits). Accounts for reversing effect of swap networks.

    Args:
        circuit: The acquaintance strategy.
        qubit_order: The qubits, in order, on which the replacing swap network
            gate acts on.
        acquaintance_size: The acquaintance size of the new swap network gate.
        swap_gate: The gate used to swap logical indices.

    Returns: Whether or not the overall effect of the inserted swap network
        gates is to reverse the order of the qubits, i.e. the parity of the
        number of swap network gates inserted.

    Raises:
        TypeError: circuit is not an acquaintance strategy.

def replace_acquaintance_with_swap_network(
        circuit: circuits.Circuit,
        qubit_order: Sequence[ops.Qid],
        acquaintance_size: Optional[int] = 0,
        swap_gate: ops.Gate = ops.SWAP
        ) -> bool:
    """
    Replace every moment containing acquaintance gates (after
    rectification) with a generalized swap network, with the partition
    given by the acquaintance gates in that moment (and singletons for the
    free qubits). Accounts for reversing effect of swap networks.

    Args:
        circuit: The acquaintance strategy.
        qubit_order: The qubits, in order, on which the replacing swap network
            gate acts on.
        acquaintance_size: The acquaintance size of the new swap network gate.
        swap_gate: The gate used to swap logical indices.

    Returns: Whether or not the overall effect of the inserted swap network
        gates is to reverse the order of the qubits, i.e. the parity of the
        number of swap network gates inserted.

    Raises:
        TypeError: circuit is not an acquaintance strategy.
    """

    if not is_acquaintance_strategy(circuit):
        raise TypeError('not is_acquaintance_strategy(circuit)')

    rectify_acquaintance_strategy(circuit)
    reflected = False
    reverse_map = {q: r for q, r in zip(qubit_order, reversed(qubit_order))}
    for moment_index, moment in enumerate(circuit):
        if reflected:
            moment = moment.transform_qubits(reverse_map.__getitem__)
        if all(isinstance(op.gate, AcquaintanceOpportunityGate)
                for op in moment.operations):
            swap_network_gate = SwapNetworkGate.from_operations(
                    qubit_order, moment.operations,
                    acquaintance_size, swap_gate)
            swap_network_op = swap_network_gate(*qubit_order)
            moment = ops.Moment([swap_network_op])
            reflected = not reflected
        circuit._moments[moment_index] = moment
    return reflected