def test_cross_entropy_result_dict_repr(): pair = tuple(cirq.LineQubit.range(2)) result = CrossEntropyResult( data=[CrossEntropyPair(2, 0.9), CrossEntropyPair(5, 0.5)], repetitions=1000 ) result_dict = CrossEntropyResultDict(results={pair: result}) cirq.testing.assert_equivalent_repr(result_dict)
def test_cross_entropy_result_purity_model_fails_with_no_data(): data = [ CrossEntropyPair(num_cycle=2, xeb_fidelity=0.9), CrossEntropyPair(num_cycle=4, xeb_fidelity=0.8), ] result = CrossEntropyResult(data=data, repetitions=1000) with pytest.raises(ValueError): _ = result.purity_depolarizing_model()
def test_cross_entropy_result_purity_model_fails_with_no_data(): with cirq.testing.assert_deprecated(_DEPRECATION_MESSAGE, deadline='v0.16'): data = [ CrossEntropyPair(num_cycle=2, xeb_fidelity=0.9), CrossEntropyPair(num_cycle=4, xeb_fidelity=0.8), ] result = CrossEntropyResult(data=data, repetitions=1000) with pytest.raises(ValueError): _ = result.purity_depolarizing_model()
def test_cross_entropy_result_repr(): result1 = CrossEntropyResult( data=[CrossEntropyPair(2, 0.9), CrossEntropyPair(5, 0.5)], repetitions=1000 ) result2 = CrossEntropyResult( data=[CrossEntropyPair(2, 0.9), CrossEntropyPair(5, 0.5)], repetitions=1000, purity_data=[SpecklePurityPair(2, 0.8), SpecklePurityPair(5, 0.3)], ) cirq.testing.assert_equivalent_repr(result1) cirq.testing.assert_equivalent_repr(result2)
def test_cross_entropy_result_dict_repr(): pair = tuple(cirq.LineQubit.range(2)) with cirq.testing.assert_deprecated(_DEPRECATION_MESSAGE, deadline='v0.16'): result = CrossEntropyResult( data=[CrossEntropyPair(2, 0.9), CrossEntropyPair(5, 0.5)], repetitions=1000) with cirq.testing.assert_deprecated(_DEPRECATION_MESSAGE, deadline='v0.16'): result_dict = CrossEntropyResultDict(results={pair: result}) with cirq.testing.assert_deprecated(_DEPRECATION_MESSAGE, deadline='v0.16', count=6): cirq.testing.assert_equivalent_repr(result_dict)
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 test_cross_entropy_result_repr(): with cirq.testing.assert_deprecated(_DEPRECATION_MESSAGE, deadline='v0.16'): result1 = CrossEntropyResult( data=[CrossEntropyPair(2, 0.9), CrossEntropyPair(5, 0.5)], repetitions=1000) with cirq.testing.assert_deprecated(_DEPRECATION_MESSAGE, deadline='v0.16'): result2 = CrossEntropyResult( data=[CrossEntropyPair(2, 0.9), CrossEntropyPair(5, 0.5)], repetitions=1000, purity_data=[SpecklePurityPair(2, 0.8), SpecklePurityPair(5, 0.3)], ) with cirq.testing.assert_deprecated(_DEPRECATION_MESSAGE, deadline='v0.16', count=6): cirq.testing.assert_equivalent_repr(result1) cirq.testing.assert_equivalent_repr(result2)
def test_cross_entropy_result_depolarizing_model(): prng = np.random.RandomState(59566) S = 0.8 p = 0.99 data = [ CrossEntropyPair(num_cycle=d, xeb_fidelity=S * p**d + prng.normal(scale=0.01)) for d in range(10, 411, 20) ] result = CrossEntropyResult(data=data, repetitions=1000) model = result.depolarizing_model() np.testing.assert_allclose(model.spam_depolarization, S, atol=1e-2) np.testing.assert_allclose(model.cycle_depolarization, p, atol=1e-2)
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)
def test_cross_entropy_result_depolarizing_models(): with cirq.testing.assert_deprecated(_DEPRECATION_MESSAGE, deadline='v0.16'): prng = np.random.RandomState(59566) S = 0.8 p = 0.99 data = [ CrossEntropyPair(num_cycle=d, xeb_fidelity=S * p**d + prng.normal(scale=0.01)) for d in range(10, 211, 20) ] purity_data = [ SpecklePurityPair(num_cycle=d, purity=S * p**(2 * d) + prng.normal(scale=0.01)) for d in range(10, 211, 20) ] result = CrossEntropyResult(data=data, repetitions=1000, purity_data=purity_data) model = result.depolarizing_model() purity_model = result.purity_depolarizing_model() np.testing.assert_allclose(model.spam_depolarization, S, atol=1e-2) np.testing.assert_allclose(model.cycle_depolarization, p, atol=1e-2) np.testing.assert_allclose(purity_model.purity, p**2, atol=1e-2)
def test_cross_entropy_result_repr(): result = CrossEntropyResult( data=[CrossEntropyPair(2, 0.9), CrossEntropyPair(5, 0.5)], repetitions=1000) cirq.testing.assert_equivalent_repr(result)