def sample_heavy_set(
    compilation_result: CompilationResult,
    heavy_set: list[int],
    *,
    repetitions=10_000,
    sampler: cirq.Sampler = cirq.Simulator(),
) -> float:
    """Run a sampler over the given circuit and compute the percentage of its
       outputs that are in the heavy set.

    Args:
        compilation_result: All the information from the compilation.
        heavy_set: The previously-computed heavy set for the given circuit.
        repetitions: The number of times to sample the circuit.
        sampler: The sampler to run on the given circuit.

    Returns:
        A probability percentage, from 0 to 1, representing how many of the
        output bit-strings were in the heavy set.

    """
    mapping = compilation_result.mapping
    circuit = compilation_result.circuit

    # Add measure gates to the end of (a copy of) the circuit. Ensure that those
    # gates measure those in the given mapping, preserving this order.
    qubits = circuit.all_qubits()
    key = None
    if mapping:
        # Add any qubits that were not explicitly mapped, so they aren't lost in
        # the sorting.
        key = lambda q: mapping.get(q, q)
        qubits = frozenset(mapping.keys())

    # Don't do a single large measurement gate because then the key will be one
    # large string. Instead, do a bunch of single-qubit measurement gates so we
    # preserve the qubit keys.
    sorted_qubits = sorted(qubits, key=key)  # type: ignore[arg-type]
    circuit_copy = circuit + [cirq.measure(q) for q in sorted_qubits]

    # Run the sampler to compare each output against the Heavy Set.
    trial_result = sampler.run(program=circuit_copy, repetitions=repetitions)

    # Post-process the results, e.g. to handle error corrections.
    results = process_results(mapping, compilation_result.parity_map, trial_result)

    # Aggregate the results into bit-strings (since we are using individual
    # measurement gates).

    results = results.agg(lambda meas: cirq.value.big_endian_bits_to_int(meas), axis=1)
    # Compute the number of outputs that are in the heavy set.
    num_in_heavy_set = np.sum(np.isin(results, heavy_set)).item()

    # Return the number of Heavy outputs over the number of valid runs.
    return num_in_heavy_set / len(results)
Run a sampler over the given circuit and compute the percentage of its
       outputs that are in the heavy set.

    Args:
        compilation_result: All the information from the compilation.
        heavy_set: The previously-computed heavy set for the given circuit.
        repetitions: The number of times to sample the circuit.
        sampler: The sampler to run on the given circuit.

    Returns:
        A probability percentage, from 0 to 1, representing how many of the
        output bit-strings were in the heavy set.

    
cirq-core/cirq/contrib/quantum_volume/quantum_volume.py