def create_qv_data_high_confidence(dir_path: str): """ create quantum volume experiment_data using seed, and save it as a json the circuit is generated with moderate noise, so the mean hop is above 2/3, and also with enough trials, so the confidence is above threshold Args: dir_path(str): The directory which the data will be saved to. """ num_of_qubits = 4 backend = AerSimulator(seed_simulator=SEED) basis_gates = ["id", "rz", "sx", "x", "cx", "reset"] noise = create_noise_model() qv_exp = QuantumVolume(range(num_of_qubits), seed=SEED) qv_exp.set_experiment_options(trials=300) qv_exp.set_transpile_options(basis_gates=basis_gates) qv_data = qv_exp.run(backend, noise_model=noise, basis_gates=basis_gates) qv_data.block_for_results() result_file_path = os.path.join(dir_path, "qv_data_moderate_noise_300_trials.json") with open(result_file_path, "w") as json_file: json.dump(qv_data.data(), json_file, cls=ExperimentEncoder) result_file_path = os.path.join(dir_path, "qv_result_moderate_noise_300_trials.json") with open(result_file_path, "w") as json_file: result_dicts = [] for result in qv_data.analysis_results(): result_dicts.append( { "name": result.name, "value": result.value, "extra": result.extra, } ) json.dump(result_dicts, json_file, cls=ExperimentEncoder)
def test_qv_sigma_decreasing(self): """ Test that the sigma is decreasing after adding more trials """ num_of_qubits = 3 backend = Aer.get_backend("aer_simulator") qv_exp = QuantumVolume(num_of_qubits, seed=SEED) # set number of trials to a low number to make the test faster qv_exp.set_experiment_options(trials=2) expdata1 = qv_exp.run(backend) expdata1.block_for_results() result_data1 = expdata1.analysis_results(0) expdata2 = qv_exp.run(backend, experiment_data=expdata1) expdata2.block_for_results() result_data2 = expdata2.analysis_results(0) self.assertTrue(result_data1.extra["trials"] == 2, "number of trials is incorrect") self.assertTrue( result_data2.extra["trials"] == 4, "number of trials is incorrect" " after adding more trials", ) self.assertTrue( result_data2.value.stderr <= result_data1.value.stderr, "sigma did not decreased after adding more trials", )
def test_qv_circuits_length(self): """ Test circuit generation - check the number of circuits generated and the amount of qubits in each circuit """ qubits_lists = [3, [0, 1, 2], [0, 1, 2, 4]] ntrials = [2, 3, 5] for qubits in qubits_lists: for trials in ntrials: qv_exp = QuantumVolume(qubits) qv_exp.set_experiment_options(trials=trials) qv_circs = qv_exp.circuits() self.assertEqual( len(qv_circs), trials, "Number of circuits generated do not match the number of trials", ) self.assertEqual( len(qv_circs[0].qubits), qv_exp.num_qubits, "Number of qubits in the Quantum Volume circuit do not match the" " number of qubits in the experiment", )
def create_qv_data_70_trials(dir_path: str): """ create quantum volume experiment_data using seed, and save it as a json Args: dir_path(str): The directory which the data will be saved to. """ num_of_qubits = 3 backend = AerSimulator(seed_simulator=SEED) qv_exp = QuantumVolume(range(num_of_qubits), seed=SEED) qv_exp.set_experiment_options(trials=70) qv_data = qv_exp.run(backend) qv_data.block_for_results() result_file_path = os.path.join(dir_path, "qv_data_70_trials.json") with open(result_file_path, "w") as json_file: json.dump(qv_data.data(), json_file, cls=ExperimentEncoder)
def create_qv_ideal_probabilities(dir_path: str): """ create quantum volume circuits using seed, and save their ideal probabilities vector in a json Args: dir_path(str): The directory which the data will be saved to. """ num_of_qubits = 3 qv_exp = QuantumVolume(range(num_of_qubits), seed=SEED) qv_exp.set_experiment_options(trials=20) qv_circs = qv_exp.circuits() simulation_probabilities = [ list(qv_circ.metadata["ideal_probabilities"]) for qv_circ in qv_circs ] result_file_path = os.path.join(dir_path, "qv_ideal_probabilities.json") with open(result_file_path, "w") as json_file: json.dump(simulation_probabilities, json_file, cls=ExperimentEncoder)
def test_qv_ideal_probabilities(self): """ Test the probabilities of ideal circuit Compare between simulation and statevector calculation and compare to pre-calculated probabilities with the same seed """ num_of_qubits = 3 qv_exp = QuantumVolume(num_of_qubits, seed=SEED) # set number of trials to a low number to make the test faster qv_exp.set_experiment_options(trials=20) qv_circs = qv_exp.circuits() simulation_probabilities = [ qv_circ.metadata["ideal_probabilities"] for qv_circ in qv_circs ] # create the circuits again, but this time disable simulation so the # ideal probabilities will be calculated using statevector qv_exp = QuantumVolume(num_of_qubits, seed=SEED) qv_exp.set_experiment_options(trials=20) qv_exp._simulation_backend = None qv_circs = qv_exp.circuits() statevector_probabilities = [ qv_circ.metadata["ideal_probabilities"] for qv_circ in qv_circs ] self.assertTrue( matrix_equal(simulation_probabilities, statevector_probabilities), "probabilities calculated using simulation and " "statevector are not the same", ) # compare to pre-calculated probabilities dir_name = os.path.dirname(os.path.abspath(__file__)) probabilities_json_file = "qv_ideal_probabilities.json" with open(os.path.join(dir_name, probabilities_json_file), "r") as json_file: probabilities = json.load(json_file, cls=ExperimentDecoder) self.assertTrue( matrix_equal(simulation_probabilities, probabilities), "probabilities calculated using simulation and " "pre-calculated probabilities are not the same", )