def drop_terminal_measurements(
    circuit: cirq.AbstractCircuit,
    *,
    context: cirq.TransformerContext | None = transformer_api.TransformerContext(deep=True),
) -> cirq.Circuit:
    """Removes terminal measurements from a circuit.

    This transformer is helpful when trying to capture the final state vector
    of a circuit with many terminal measurements, as simulating the circuit
    with those measurements in place would otherwise collapse the final state.

    Args:
        circuit: The circuit to transform. It will not be modified.
        context: `cirq.TransformerContext` storing common configurable options
            for transformers. The default has `deep=True`, as "terminal
            measurements" is ill-defined without inspecting subcircuits;
            passing a context with `deep=False` will return an error.
    Returns:
        A copy of the circuit, with identity or X gates in place of terminal
        measurements.
    Raises:
        ValueError: if the circuit contains non-terminal measurements, or if
            the provided context has`deep=False`.
    """

    if context is None or not context.deep:
        raise ValueError(
            'Context has `deep=False`, but `deep=True` is required to drop terminal measurements.'
        )

    if not circuit.are_all_measurements_terminal():
        raise ValueError('Circuit contains a non-terminal measurement.')

    def flip_inversion(op: cirq.Operation, _) -> cirq.OP_TREE:
        if isinstance(op.gate, ops.MeasurementGate):
            return [
                (
                    (ops.X if b else ops.I)
                    if q.dimension == 2
                    else (
                        ops.MatrixGate(
                            # Per SimulationState.measure(), swap 0,1 but leave other dims alone
                            np.eye(q.dimension)[[1, 0, *range(2, q.dimension)]],
                            qid_shape=(q.dimension,),
                        )
                        if b
                        else ops.IdentityGate(qid_shape=(q.dimension,))
                    )
                ).on(q)
                for q, b in zip(op.qubits, op.gate.full_invert_mask())
            ]
        return op

    ignored = () if context is None else context.tags_to_ignore
    return transformer_primitives.map_operations(
        circuit, flip_inversion, deep=context.deep if context else True, tags_to_ignore=ignored
    ).unfreeze()
Removes terminal measurements from a circuit.

    This transformer is helpful when trying to capture the final state vector
    of a circuit with many terminal measurements, as simulating the circuit
    with those measurements in place would otherwise collapse the final state.

    Args:
        circuit: The circuit to transform. It will not be modified.
        context: `cirq.TransformerContext` storing common configurable options
            for transformers. The default has `deep=True`, as "terminal
            measurements" is ill-defined without inspecting subcircuits;
            passing a context with `deep=False` will return an error.
    Returns:
        A copy of the circuit, with identity or X gates in place of terminal
        measurements.
    Raises:
        ValueError: if the circuit contains non-terminal measurements, or if
            the provided context has`deep=False`.
    
cirq-core/cirq/transformers/measurement_transformers.py