def test_tensored_meas_cal_on_circuit(self): """Test an execution on a circuit.""" # Generate the calibration circuits meas_calibs, mit_pattern, ghz, meas_layout = tensored_calib_circ_creation( ) # Run the calibration circuits backend = Aer.get_backend('qasm_simulator') cal_results = qiskit.execute(meas_calibs, backend=backend, shots=self.shots, seed_simulator=SEED, seed_transpiler=SEED).result() # Make a calibration matrix meas_cal = TensoredMeasFitter(cal_results, mit_pattern=mit_pattern) # Calculate the fidelity fidelity = meas_cal.readout_fidelity(0) * meas_cal.readout_fidelity(1) results = qiskit.execute([ghz], backend=backend, shots=self.shots, seed_simulator=SEED, seed_transpiler=SEED).result() # Predicted equally distributed results predicted_results = {'000': 0.5, '111': 0.5} meas_filter = meas_cal.filter # Calculate the results after mitigation output_results_pseudo_inverse = meas_filter.apply( results, method='pseudo_inverse', meas_layout=meas_layout).get_counts(0) output_results_least_square = meas_filter.apply( results, method='least_squares', meas_layout=meas_layout).get_counts(0) # Compare with expected fidelity and expected results self.assertAlmostEqual(fidelity, 1.0) self.assertAlmostEqual(output_results_pseudo_inverse['000'] / self.shots, predicted_results['000'], places=1) self.assertAlmostEqual(output_results_least_square['000'] / self.shots, predicted_results['000'], places=1) self.assertAlmostEqual(output_results_pseudo_inverse['111'] / self.shots, predicted_results['111'], places=1) self.assertAlmostEqual(output_results_least_square['111'] / self.shots, predicted_results['111'], places=1)
def test_ideal_tensored_meas_cal(self): """Test ideal execution, without noise.""" mit_pattern = [[1, 2], [3, 4, 5], [6]] meas_layout = [1, 2, 3, 4, 5, 6] # Generate the calibration circuits meas_calibs, _ = tensored_meas_cal(mit_pattern=mit_pattern) # Perform an ideal execution on the generated circuits backend = Aer.get_backend('qasm_simulator') cal_results = qiskit.execute(meas_calibs, backend=backend, shots=self.shots).result() # Make calibration matrices meas_cal = TensoredMeasFitter(cal_results, mit_pattern=mit_pattern) # Assert that the calibration matrices are equal to identity cal_matrices = meas_cal.cal_matrices self.assertEqual(len(mit_pattern), len(cal_matrices), 'Wrong number of calibration matrices') for qubit_list, cal_mat in zip(mit_pattern, cal_matrices): IdentityMatrix = np.identity(2**len(qubit_list)) self.assertListEqual( cal_mat.tolist(), IdentityMatrix.tolist(), 'Error: the calibration matrix is \ not equal to identity') # Assert that the readout fidelity is equal to 1 self.assertEqual( meas_cal.readout_fidelity(), 1.0, 'Error: the average fidelity \ is not equal to 1') # Generate ideal (equally distributed) results results_dict, _ = \ self.generate_ideal_results(count_keys(6), 6) # Output the filter meas_filter = meas_cal.filter # Apply the calibration matrix to results # in list and dict forms using different methods results_dict_1 = meas_filter.apply(results_dict, method='least_squares', meas_layout=meas_layout) results_dict_0 = meas_filter.apply(results_dict, method='pseudo_inverse', meas_layout=meas_layout) # Assert that the results are equally distributed self.assertDictEqual(results_dict, results_dict_0) round_results = {} for key, val in results_dict_1.items(): round_results[key] = np.round(val) self.assertDictEqual(results_dict, round_results)
def generate_tensormeas_calibration(results_file_path: str): """ run the tensored measurement calibration circuits, calculate the fitter in a few methods and save the results The simulation results files will contain a list of dictionaries with the keys: cal_results - the results of the measurement calibration circuit results - results of a GHZ state circuit with noise mit_pattern - the mitigation pattern fidelity - the calculated fidelity of using this matrix results_pseudo_inverse - the result of using the psedo-inverse method on the GHZ state results_least_square - the result of using the least-squares method on the GHZ state Args: results_file_path: path of the json file of the results file """ cal_results, mit_pattern, circuit_results = \ tensored_calib_circ_execution(1000, SEED) meas_cal = TensoredMeasFitter(cal_results, mit_pattern=mit_pattern) meas_filter = meas_cal.filter # Calculate the results after mitigation results_pseudo_inverse = meas_filter.apply(circuit_results.get_counts(), method='pseudo_inverse') results_least_square = meas_filter.apply(circuit_results.get_counts(), method='least_squares') results = { "cal_results": cal_results.to_dict(), "results": circuit_results.to_dict(), "mit_pattern": mit_pattern, "fidelity": meas_cal.readout_fidelity(), "results_pseudo_inverse": results_pseudo_inverse, "results_least_square": results_least_square } with open(results_file_path, "w") as results_file: json.dump(results, results_file)
def test_tensored_meas_fitter_with_noise(self): """Test the TensoredFitter with noise.""" # pre-generated results with noise # load from json file with open( os.path.join(os.path.dirname(__file__), 'test_tensored_meas_results.json'), "r") as saved_file: saved_info = json.load(saved_file) saved_info['cal_results'] = Result.from_dict(saved_info['cal_results']) saved_info['results'] = Result.from_dict(saved_info['results']) meas_cal = TensoredMeasFitter(saved_info['cal_results'], mit_pattern=saved_info['mit_pattern']) # Calculate the fidelity fidelity = meas_cal.readout_fidelity(0) * meas_cal.readout_fidelity(1) # Compare with expected fidelity and expected results self.assertAlmostEqual(fidelity, saved_info['fidelity'], places=0) meas_filter = meas_cal.filter # Calculate the results after mitigation output_results_pseudo_inverse = meas_filter.apply( saved_info['results'].get_counts(0), method='pseudo_inverse') output_results_least_square = meas_filter.apply(saved_info['results'], method='least_squares') self.assertAlmostEqual(output_results_pseudo_inverse['000'], saved_info['results_pseudo_inverse']['000'], places=0) self.assertAlmostEqual( output_results_least_square.get_counts(0)['000'], saved_info['results_least_square']['000'], places=0) self.assertAlmostEqual(output_results_pseudo_inverse['111'], saved_info['results_pseudo_inverse']['111'], places=0) self.assertAlmostEqual( output_results_least_square.get_counts(0)['111'], saved_info['results_least_square']['111'], places=0) substates_list = [] for qubit_list in saved_info['mit_pattern']: substates_list.append(count_keys(len(qubit_list))[::-1]) fitter_other_order = TensoredMeasFitter( saved_info['cal_results'], substate_labels_list=substates_list, mit_pattern=saved_info['mit_pattern']) fidelity = fitter_other_order.readout_fidelity(0) * \ meas_cal.readout_fidelity(1) self.assertAlmostEqual(fidelity, saved_info['fidelity'], places=0) meas_filter = fitter_other_order.filter # Calculate the results after mitigation output_results_pseudo_inverse = meas_filter.apply( saved_info['results'].get_counts(0), method='pseudo_inverse') output_results_least_square = meas_filter.apply(saved_info['results'], method='least_squares') self.assertAlmostEqual(output_results_pseudo_inverse['000'], saved_info['results_pseudo_inverse']['000'], places=0) self.assertAlmostEqual( output_results_least_square.get_counts(0)['000'], saved_info['results_least_square']['000'], places=0) self.assertAlmostEqual(output_results_pseudo_inverse['111'], saved_info['results_pseudo_inverse']['111'], places=0) self.assertAlmostEqual( output_results_least_square.get_counts(0)['111'], saved_info['results_least_square']['111'], places=0)
def test_tensored_meas_cal_on_circuit(self): """Test an execution on a circuit.""" mit_pattern = [[2], [4, 1]] qr = qiskit.QuantumRegister(5) # Generate the calibration circuits meas_calibs, _ = tensored_meas_cal(mit_pattern, qr=qr) # Run the calibration circuits backend = Aer.get_backend('qasm_simulator') cal_results = qiskit.execute(meas_calibs, backend=backend, shots=self.shots).result() # Make a calibration matrix meas_cal = TensoredMeasFitter(cal_results, mit_pattern=mit_pattern) # Calculate the fidelity fidelity = meas_cal.readout_fidelity(0) * meas_cal.readout_fidelity(1) # Make a 3Q GHZ state cr = ClassicalRegister(3) ghz = QuantumCircuit(qr, cr) ghz.h(qr[2]) ghz.cx(qr[2], qr[4]) ghz.cx(qr[2], qr[1]) ghz.measure(qr[2], cr[0]) ghz.measure(qr[4], cr[1]) ghz.measure(qr[1], cr[2]) results = qiskit.execute([ghz], backend=backend, shots=self.shots).result() # Predicted equally distributed results predicted_results = {'000': 0.5, '111': 0.5} meas_filter = meas_cal.filter # Calculate the results after mitigation output_results_pseudo_inverse = meas_filter.apply( results, method='pseudo_inverse').get_counts(0) output_results_least_square = meas_filter.apply( results, method='least_squares').get_counts(0) # Compare with expected fidelity and expected results self.assertAlmostEqual(fidelity, 1.0) self.assertAlmostEqual(output_results_pseudo_inverse['000'] / self.shots, predicted_results['000'], places=1) self.assertAlmostEqual(output_results_least_square['000'] / self.shots, predicted_results['000'], places=1) self.assertAlmostEqual(output_results_pseudo_inverse['111'] / self.shots, predicted_results['111'], places=1) self.assertAlmostEqual(output_results_least_square['111'] / self.shots, predicted_results['111'], places=1)