def estimate_fidelities(
    sampling_results: Sequence[dict[str, np.ndarray]],
    simulation_results: (
        Sequence[Sequence[np.ndarray]] | dict[_QUBIT_PAIR_T, Sequence[Sequence[np.ndarray]]]
    ),
    cycle_depths: Sequence[int],
    wide_circuits_info: Sequence[XEBWideCircuitInfo],
    pairs: Sequence[_QUBIT_PAIR_T],
    num_templates: int,
) -> Sequence[XEBFidelity]:
    """Estimates the fidelities from the given sampling and simulation results.

    Args:
        sampling_results: The result of `sample_all_circuits`.
        simulation_results: The result of `simulate_circuit_library`,
        cycle_depths: The sequence of cycle depths,
        wide_circuits_info: Sequence of XEBWideCircuitInfo detailing describing
            the sampled circuits.
        pairs: The qubit pairs being tests,
        num_templates: The number of circuit templates used for benchmarking,

    Returns:
        A sequence of XEBFidelity objects.
    """

    sampled_probabilities = _reshape_sampling_results(
        sampling_results, cycle_depths, wide_circuits_info, pairs, num_templates
    )

    pure_probabilities = _reshape_simulation_results(
        simulation_results, cycle_depths, pairs, num_templates
    )

    records = []
    for pair in pairs:
        for depth_idx, cycle_depth in enumerate(cycle_depths):
            numerator = 0.0
            denominator = 0.0
            for template_idx in range(num_templates):
                pure_probs = pure_probabilities[pair][depth_idx][template_idx]
                sampled_probs = sampled_probabilities[pair][depth_idx][template_idx]
                if len(sampled_probs) == 0:
                    continue
                assert (
                    len(sampled_probs) == 4
                ), f'{pair=} {cycle_depth=} {template_idx=}: {sampled_probs=}'
                p_uniform = np.ones_like(pure_probs) / len(pure_probs)
                pure_probs /= pure_probs.sum()
                sampled_probs /= sampled_probs.sum()

                h_up = _cross_entropy(p_uniform, pure_probs)  # H[uniform, pure probs]
                h_sp = _cross_entropy(sampled_probs, pure_probs)  # H[sampled probs, pure probs]
                h_pp = _cross_entropy(pure_probs, pure_probs)  # H[pure probs]

                y = h_up - h_sp
                x = h_up - h_pp
                numerator += x * y
                denominator += x**2
            fidelity = numerator / denominator
            records.append(XEBFidelity(pair=pair, cycle_depth=cycle_depth, fidelity=fidelity))
    return records
Estimates the fidelities from the given sampling and simulation results.

    Args:
        sampling_results: The result of `sample_all_circuits`.
        simulation_results: The result of `simulate_circuit_library`,
        cycle_depths: The sequence of cycle depths,
        wide_circuits_info: Sequence of XEBWideCircuitInfo detailing describing
            the sampled circuits.
        pairs: The qubit pairs being tests,
        num_templates: The number of circuit templates used for benchmarking,

    Returns:
        A sequence of XEBFidelity objects.
    
cirq-core/cirq/experiments/benchmarking/parallel_xeb.py