def _estimate_pauli_traces_general(
    qubits: list[cirq.Qid], circuit: cirq.Circuit, n_measured_operators: int | None
) -> list[PauliTrace]:
    """Estimates the Pauli traces in case the circuit is not Clifford.

    In this case we cannot use the speedup implemented in the function
    _estimate_pauli_traces_clifford() above, and so do a slow, density matrix
    simulation.

    Args:
        qubits: The list of qubits.
        circuit: The (non Clifford) circuit.
        n_measured_operators: The total number of Pauli measurements, or None to
            explore each Pauli state once.

    Returns:
        A list of Pauli states (represented as tuples of Pauli string, rho_i,
            and probability.
    """

    n_qubits = len(qubits)

    dense_simulator = cirq.DensityMatrixSimulator()
    # rho in https://arxiv.org/abs/1104.3835
    clean_density_matrix = dense_simulator.simulate(circuit).final_density_matrix

    all_operators = itertools.product([cirq.I, cirq.X, cirq.Y, cirq.Z], repeat=n_qubits)
    if n_measured_operators is not None:
        dense_operators = random.sample(tuple(all_operators), n_measured_operators)
    else:
        dense_operators = list(all_operators)

    pauli_traces: list[PauliTrace] = []
    for P_i in dense_operators:
        pauli_string: cirq.PauliString[cirq.Qid] = cirq.PauliString(dict(zip(qubits, P_i)))
        rho_i, Pr_i = compute_characteristic_function(pauli_string, qubits, clean_density_matrix)
        pauli_traces.append(PauliTrace(P_i=pauli_string, rho_i=rho_i, Pr_i=Pr_i))
    return pauli_traces
Estimates the Pauli traces in case the circuit is not Clifford.

    In this case we cannot use the speedup implemented in the function
    _estimate_pauli_traces_clifford() above, and so do a slow, density matrix
    simulation.

    Args:
        qubits: The list of qubits.
        circuit: The (non Clifford) circuit.
        n_measured_operators: The total number of Pauli measurements, or None to
            explore each Pauli state once.

    Returns:
        A list of Pauli states (represented as tuples of Pauli string, rho_i,
            and probability.
    
examples/direct_fidelity_estimation.py