def test_least_squares_xeb_fidelity_from_expectations(): prng_state = np.random.get_state() np.random.seed(0) depolarization = 0.5 n_qubits = 5 dim = 2**n_qubits n_circuits = 10 qubits = cirq.LineQubit.range(n_qubits) measured_expectations_lin = [] exact_expectations_lin = [] measured_expectations_log = [] exact_expectations_log = [] uniform_expectations_log = [] for _ in range(n_circuits): circuit = make_random_quantum_circuit(qubits, depth=12) bitstrings = sample_noisy_bitstrings( circuit, qubits, depolarization=depolarization, repetitions=5000 ) amplitudes = cirq.final_state_vector(circuit) probabilities = cirq.state_vector_to_probabilities(amplitudes) measured_expectations_lin.append(dim * np.mean(probabilities[bitstrings])) exact_expectations_lin.append(dim * np.sum(probabilities**2)) measured_expectations_log.append(np.mean(np.log(dim * probabilities[bitstrings]))) exact_expectations_log.append(np.sum(probabilities * np.log(dim * probabilities))) uniform_expectations_log.append(np.mean(np.log(dim * probabilities))) with cirq.testing.assert_deprecated( 'Use cirq.experiments.xeb_fitting', deadline='v0.16', count=2 ): f_lin, r_lin = cirq.experiments.least_squares_xeb_fidelity_from_expectations( measured_expectations_lin, exact_expectations_lin, [1.0] * n_circuits ) f_log, r_log = cirq.experiments.least_squares_xeb_fidelity_from_expectations( measured_expectations_log, exact_expectations_log, uniform_expectations_log ) assert np.isclose(f_lin, 1 - depolarization, atol=0.01) assert np.isclose(f_log, 1 - depolarization, atol=0.01) np.testing.assert_allclose(np.sum(np.array(r_lin) ** 2), 0.0, atol=1e-2) np.testing.assert_allclose(np.sum(np.array(r_log) ** 2), 0.0, atol=1e-2) np.random.set_state(prng_state)
def test_least_squares_xeb_fidelity_from_probabilities(): prng_state = np.random.get_state() np.random.seed(0) depolarization = 0.5 n_qubits = 5 dim = 2**n_qubits n_circuits = 10 qubits = cirq.LineQubit.range(n_qubits) all_probabilities = [] observed_probabilities = [] for _ in range(n_circuits): circuit = make_random_quantum_circuit(qubits, depth=12) bitstrings = sample_noisy_bitstrings( circuit, qubits, depolarization=depolarization, repetitions=5000 ) amplitudes = cirq.final_state_vector(circuit) probabilities = cirq.state_vector_to_probabilities(amplitudes) all_probabilities.append(probabilities) observed_probabilities.append(probabilities[bitstrings]) # 2 deprecation warnings for each of the following with cirq.testing.assert_deprecated( 'Use cirq.experiments.xeb_fitting', deadline='v0.16', count=6 ): f_lin, r_lin = cirq.least_squares_xeb_fidelity_from_probabilities( dim, observed_probabilities, all_probabilities, None, True ) f_log_np, r_log_np = cirq.least_squares_xeb_fidelity_from_probabilities( dim, observed_probabilities, all_probabilities, np.log, True ) f_log_math, r_log_math = cirq.least_squares_xeb_fidelity_from_probabilities( dim, observed_probabilities, all_probabilities, math.log, False ) assert np.isclose(f_lin, 1 - depolarization, atol=0.01) assert np.isclose(f_log_np, 1 - depolarization, atol=0.01) assert np.isclose(f_log_math, 1 - depolarization, atol=0.01) np.testing.assert_allclose(np.sum(np.array(r_lin) ** 2), 0.0, atol=1e-2) np.testing.assert_allclose(np.sum(np.array(r_log_np) ** 2), 0.0, atol=1e-2) np.testing.assert_allclose(np.sum(np.array(r_log_math) ** 2), 0.0, atol=1e-2) np.random.set_state(prng_state)
def _ref_simulate_2q_xeb_circuit(task: Dict[str, Any]): """Helper function for simulating a given (circuit, cycle_depth).""" circuit_i = task['circuit_i'] cycle_depth = task['cycle_depth'] circuit = task['circuit'] param_resolver = task['param_resolver'] circuit_depth = cycle_depth * 2 + 1 assert circuit_depth <= len(circuit) tcircuit = circuit[:circuit_depth] tcircuit = cirq.resolve_parameters_once(tcircuit, param_resolver=param_resolver) pure_sim = cirq.Simulator() psi = pure_sim.simulate(tcircuit) psi = psi.final_state_vector pure_probs = cirq.state_vector_to_probabilities(psi) return {'circuit_i': circuit_i, 'cycle_depth': cycle_depth, 'pure_probs': pure_probs}