def test_run_factory_with_number_of_shots(): true_zne_value = 1.0 scale_factors = [1.0, 2.0, 3.0] shot_list = [1_000, 2_000, 3_000] fac = zne.inference.ExpFactory(scale_factors=scale_factors, shot_list=shot_list) circuit = measure( random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH), 0) base = qiskit_executor(circuit) zne_value = fac.run( circuit, qiskit_executor, scale_noise=zne.scaling.fold_gates_at_random, ).reduce() assert abs(true_zne_value - zne_value) < abs(true_zne_value - base) for i in range(len(fac._instack)): assert fac._instack[i] == { "scale_factor": scale_factors[i], "shots": shot_list[i], }
def test_execute_with_zne(): """Tests a random identity circuit execution with zero-noise extrapolation. """ rand_circ = random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH) qp = measure(rand_circ, qid=0) result = execute_with_zne(qp, basic_executor, scale_noise=scale_noise) assert np.isclose(result, 1.0, atol=1.0e-1)
def test_run_factory(): """Tests qrun of a Richardson Factory.""" qp = random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH) qp = measure(qp, 0) fac = RichardsonFactory([1.0, 2.0, 3.0]) fac.run(qp, basic_executor, scale_noise) result = fac.reduce() assert np.isclose(result, 1.0, atol=1.0e-1)
def test_zne_decorator(): true_zne_value = 1.0 circuit = measure( random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH), 0) base = qiskit_executor(circuit) zne_value = decorated_executor(circuit) assert abs(true_zne_value - zne_value) < abs(true_zne_value - base)
def test_execute_with_zne(): true_zne_value = 1.0 circuit = measure( random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH), 0) base = qiskit_executor(circuit) zne_value = zne.execute_with_zne(circuit, qiskit_executor) assert abs(true_zne_value - zne_value) < abs(true_zne_value - base)
def test_zne_decorator(): """Tests a zne decorator.""" rand_circ = random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH) qp = measure(rand_circ, qid=0) # bad_result is computed with native noise (scale = 1) bad_result = basic_executor(scale_noise(qp, 1)) good_result = decorated_executor(qp) assert not np.isclose(bad_result, 1.0, atol=1.0e-1) assert np.isclose(good_result, 1.0, atol=1.0e-1)
def test_run_factory_with_number_of_shots(): """Tests "run" method of an ExpFactory with shot_list.""" qp = random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH) qp = measure(qp, 0) fac = ExpFactory([1.0, 2.0, 3.0], shot_list=[10 ** 4, 10 ** 5, 10 ** 6]) fac.run(qp, basic_executor, scale_noise=scale_noise) result = fac.reduce() assert np.isclose(result, 1.0, atol=1.0e-1) assert fac._instack[0] == {"scale_factor": 1.0, "shots": 10 ** 4} assert fac._instack[1] == {"scale_factor": 2.0, "shots": 10 ** 5} assert fac._instack[2] == {"scale_factor": 3.0, "shots": 10 ** 6}
def test_mitigate_executor(): true_zne_value = 1.0 for _ in range(10): circuit = measure( random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH), 0) base = qiskit_executor(circuit) mitigated_executor = zne.mitigate_executor(qiskit_executor) zne_value = mitigated_executor(circuit) assert abs(true_zne_value - zne_value) < abs(true_zne_value - base)
def test_mitigate_executor_with_shot_list(): """Tests the mitigation of an executor using different shots for each noise scale factor. """ rand_circ = random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH) qp = measure(rand_circ, qid=0) fac = RichardsonFactory( [1.0, 2.0, 3.0], shot_list=[10 ** 4, 10 ** 5, 10 ** 6] ) new_executor = mitigate_executor( basic_executor, scale_noise=scale_noise, factory=fac ) # bad_result is computed with native noise (scale = 1) bad_result = basic_executor(scale_noise(qp, 1)) good_result = new_executor(qp) assert not np.isclose(bad_result, 1.0, atol=1.0e-1) assert np.isclose(good_result, 1.0, atol=1.0e-1) assert fac._instack[0] == {"scale_factor": 1.0, "shots": 10 ** 4} assert fac._instack[1] == {"scale_factor": 2.0, "shots": 10 ** 5} assert fac._instack[2] == {"scale_factor": 3.0, "shots": 10 ** 6}
def test_mitigate_executor_with_shot_list(): true_zne_value = 1.0 scale_factors = [1.0, 2.0, 3.0] shot_list = [1_000, 2_000, 3_000] fac = zne.inference.ExpFactory(scale_factors=scale_factors, shot_list=shot_list) mitigated_executor = zne.mitigate_executor(qiskit_executor, fac) circuit = measure( random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH), 0) base = qiskit_executor(circuit) zne_value = mitigated_executor(circuit) assert abs(true_zne_value - zne_value) < abs(true_zne_value - base) for i in range(len(fac._instack)): assert fac._instack[i] == { "scale_factor": scale_factors[i], "shots": shot_list[i], }