Beispiel #1
0
def _get_xeb_result(
    qubit_pair: GridQubitPair,
    circuits: List['cirq.Circuit'],
    measurement_results: Sequence[List[np.ndarray]],
    num_circuits: int,
    repetitions: int,
    cycles: List[int],
) -> CrossEntropyResult:
    # pytest-cov is unable to detect that this function is called by a
    # multiprocessing Pool
    # coverage: ignore
    simulator = sim.Simulator()
    # Simulate circuits to get bitstring probabilities
    all_and_observed_probabilities: Dict[int, List[Tuple[
        np.ndarray, np.ndarray]]] = collections.defaultdict(list)
    empirical_probabilities: Dict[
        int, List[np.ndarray]] = collections.defaultdict(list)
    for i, circuit in enumerate(circuits):
        step_results = simulator.simulate_moment_steps(circuit,
                                                       qubit_order=qubit_pair)
        moment_index = 0
        for depth, measurements in zip(cycles, measurement_results[i]):
            while moment_index < 2 * depth:
                step_result = next(step_results)
                moment_index += 1
            # copy=False is safe because state_vector_to_probabilities will copy anyways
            amplitudes = step_result.state_vector(copy=False)
            probabilities = value.state_vector_to_probabilities(amplitudes)
            _, counts = np.unique(measurements, return_counts=True)
            empirical_probs = counts / len(measurements)
            empirical_probs = np.pad(empirical_probs,
                                     (0, 4 - len(empirical_probs)),
                                     mode='constant')
            all_and_observed_probabilities[depth].append(
                (probabilities, probabilities[measurements]))
            empirical_probabilities[depth].append(empirical_probs)
    # Compute XEB result
    data = []
    purity_data = []
    for depth in cycles:
        all_probabilities, observed_probabilities = zip(
            *all_and_observed_probabilities[depth])
        empirical_probs = np.asarray(empirical_probabilities[depth]).flatten()
        fidelity, _ = least_squares_xeb_fidelity_from_probabilities(
            hilbert_space_dimension=4,
            observed_probabilities=observed_probabilities,
            all_probabilities=all_probabilities,
            observable_from_probability=None,
            normalize_probabilities=True,
        )
        purity = purity_from_probabilities(4, empirical_probs)
        data.append(CrossEntropyPair(depth, fidelity))
        purity_data.append(SpecklePurityPair(depth, purity))
    return CrossEntropyResult(  # type: ignore
        data=data,
        repetitions=repetitions,
        purity_data=purity_data)
def _get_xeb_result(qubit_pair: GridQubitPair, circuits: List['cirq.Circuit'],
                    measurement_results: Sequence[List[np.ndarray]],
                    num_circuits: int, repetitions: int,
                    cycles: List[int]) -> CrossEntropyResult:
    # pytest-cov is unable to detect that this function is called by a
    # multiprocessing Pool
    # coverage: ignore
    simulator = sim.Simulator()
    # Simulate circuits to get bitstring probabilities
    all_and_observed_probabilities = collections.defaultdict(
        list)  # type: Dict[int, List[Tuple[np.ndarray, np.ndarray]]]
    for i, circuit in enumerate(circuits):
        step_results = simulator.simulate_moment_steps(circuit,
                                                       qubit_order=qubit_pair)
        moment_index = 0
        for depth, measurements in zip(cycles, measurement_results[i]):
            while moment_index < 2 * depth:
                step_result = next(step_results)
                moment_index += 1
            amplitudes = step_result.state_vector()
            probabilities = np.abs(amplitudes)**2
            all_and_observed_probabilities[depth].append(
                (probabilities, probabilities[measurements]))
    # Compute XEB result
    data = []
    for depth in cycles:
        all_probabilities, observed_probabilities = zip(
            *all_and_observed_probabilities[depth])
        fidelity, _ = least_squares_xeb_fidelity_from_probabilities(
            hilbert_space_dimension=4,
            observed_probabilities=observed_probabilities,
            all_probabilities=all_probabilities,
            observable_from_probability=None,
            normalize_probabilities=True)
        data.append(CrossEntropyPair(depth, fidelity))
    return CrossEntropyResult(  # type: ignore
        data=data, repetitions=repetitions)