Esempio n. 1
0
def compare_methods(N):
    """
    Compare different optimization methods
    :param N: dimensionality of quantum system
    :return: a dictionary of results
    """
    # generate a random system
    sys = get_rand_unitary_sys(N)

    max_cost_func = sys["max_cost_func"]

    # we wrap the propagator to track how many calls each method makes
    propagator = sys["propagator"] = counted(sys["propagator"])

    # initialize the dictionary where results are stored
    results = dict(max_cost_func=max_cost_func, N=N)

    # ###################### Dynamical programming ######################
    results["dp_max"] = QStochDynProg(
        **sys).make_rounds(21).max_cost() / max_cost_func

    # save number of calls made during optimization
    results["dp_calls"] = propagator.calls

    # reset the count
    propagator.calls = 0

    ################ Hybrid monte-carlo tree seach and dynamical programming #############
    results["mcdp_max"] = MTCDynProg(nsteps=10, **sys).make_rounds(
        2**13).max_cost() / max_cost_func

    # save number of calls made during optimization
    results["mcdp_calls"] = propagator.calls

    # reset the count
    propagator.calls = 0

    ###################### Monte-Carlo tree search ######################
    results["mcts_max"] = MCTreeSearch(nsteps=2**10, **sys).make_rounds(
        2**13).max_cost() / max_cost_func

    # save number of calls made during optimization
    results["mcts_calls"] = propagator.calls

    # reset the count
    propagator.calls = 0

    ###################### Genetic algorithm ######################
    results["ga_max"] = GA(nsteps=2**11, pop_size=2**7, **sys).make_rounds(
        2**9).max_cost() / max_cost_func

    # save number of calls made during optimization
    results["ga_calls"] = propagator.calls

    # reset the count
    propagator.calls = 0

    return results
Esempio n. 2
0
#
###################################################################################################

if __name__ == '__main__':

    import matplotlib.pyplot as plt

    ###############################################################################################
    #
    #   Run the optimization
    #
    ###############################################################################################
    # import declaration of random system
    from get_randsys import get_rand_unitary_sys

    mcts = MCTreeSearch(nsteps=10, **get_rand_unitary_sys(5))

    for _ in range(1):

        mcts.make_rounds(200)

        print(
            "Known optimal control policy:                                    ",
            mcts.known_opt_control_policy)
        print(
            "Obtained optimal control policy via the Monte Carlo Tree search: ",
            mcts.opt_control_policy)
        print("Difference between the known optimal value and found: ",
              mcts.max_cost_func - mcts.max_cost())

        plt.title("Landscape")
Esempio n. 3
0
if __name__=='__main__':

    import matplotlib.pyplot as plt

    ###############################################################################################
    #
    #   Run the optimization
    #
    ###############################################################################################

    # import declaration of random system
    from get_randsys import get_rand_unitary_sys

    # initialize
    opt = QStochDynProg(**get_rand_unitary_sys(5))

    for iter_num in range(10):
        opt.next_time_step()

    ###############################################################################################
    #
    #   Plot results
    #
    ###############################################################################################

    # plt.title("Landscape")
    # plt.xlabel("time variable (dt)")
    # plt.ylabel("Value of objective function")
    # nx.draw(
    #     opt.landscape,
Esempio n. 4
0
    def max_cost(self):
        """
        Return the found maximal value of the cost fuction
        :return: max val
        """
        # Extract the best individual from the hol of fame
        return max(self.ga_obj_func(individual)[0] for individual in self.hof)


###################################################################################################
#
#   Test
#
###################################################################################################

if __name__ == '__main__':
    ###############################################################################################
    #
    #   Run the optimization
    #
    ###############################################################################################

    # import declaration of random system
    from get_randsys import get_rand_unitary_sys

    ga = GA(nsteps=100, pop_size=200, **get_rand_unitary_sys(10))

    print("Maximal attainable value ", ga.max_cost_func)

    ga.make_rounds(nrounds=100, verbose=True)
Esempio n. 5
0
# set the common axis
_, axes = plt.subplots(2, 3, sharex=True, sharey=True)

################################################################################
#
#   Plot Monte Carlo tree search
#
################################################################################

# send random seed
seed(122)
np.random.seed(6711)

# create the data for plotting
mcts = MCTreeSearch(nsteps=100, **get_rand_unitary_sys(5))
data = [deepcopy(mcts.make_rounds(1)) for _ in range(axes.shape[1])]

vmin = min(mcts.get_original_weight(_) for _ in mcts.decision_graph.node.values())
vmax = max(mcts.get_original_weight(_) for _ in mcts.decision_graph.node.values())

for title, mcts, ax in zip(["(a)", "(b)", "(c)"], data, axes[0]):

    ax.set_title(title)
    nx.draw(
        mcts.decision_graph,
        pos=mcts.get_pos_iteration_cost(),
        node_color=mcts.get_node_color_weight(),
        edge_color=mcts.get_edge_color_weight(),
        arrows=False,
        #alpha=0.8,