Beispiel #1
0
    def test_fitters(self):
        """ Test the fitters """

        # Use json results files
        tests_settings = [{
            'rb_opts': {
                'xdata':
                np.array([[1, 21, 41, 61, 81, 101, 121, 141, 161, 181],
                          [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]]),
                'rb_pattern': [[0, 1], [2]],
                'shots':
                1024
            },
            'results_file':
            os.path.join(os.path.dirname(__file__),
                         'test_fitter_results_1.json'),
            'expected_results_file':
            os.path.join(os.path.dirname(__file__),
                         'test_fitter_expected_results_1.json')
        }, {
            'rb_opts': {
                'xdata':
                np.array([[1, 21, 41, 61, 81, 101, 121, 141, 161, 181]]),
                'rb_pattern': [[0]],
                'shots': 1024
            },
            'results_file':
            os.path.join(os.path.dirname(__file__),
                         'test_fitter_results_2.json'),
            'expected_results_file':
            os.path.join(os.path.dirname(__file__),
                         'test_fitter_expected_results_2.json')
        }]

        for tst_index, tst_settings in enumerate(tests_settings):
            results_list = load_results_from_json(tst_settings['results_file'])
            with open(tst_settings['expected_results_file'],
                      "r") as expected_results_file:
                tst_expected_results = json.load(expected_results_file)

            # RBFitter class
            rb_fit = RBFitter(results_list[0],
                              tst_settings['rb_opts']['xdata'],
                              tst_settings['rb_opts']['rb_pattern'])

            # add the seeds in reverse order
            for seedind in range(len(results_list) - 1, 0, -1):
                rb_fit.add_data([results_list[seedind]])

            ydata = rb_fit.ydata
            fit = rb_fit.fit

            self.compare_results_and_excpected(ydata,
                                               tst_expected_results['ydata'],
                                               tst_index)
            self.compare_results_and_excpected(fit,
                                               tst_expected_results['fit'],
                                               tst_index)
Beispiel #2
0
def generate_fitter_data_1(results_file_path: str,
                           expected_results_file_path: str):
    """
    Create rb circuits with depolarizing error and simulate them,
    then write the results in a json file.
    also creates fitter for the data results,
    and also write the fitter results in another json file.

    The simulation results file will contain a list of Result objects in a dictionary format.
    The fitter data file will contain dictionary with the following keys:
    - 'ydata', value is stored in the form of list of dictionaries with keys of:
        - mean, list of integers
        - std, list of integers
    - fit, value is stored in the form of list of dictionaries with keys of:
        - params, list of integers
        - params_err, list of integers
        - epc, list of integers
        - epc_err, list of integers

    Args:
        results_file_path: path of the json file of the simulation results file
        expected_results_file_path: path of the json file of the fitter results file
    """

    rb_opts = {}
    shots = 1024
    rb_opts['nseeds'] = 5
    rb_opts['rb_pattern'] = [[0, 1], [2]]
    rb_opts['length_multiplier'] = [1, 2]
    rb_opts['length_vector'] = np.arange(1, 200, 20)
    rb_opts['rand_seed'] = SEED

    rb_results, xdata = rb_circuit_execution(rb_opts, shots)
    save_results_as_json(rb_results, results_file_path)

    # generate also the expected results of the fitter
    rb_fit = RBFitter(rb_results, xdata, rb_opts['rb_pattern'])
    ydata = rb_fit.ydata
    fit = rb_fit.fit
    # convert ndarray to list
    ydata = convert_ndarray_to_list_in_data(ydata)
    fit = convert_ndarray_to_list_in_data(fit)

    expected_result = {"ydata": ydata, "fit": fit}
    with open(expected_results_file_path, "w") as expected_results_file:
        json.dump(expected_result, expected_results_file)
    def test_fitters(self):
        """ Test the fitters """

        # Use pickled results files

        tests = [{
            'rb_opts': {
                'xdata': np.array([[1, 21, 41, 61, 81, 101, 121, 141,
                                    161, 181],
                                   [2, 42, 82, 122, 162, 202, 242, 282,
                                    322, 362]]),
                'rb_pattern': [[0, 1], [2]],
                'shots': 1024},
            'results_file': os.path.join(os.path.dirname(__file__),
                                         'test_fitter_results_1.pkl'),
            'expected': {
                'ydata': [{
                    'mean': np.array([0.96367187, 0.73457031,
                                      0.58066406, 0.4828125,
                                      0.41035156, 0.34902344,
                                      0.31210938, 0.2765625,
                                      0.29453125, 0.27695313]),
                    'std': np.array([0.01013745, 0.0060955, 0.00678272,
                                     0.01746491, 0.02015981, 0.02184184,
                                     0.02340167, 0.02360293, 0.00874773,
                                     0.01308156])}, {
                                         'mean': np.array([0.98925781,
                                                           0.87734375, 0.78125,
                                                           0.73066406,
                                                           0.68496094,
                                                           0.64296875,
                                                           0.59238281,
                                                           0.57421875,
                                                           0.56074219,
                                                           0.54980469]),
                                         'std': np.array(
                                             [0.00276214, 0.01602991,
                                              0.00768946, 0.01413015,
                                              0.00820777, 0.01441348,
                                              0.01272682, 0.01031649,
                                              0.02103036, 0.01224408])}],
                'fit': [{
                    'params': np.array([0.71936804, 0.98062119,
                                        0.25803749]),
                    'params_err': np.array([0.0065886, 0.00046714,
                                            0.00556488]),
                    'epc': 0.014534104912075935,
                    'epc_err': 6.923601336318206e-06},
                        {'params': np.array([0.49507094, 0.99354093,
                                             0.50027262]),
                         'params_err': np.array([0.0146191, 0.0004157,
                                                 0.01487439]),
                         'epc': 0.0032295343343508587,
                         'epc_err': 1.3512528169626024e-06}]
            }}, {
                'rb_opts': {
                    'xdata': np.array([[1, 21, 41, 61, 81, 101, 121, 141, 161,
                                        181]]),
                    'rb_pattern': [[0]],
                    'shots': 1024},
                'results_file': os.path.join(os.path.dirname(__file__),
                                             'test_fitter_results_2.pkl'),
                'expected': {
                    'ydata': [{'mean': np.array([0.99199219, 0.93867188,
                                                 0.87871094, 0.83945313,
                                                 0.79335937, 0.74785156,
                                                 0.73613281, 0.69414062,
                                                 0.67460937, 0.65664062]),
                               'std': np.array([0.00567416, 0.00791919,
                                                0.01523437, 0.01462368,
                                                0.01189002, 0.01445049,
                                                0.00292317, 0.00317345,
                                                0.00406888,
                                                0.01504794])}],
                    'fit': [{'params': np.array([0.59599995, 0.99518211,
                                                 0.39866989]),
                             'params_err': np.array([0.08843152, 0.00107311,
                                                     0.09074325]),
                             'epc': 0.0024089464034862673,
                             'epc_err': 2.5975709525210163e-06}]}}]

        for tst_index, tst in enumerate(tests):
            fo = open(tst['results_file'], 'rb')
            results_list = pickle.load(fo)
            fo.close()

            # RBFitter class
            rb_fit = RBFitter(results_list, tst['rb_opts']['xdata'],
                              tst['rb_opts']['rb_pattern'])
            ydata = rb_fit.ydata
            fit = rb_fit.fit

            for i, _ in enumerate(ydata):
                self.assertTrue(all(np.isclose(a, b) for a, b in
                                    zip(ydata[i]['mean'],
                                        tst['expected']['ydata'][i]['mean'])),
                                'Incorrect mean in test no. ' + str(tst_index))
                if tst['expected']['ydata'][i]['std'] is None:
                    self.assertIsNone(
                        ydata[i]['std'],
                        'Incorrect std in test no. ' + str(tst_index))
                else:
                    self.assertTrue(
                        all(np.isclose(a, b) for a, b in zip(
                            ydata[i]['std'],
                            tst['expected']['ydata'][i]['std'])),
                        'Incorrect std in test no. ' + str(tst_index))
                self.assertTrue(
                    all(np.isclose(a, b) for a, b in zip(
                        fit[i]['params'],
                        tst['expected']['fit'][i]['params'])),
                    'Incorrect fit parameters in test no. ' + str(tst_index))
                self.assertTrue(
                    all(np.isclose(a, b) for a, b in zip(
                        fit[i]['params_err'],
                        tst['expected']['fit'][i]['params_err'])),
                    'Incorrect fit error in test no. ' + str(tst_index))
                self.assertTrue(np.isclose(fit[i]['epc'],
                                           tst['expected']['fit'][i]['epc']),
                                'Incorrect EPC in test no. ' + str(tst_index))
                self.assertTrue(
                    np.isclose(fit[i]['epc_err'],
                               tst['expected']['fit'][i]['epc_err']),
                    'Incorrect EPC error in test no. ' + str(tst_index))
# Import the RB Functions
from qiskit.ignis.verification.randomized_benchmarking import randomized_benchmarking_seq, RBFitter

# Generate RB circuits (2Q RB)
rb_opts = {}
rb_opts['length_vector'] = [1, 10, 20, 50, 75, 100, 125]
rb_opts['nseeds'] = 5
rb_opts['rb_pattern'] = [[0, 1]]
rb_circs, xdata = randomized_benchmarking_seq(**rb_opts)

# Run on a noisy simulator
noise_model = NoiseModel()
noise_model.add_all_qubit_quantum_error(depolarizing_error(0.002, 1),
                                        ['u1', 'u2', 'u3'])
noise_model.add_all_qubit_quantum_error(depolarizing_error(0.002, 2), 'cx')

backend = qiskit.Aer.get_backend('qasm_simulator')

# Create the RB fitter
rb_fit = RBFitter(None, xdata, rb_opts['rb_pattern'])
for rb_seed, rb_circ_seed in enumerate(rb_circs):

    job = qiskit.execute(rb_circ_seed,
                         backend=backend,
                         basis_gates=['u1', 'u2', 'u3', 'cx'],
                         noise_model=noise_model)

    # Add data to the fitter
    rb_fit.add_data(job.result())
    print('After seed %d, EPC %f' % (rb_seed, rb_fit.fit[0]['epc']))