def test_meas_fitter_with_noise(self):
        """
            Test the MeasurementFitter with noise
        """
        print("Testing MeasurementFitter with noise")

        # pre-generated results with noise
        # load from pickled file
        fo = open(
            os.path.join(os.path.dirname(__file__), 'test_meas_results.pkl'),
            'rb')
        tests = pickle.load(fo)
        fo.close()

        # Set the state labels
        state_labels = ['000', '001', '010', '011', '100', '101', '110', '111']
        meas_cal = CompleteMeasFitter(None, state_labels, circlabel='test')

        for tst_index, _ in enumerate(tests):
            # Set the calibration matrix
            meas_cal.cal_matrix = tests[tst_index]['cal_matrix']
            # Calculate the fidelity
            fidelity = meas_cal.readout_fidelity()

            meas_filter = MeasurementFilter(tests[tst_index]['cal_matrix'],
                                            state_labels)

            # Calculate the results after mitigation
            output_results_pseudo_inverse = meas_filter.apply(
                tests[tst_index]['results'], method='pseudo_inverse')
            output_results_least_square = meas_filter.apply(
                tests[tst_index]['results'], method='least_squares')

            # Compare with expected fidelity and expected results
            self.assertAlmostEqual(fidelity,
                                   tests[tst_index]['fidelity'],
                                   places=0)
            self.assertAlmostEqual(
                output_results_pseudo_inverse['000'],
                tests[tst_index]['results_pseudo_inverse']['000'],
                places=0)

            self.assertAlmostEqual(
                output_results_least_square['000'],
                tests[tst_index]['results_least_square']['000'],
                places=0)

            self.assertAlmostEqual(
                output_results_pseudo_inverse['111'],
                tests[tst_index]['results_pseudo_inverse']['111'],
                places=0)

            self.assertAlmostEqual(
                output_results_least_square['111'],
                tests[tst_index]['results_least_square']['111'],
                places=0)
Exemplo n.º 2
0
class MeasurementErrorMitigation:
    def __init__(self, backend, qubits):
        self.backend = backend
        self.qubits = qubits
        self.filter = None

    def run_experiment(self, circuits_per_state=1):
        exp = MeasurementErrorExperiment(self.qubits,
                                         circuits_per_state=circuits_per_state)
        exp_data = exp.run(backend=self.backend,
                           shots=self.backend.configuration().max_shots)
        print('Experiment ID:', exp_data.experiment_id)
        exp_data.block_for_results()
        exp_data.save()
        self._load_from_exp_data(exp_data)

        return exp_data.experiment_id

    def load_matrix(self, experiment_id):
        exp_data = DbExperimentData.load(
            experiment_id,
            self.backend.provider().service("experiment"))
        self._load_from_exp_data(exp_data)

    def _load_from_exp_data(self, exp_data):
        analysis_result = exp_data.analysis_results()[0]
        self.filter = MeasurementFilter(analysis_result.value,
                                        analysis_result.extra)

    def apply(self, counts_list):
        corrected_counts = []
        for counts in counts_list:
            corrected_counts.append(self.filter.apply(counts))

        return corrected_counts
Exemplo n.º 3
0
def generate_meas_calibration(results_file_path: str, runs: int):
    """
    run the measurement calibration circuits, calculates the fitter matrix in few methods
    and saves the results
    The simulation results files will contain a list of dictionaries with the keys:
        cal_matrix - the matrix used to calculate the ideal measurement
        fidelity - the calculated fidelity of using this matrix
        results - results of a bell state circuit with noise
        results_pseudo_inverse - the result of using the psedo-inverse method on the bell state
        results_least_square - the result of using the least-squares method on the bell state

    Args:
        results_file_path: path of the json file of the results file
        runs: the number of different runs to save
    """
    results = []
    for run in range(runs):
        cal_results, state_labels, circuit_results = \
            meas_calibration_circ_execution(3, 1000, SEED + run)

        meas_cal = CompleteMeasFitter(cal_results,
                                      state_labels,
                                      circlabel='test')
        meas_filter = MeasurementFilter(meas_cal.cal_matrix, state_labels)

        # Calculate the results after mitigation
        results_pseudo_inverse = meas_filter.apply(circuit_results,
                                                   method='pseudo_inverse')
        results_least_square = meas_filter.apply(circuit_results,
                                                 method='least_squares')
        results.append({
            "cal_matrix":
            convert_ndarray_to_list_in_data(meas_cal.cal_matrix),
            "fidelity":
            meas_cal.readout_fidelity(),
            "results":
            circuit_results,
            "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)