def test_qc_expectation(client_configuration: QCSClientConfiguration, dummy_compiler: DummyCompiler): qc = QuantumComputer(name="testy!", qam=QVM(client_configuration=client_configuration), compiler=dummy_compiler) # bell state program p = Program() p += RESET() p += H(0) p += CNOT(0, 1) p.wrap_in_numshots_loop(10) # XX, YY, ZZ experiment sx = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sZ(1)), out_operator=sX(0) * sX(1)) sy = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sZ(1)), out_operator=sY(0) * sY(1)) sz = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sZ(1)), out_operator=sZ(0) * sZ(1)) e = Experiment(settings=[sx, sy, sz], program=p) results = qc.experiment(e) # XX expectation value for bell state |00> + |11> is 1 assert np.isclose(results[0].expectation, 1) assert np.isclose(results[0].std_err, 0) assert results[0].total_counts == 40 # YY expectation value for bell state |00> + |11> is -1 assert np.isclose(results[1].expectation, -1) assert np.isclose(results[1].std_err, 0) assert results[1].total_counts == 40 # ZZ expectation value for bell state |00> + |11> is 1 assert np.isclose(results[2].expectation, 1) assert np.isclose(results[2].std_err, 0) assert results[2].total_counts == 40
def test_qc_joint_calibration(client_configuration: QCSClientConfiguration): # noise model with 95% symmetrized readout fidelity per qubit noise_model = asymmetric_ro_model([0, 1], 0.945, 0.955) qc = get_qc("2q-qvm", client_configuration=client_configuration) qc.qam.noise_model = noise_model # |01> state program p = Program() p += RESET() p += X(0) p.wrap_in_numshots_loop(10000) # ZZ experiment sz = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sZ(1)), out_operator=sZ(0) * sZ(1), additional_expectations=[[0], [1]]) e = Experiment(settings=[sz], program=p) results = qc.run_experiment(e) # ZZ expectation value for state |01> with 95% RO fid on both qubits is about -0.81 assert np.isclose(results[0].expectation, -0.81, atol=0.01) assert results[0].total_counts == 40000 # Z0 expectation value for state |01> with 95% RO fid on both qubits is about -0.9 assert np.isclose(results[0].additional_results[0].expectation, -0.9, atol=0.01) assert results[0].additional_results[1].total_counts == 40000 # Z1 expectation value for state |01> with 95% RO fid on both qubits is about 0.9 assert np.isclose(results[0].additional_results[1].expectation, 0.9, atol=0.01) assert results[0].additional_results[1].total_counts == 40000
def test_qc_joint_expectation(client_configuration: QCSClientConfiguration, dummy_compiler: DummyCompiler): qc = QuantumComputer(name="testy!", qam=QVM(client_configuration=client_configuration), compiler=dummy_compiler) # |01> state program p = Program() p += RESET() p += X(0) p.wrap_in_numshots_loop(10) # ZZ experiment sz = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sZ(1)), out_operator=sZ(0) * sZ(1), additional_expectations=[[0], [1]]) e = Experiment(settings=[sz], program=p) results = qc.run_experiment(e) # ZZ expectation value for state |01> is -1 assert np.isclose(results[0].expectation, -1) assert np.isclose(results[0].std_err, 0) assert results[0].total_counts == 40 # Z0 expectation value for state |01> is -1 assert np.isclose(results[0].additional_results[0].expectation, -1) assert results[0].additional_results[1].total_counts == 40 # Z1 expectation value for state |01> is 1 assert np.isclose(results[0].additional_results[1].expectation, 1) assert results[0].additional_results[1].total_counts == 40
def test_group_experiments_greedy(): ungrouped_tomo_expt = Experiment( [ [ ExperimentSetting( _pauli_to_product_state( PauliTerm.from_compact_str("(1+0j)*Z7Y8Z1Y4Z2Y5Y0X6")), PauliTerm.from_compact_str("(1+0j)*Z4X8Y5X3Y7Y1"), ) ], [ExperimentSetting(plusZ(7), sY(1))], ], program=Program(H(0), H(1), H(2)), ) grouped_tomo_expt = group_settings(ungrouped_tomo_expt, method="greedy") expected_grouped_tomo_expt = Experiment( [[ ExperimentSetting( TensorProductState.from_str( "Z0_7 * Y0_8 * Z0_1 * Y0_4 * Z0_2 * Y0_5 * Y0_0 * X0_6"), PauliTerm.from_compact_str("(1+0j)*Z4X8Y5X3Y7Y1"), ), ExperimentSetting(plusZ(7), sY(1)), ]], program=Program(H(0), H(1), H(2)), ) assert grouped_tomo_expt == expected_grouped_tomo_expt
def test_qc_expectation_on_qvm(client_configuration: QCSClientConfiguration, dummy_compiler: DummyCompiler): # regression test for https://github.com/rigetti/forest-tutorials/issues/2 qc = QuantumComputer(name="testy!", qam=QVM(client_configuration=client_configuration), compiler=dummy_compiler) p = Program() theta = p.declare("theta", "REAL") p += RESET() p += RY(theta, 0) p.wrap_in_numshots_loop(10000) sx = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0)), out_operator=sX(0)) e = Experiment(settings=[sx], program=p) thetas = [-np.pi / 2, 0.0, np.pi / 2] results = [] # Verify that multiple calls to qc.experiment with the same experiment backed by a QVM that # requires_exectutable does not raise an exception. for theta in thetas: results.append(qc.experiment(e, memory_map={"theta": [theta]})) assert np.isclose(results[0][0].expectation, -1.0, atol=0.01) assert np.isclose(results[0][0].std_err, 0) assert results[0][0].total_counts == 20000 # bounds on atol and std_err here are a little loose to try and avoid test flakiness. assert np.isclose(results[1][0].expectation, 0.0, atol=0.1) assert results[1][0].std_err < 0.01 assert results[1][0].total_counts == 20000 assert np.isclose(results[2][0].expectation, 1.0, atol=0.01) assert np.isclose(results[2][0].std_err, 0) assert results[2][0].total_counts == 20000
def test_setting_no_in(): out_ops = _generate_random_paulis(n_qubits=4, n_terms=7) for oop in out_ops: expt = ExperimentSetting(zeros_state(oop.get_qubits()), oop) expt2 = ExperimentSetting.from_str(str(expt)) assert expt == expt2 assert expt2.in_state == _pauli_to_product_state(functools.reduce(mul, [sZ(q) for q in oop.get_qubits()], sI())) assert expt2.out_operator == oop
def test_setting_no_in_back_compat(): out_ops = _generate_random_paulis(n_qubits=4, n_terms=7) for oop in out_ops: expt = ExperimentSetting(TensorProductState(), oop) expt2 = ExperimentSetting.from_str(str(expt)) assert expt == expt2 assert expt2.in_state == _pauli_to_product_state(sI()) assert expt2.out_operator == oop
def test_max_tpb_overlap_2(): expt_setting = ExperimentSetting( _pauli_to_product_state( PauliTerm.from_compact_str("(1+0j)*Z7Y8Z1Y4Z2Y5Y0X6")), PauliTerm.from_compact_str("(1+0j)*Z4X8Y5X3Y7Y1"), ) p = Program(H(0), H(1), H(2)) tomo_expt = Experiment([expt_setting], p) expected_dict = {expt_setting: [expt_setting]} assert expected_dict == _max_tpb_overlap(tomo_expt)
def test_max_tpb_overlap_3(): # add another ExperimentSetting to the above expt_setting = ExperimentSetting( _pauli_to_product_state( PauliTerm.from_compact_str("(1+0j)*Z7Y8Z1Y4Z2Y5Y0X6")), PauliTerm.from_compact_str("(1+0j)*Z4X8Y5X3Y7Y1"), ) expt_setting2 = ExperimentSetting(plusZ(7), sY(1)) p = Program(H(0), H(1), H(2)) tomo_expt2 = Experiment([expt_setting, expt_setting2], p) expected_dict2 = {expt_setting: [expt_setting, expt_setting2]} assert expected_dict2 == _max_tpb_overlap(tomo_expt2)
def test_build_experiment_setting_memory_map(): p = Program() s = ExperimentSetting(in_state=_pauli_to_product_state(sX(0)), out_operator=sZ(0) * sY(1)) e = Experiment(settings=[s], program=p) memory_map = e.build_setting_memory_map(s) assert memory_map == { "preparation_alpha": [0.0], "preparation_beta": [np.pi / 2], "preparation_gamma": [0.0], "measurement_alpha": [0.0, np.pi / 2], "measurement_beta": [0.0, np.pi / 2], "measurement_gamma": [0.0, -np.pi / 2], }
def test_build_symmetrization_memory_maps(): p = Program() s = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sZ(1)), out_operator=sZ(0) * sZ(1)) e = Experiment(settings=[s], program=p) memory_maps = [ { "symmetrization": [0.0, 0.0] }, { "symmetrization": [0.0, np.pi] }, { "symmetrization": [np.pi, 0.0] }, { "symmetrization": [np.pi, np.pi] }, ] assert e.build_symmetrization_memory_maps([0, 1]) == memory_maps
def test_qc_calibration_2q(client_configuration: QCSClientConfiguration): # noise model with 95% symmetrized readout fidelity per qubit noise_model = asymmetric_ro_model([0, 1], 0.945, 0.955) qc = get_qc("2q-qvm", client_configuration=client_configuration) qc.qam.noise_model = noise_model # bell state program (doesn't matter) p = Program() p += RESET() p += H(0) p += CNOT(0, 1) p.wrap_in_numshots_loop(10000) # ZZ experiment sz = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sZ(1)), out_operator=sZ(0) * sZ(1)) e = Experiment(settings=[sz], program=p) results = qc.calibrate(e) # ZZ expectation should just be (1 - 2 * readout_error_q0) * (1 - 2 * readout_error_q1) np.isclose(results[0].expectation, 0.81, atol=0.01) assert results[0].total_counts == 40000
def test_generate_experiment_program(): # simplest example p = Program() s = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0)), out_operator=sZ(0)) e = Experiment(settings=[s], program=p, symmetrization=0) exp = e.generate_experiment_program() test_exp = Program() ro = test_exp.declare("ro", "BIT") test_exp += MEASURE(0, ro[0]) assert exp.out() == test_exp.out() assert exp.num_shots == 1 # 2Q exhaustive symmetrization p = Program() s = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sZ(1)), out_operator=sZ(0) * sZ(1)) e = Experiment(settings=[s], program=p) exp = e.generate_experiment_program() test_exp = Program() test_exp += parameterized_readout_symmetrization([0, 1]) ro = test_exp.declare("ro", "BIT", 2) test_exp += MEASURE(0, ro[0]) test_exp += MEASURE(1, ro[1]) assert exp.out() == test_exp.out() assert exp.num_shots == 1 # add shots p = Program() p.wrap_in_numshots_loop(1000) s = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0)), out_operator=sZ(0)) e = Experiment(settings=[s], program=p, symmetrization=0) exp = e.generate_experiment_program() test_exp = Program() ro = test_exp.declare("ro", "BIT") test_exp += MEASURE(0, ro[0]) assert exp.out() == test_exp.out() assert exp.num_shots == 1000 # active reset p = Program() p += RESET() s = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0)), out_operator=sZ(0)) e = Experiment(settings=[s], program=p, symmetrization=0) exp = e.generate_experiment_program() test_exp = Program() test_exp += RESET() ro = test_exp.declare("ro", "BIT") test_exp += MEASURE(0, ro[0]) assert exp.out() == test_exp.out() assert exp.num_shots == 1 # state preparation and measurement p = Program() s = ExperimentSetting(in_state=_pauli_to_product_state(sY(0)), out_operator=sX(0)) e = Experiment(settings=[s], program=p, symmetrization=0) exp = e.generate_experiment_program() test_exp = Program() test_exp += parameterized_single_qubit_state_preparation([0]) test_exp += parameterized_single_qubit_measurement_basis([0]) ro = test_exp.declare("ro", "BIT") test_exp += MEASURE(0, ro[0]) assert exp.out() == test_exp.out() assert exp.num_shots == 1 # multi-qubit state preparation and measurement p = Program() s = ExperimentSetting(in_state=_pauli_to_product_state(sZ(0) * sY(1)), out_operator=sZ(0) * sX(1)) e = Experiment(settings=[s], program=p, symmetrization=0) exp = e.generate_experiment_program() test_exp = Program() test_exp += parameterized_single_qubit_state_preparation([0, 1]) test_exp += parameterized_single_qubit_measurement_basis([0, 1]) ro = test_exp.declare("ro", "BIT", 2) test_exp += MEASURE(0, ro[0]) test_exp += MEASURE(1, ro[1]) assert exp.out() == test_exp.out() assert exp.num_shots == 1