def marginalized_success_counts(dsrow, circ, target, qubits):
    """
    Marginalize the success counts over qubits.

    Parameters
    ----------
    dsrow : _DataSetRow
        The circuit outcome data to marginalize.

    circ : Circuit
        The circuit.

    target : str
        The ideal outcome, e.g. `"0010"`.

    qubits : tuple
        The qubit labels that are retained after the marginalization.

    Returns
    -------
    int
        The number of success counts.
    """
    if dsrow.total == 0:
        return 0
    else:
        # The rows of the circuit that we are interested in
        indices = [circ.line_labels.index(q) for q in qubits]
        # The ordering of this must be the same as what we compare it to.
        margtarget = ''.join([target[i] for i in indices])

        if qubits == circ.line_labels:
            try:
                return dsrow.counts[target]
            except:
                return 0

        else:

            success_counts = 0

            for (outbitstring,), counts in dsrow.counts.items():
                if ''.join([outbitstring[i] for i in indices]) == margtarget:
                    success_counts += counts

            return success_counts