def synchronize_terminal_measurements(
    circuit: cirq.AbstractCircuit,
    *,
    context: cirq.TransformerContext | None = None,
    after_other_operations: bool = True,
) -> cirq.Circuit:
    """Move measurements to the end of the circuit.

    Move all measurements in a circuit to the final moment, if it can accommodate them (without
    overlapping with other operations). If `after_other_operations` is true, then a new moment will
    be added to the end of the circuit containing all the measurements that should be brought
    forward.

    Args:
          circuit: Input circuit to transform.
          context: `cirq.TransformerContext` storing common configurable options for transformers.
          after_other_operations: Set by default. If the circuit's final moment contains
                non-measurement operations and this is set then a new empty moment is appended to
                the circuit before pushing measurements to the end.
    Returns:
          Copy of the transformed input circuit.
    """
    if context is None:
        context = transformer_api.TransformerContext()
    terminal_measurements = [
        (i, op)
        for i, op in find_terminal_measurements(circuit)
        if set(op.tags).isdisjoint(context.tags_to_ignore)
    ]
    ret = circuit.unfreeze(copy=True)
    if not terminal_measurements:
        return ret

    ret.batch_remove(terminal_measurements)
    if ret[-1] and after_other_operations:
        ret.append(circuits.Moment())
    ret[-1] = ret[-1].with_operations(op for _, op in terminal_measurements)
    return ret
Move measurements to the end of the circuit.

    Move all measurements in a circuit to the final moment, if it can accommodate them (without
    overlapping with other operations). If `after_other_operations` is true, then a new moment will
    be added to the end of the circuit containing all the measurements that should be brought
    forward.

    Args:
          circuit: Input circuit to transform.
          context: `cirq.TransformerContext` storing common configurable options for transformers.
          after_other_operations: Set by default. If the circuit's final moment contains
                non-measurement operations and this is set then a new empty moment is appended to
                the circuit before pushing measurements to the end.
    Returns:
          Copy of the transformed input circuit.
    
cirq-core/cirq/transformers/synchronize_terminal_measurements.py