Пример #1
0
def run_SD(parameters, model: MODEL, utils, root, save=True):

    if parameters['verbose'] == 0:
        blockPrint()

    outfile = utils.make_file_name(parameters, root=root)
    n_exist_sample, all_result = utils.read_current_results(outfile)
    n_sample = parameters['n_sample']

    if n_exist_sample >= n_sample:
        print(
            "\n\n-----------> Samples already computed in file -- terminating ... <-----------"
        )
        return all_result

    print("\n\n-----------> Starting stochastic descent <-----------")

    n_iteration_left = n_sample - n_exist_sample  # data should be saved 10 times --> no more (otherwise things are way too slow !)
    n_mod = max([1, n_iteration_left // 10])

    for it in range(n_iteration_left):

        start_time = time.time()

        if parameters['task'] == 'SD':
            best_fid, best_protocol, n_fid_eval = SD(
                parameters, model,
                init=True)  # -- --> performing stochastic descent here <-- --
        elif parameters['task'] == 'SD2':
            best_fid, best_protocol, n_fid_eval = SD_2SF(
                parameters, model, init=True
            )  # -- --> performing 2 spin flip stochastic descent here <-- --
        else:
            assert False, 'Error in task specification'

        energy, delta_energy, Sent = model.compute_observables(
            protocol=best_protocol)

        result = [
            n_fid_eval, best_fid, energy, delta_energy, Sent, best_protocol
        ]

        print(
            "\n----------> RESULT FOR STOCHASTIC DESCENT NO %i <-------------"
            % (it + 1))
        print("Number of fidelity eval \t%i" % n_fid_eval)
        print("Best fidelity \t\t\t%.16f" % best_fid)
        print("Best hx_protocol\t\t", list(best_protocol))

        all_result.append(result)

        if save and it % n_mod == 0:
            with open(outfile, 'wb') as f:
                pickle.dump([parameters, all_result], f)
                f.close()
            print("Saved iteration --> %i to %s" %
                  (it + n_exist_sample, outfile))
        print("Iteration run time --> %.4f s" % (time.time() - start_time))

    print("\n Thank you and goodbye !")
    enablePrint()

    if save:
        with open(outfile, 'wb') as f:
            pickle.dump([parameters, all_result], f)
            f.close()
    return all_result
Пример #2
0
def run_SA(parameters, model: MODEL, utils, root, save=True):

    if parameters['verbose'] == 0:
        blockPrint()

    outfile = utils.make_file_name(parameters, root=root)
    n_exist_sample, optimal_index, all_result = utils.read_current_results(
        outfile)
    n_sample = parameters['n_sample']
    if optimal_index is not None:
        best_seen_fid = all_result[optimal_index][1]
    else:
        best_seen_fid = 0.0

    if parameters['Ti'] < 0.:
        parameters['Ti'] = compute_initial_Ti(parameters, model, n_sample=1000)
        print("Initial temperature Ti=%.3f" % parameters['Ti'])

    if n_exist_sample >= n_sample:
        print(
            "\n\n-----------> Samples already computed in file -- terminating ... <-----------"
        )

        print(
            "\n\n-------> Best encountered fidelity over all samples is %0.8f <-------"
            % (best_seen_fid))

        print("\n\n-------> Best encountered hx_protocol over all samples:")
        print(list(all_result[optimal_index][5]))
        print("<-------")

        return all_result

    print("\n\n-----------> Starting simulated annealing <-----------")

    n_iteration_left = n_sample - n_exist_sample  # data should be saved 10 times --> no more (otherwise things are way too slow !)
    n_mod = max([1, n_iteration_left // 10])

    for it in range(n_iteration_left):

        start_time = time.time()
        best_fid, best_protocol, n_fid_eval = SA(
            parameters, model)  # -- --> performing annealing here <-- --

        if parameters['task'] == 'SASD':
            print(' -> Stochastic descent ... ')
            model.update_protocol(best_protocol)
            best_fid, best_protocol, n_fid_eval_SD = SD(parameters,
                                                        model,
                                                        init=False)
            n_fid_eval += n_fid_eval_SD

        energy, delta_energy, Sent = model.compute_observables(
            protocol=best_protocol)

        result = [
            n_fid_eval, best_fid, energy, delta_energy, Sent, best_protocol
        ]

        print("\n----------> RESULT FOR ANNEALING NO %i <-------------" %
              (it + 1))
        print("Number of fidelity eval \t%i" % n_fid_eval)
        print("Best fidelity \t\t\t%.4f" % best_fid)
        print("Best hx_protocol\t\t", list(best_protocol))

        all_result.append(result)

        # check if a better fidelity has been seen in previous samples
        if best_fid > best_seen_fid:
            best_seen_fid = best_fid
            optimal_index = len(all_result) - 1

        if save and it % n_mod == 0:
            with open(outfile, 'wb') as f:
                pickle.dump([parameters, all_result], f)
                f.close()
            print("Saved iteration --> %i to %s" %
                  (it + n_exist_sample, outfile))
        print("Iteration run time --> %.4f s" % (time.time() - start_time))

    # print best seen fidelity and protocol over all times
    print(
        "\n\n-------> Best encountered fidelity over all samples is %0.8f <-------"
        % (best_seen_fid))

    print("\n\n-------> Best encountered hx_protocol over all samples:")
    print(list(all_result[optimal_index][5]))
    print("<-------")

    print("\n Thank you and goodbye !")
    enablePrint()

    if save:
        with open(outfile, 'wb') as f:
            pickle.dump([parameters, all_result], f)
            f.close()
    return all_result