def test_execute_with_cdr(circuit_type): circuit = random_x_z_circuit(LineQubit.range(2), n_moments=2, random_state=1) circuit = convert_from_mitiq(circuit, circuit_type) # Define observables for testing. sigma_z = np.diag([1, -1]) obs = np.kron(np.identity(2), sigma_z) obs2 = np.kron(sigma_z, sigma_z) obs_list = [np.diag(obs), np.diag(obs2)] exact_solution = [ calculate_observable(simulator_statevector(circuit), observable=obs) for obs in obs_list ] kwargs = { "method_select": "gaussian", "method_replace": "gaussian", "sigma_select": 0.5, "sigma_replace": 0.5, "random_state": 1, } num_circuits = 4 frac_non_cliff = 0.5 noisy_executor = partial(executor, noise_level=0.5) results0 = execute_with_cdr( circuit, noisy_executor, simulator_statevector, obs_list, num_circuits, frac_non_cliff, full_output=True, ) results1 = execute_with_cdr( circuit, noisy_executor, simulator_statevector, obs_list, num_circuits, frac_non_cliff, ansatz=linear_fit_function_no_intercept, num_fit_parameters=1, scale_noise=fold_gates_from_left, scale_factors=[3], full_output=True, **kwargs, ) for results in [results0, results1]: for i in range(len(results[1])): assert abs(results[1][i][0] - exact_solution[i]) >= abs(results[0][i] - exact_solution[i])
def test_no_num_fit_parameters_with_custom_fit_raises_error(): with pytest.raises(ValueError, match="Must provide `num_fit_parameters`"): execute_with_cdr( random_x_z_cnot_circuit(LineQubit.range(2), n_moments=2, random_state=1), execute, observables=Observable(PauliString()), simulator=simulate, fit_function=lambda _: 1, )
def test_execute_with_cdr_using_clifford_circuit(): a, b = cirq.LineQubit.range(2) clifCirc = cirq.Circuit( cirq.H.on(a), cirq.H.on(b), ) obs = Observable(PauliString("XZ"), PauliString("YY")) cdr_value = execute_with_cdr(clifCirc, observable=obs, executor=execute, simulator=simulate) assert obs.expectation(clifCirc, simulate) == cdr_value
def test_execute_with_variable_noise_cdr(circuit_type): circuit = random_x_z_cnot_circuit(LineQubit.range(2), n_moments=5, random_state=1) circuit = convert_from_mitiq(circuit, circuit_type) obs = Observable(PauliString("IZ"), PauliString("ZZ")) true_value = obs.expectation(circuit, simulate) noisy_value = obs.expectation(circuit, execute) vncdr_value = execute_with_cdr( circuit, execute, obs, simulator=simulate, num_training_circuits=10, fraction_non_clifford=0.5, scale_factors=[1, 3], random_state=1, ) assert abs(vncdr_value - true_value) <= abs(noisy_value - true_value)
def test_execute_with_cdr(circuit_type, fit_function, kwargs, random_state): circuit = random_x_z_cnot_circuit( LineQubit.range(2), n_moments=5, random_state=random_state, ) circuit = convert_from_mitiq(circuit, circuit_type) obs = Observable(PauliString("XZ"), PauliString("YY")) true_value = obs.expectation(circuit, simulate) noisy_value = obs.expectation(circuit, execute) cdr_value = execute_with_cdr( circuit, execute, obs, simulator=simulate, num_training_circuits=20, fraction_non_clifford=0.5, fit_function=fit_function, random_state=random_state, **kwargs, ) assert abs(cdr_value - true_value) <= abs(noisy_value - true_value)