qiskit/tools/apps/optimization.py
def SPSA_optimization(obj_fun, initial_theta, SPSA_parameters, max_trials,
                      save_steps=1, last_avg=1):
    """Minimizes obj_fun(theta) with a simultaneous perturbation stochastic
    approximation algorithm.

    Args:
        obj_fun (callable): the function to minimize
        initial_theta (numpy.array): initial value for the variables of
            obj_fun
        SPSA_parameters (list[float]) :  the parameters of the SPSA
            optimization routine
        max_trials (int) : the maximum number of trial steps ( = function
            calls/2) in the optimization
        save_steps (int) : stores optimization outcomes each 'save_steps'
            trial steps
        last_avg (int) : number of last updates of the variables to average
            on for the final obj_fun
    Returns:
        list: a list with the following elements:
            cost_final : final optimized value for obj_fun
            theta_best : final values of the variables corresponding to
                cost_final
            cost_plus_save : array of stored values for obj_fun along the
                optimization in the + direction
            cost_minus_save : array of stored values for obj_fun along the
                optimization in the - direction
            theta_plus_save : array of stored variables of obj_fun along the
                optimization in the + direction
            theta_minus_save : array of stored variables of obj_fun along the
                optimization in the - direction
        list[QuantumCircuit]: the circuits used in optimization
    """

    theta_plus_save = []
    theta_minus_save = []
    cost_plus_save = []
    cost_minus_save = []
    theta = initial_theta
    theta_best = np.zeros(initial_theta.shape)
    circuits = []
    for k in range(max_trials):
        # SPSA Paramaters
        a_spsa = float(SPSA_parameters[0]) / np.power(k + 1 +
                                                      SPSA_parameters[4],
                                                      SPSA_parameters[2])
        c_spsa = float(SPSA_parameters[1]) / np.power(k + 1,
                                                      SPSA_parameters[3])
        delta = 2 * np.random.randint(2, size=np.shape(initial_theta)[0]) - 1
        # plus and minus directions
        theta_plus = theta + c_spsa * delta
        theta_minus = theta - c_spsa * delta
        # cost fuction for the two directions
        cost_plus, circuits_plus = obj_fun(theta_plus)
        cost_minus, circuits_minus = obj_fun(theta_minus)
        # derivative estimate
        g_spsa = (cost_plus - cost_minus) * delta / (2.0 * c_spsa)
        # updated theta
        theta = theta - a_spsa * g_spsa
        # saving
        if k % save_steps == 0:
            print('objective function at theta+ for step # ' + str(k))
            print("%.7f" % cost_plus)
            print(('objective function at theta- for step # ' + str(k)))
            print("%.7f" % cost_minus)
            theta_plus_save.append(theta_plus)
            theta_minus_save.append(theta_minus)
            cost_plus_save.append(cost_plus)
            cost_minus_save.append(cost_minus)
        # record circuits ran
        circuits += circuits_plus
        circuits += circuits_minus

        if k >= max_trials - last_avg:
            theta_best += theta / last_avg

    # final cost update
    cost_final, circuits_final = obj_fun(theta_best)
    circuits += circuits_final
    print('Final objective function is: %.7f' % cost_final)
    return [cost_final, theta_best, cost_plus_save, cost_minus_save,
            theta_plus_save, theta_minus_save], circuits
Minimizes obj_fun(theta) with a simultaneous perturbation stochastic
    approximation algorithm.

    Args:
        obj_fun (callable): the function to minimize
        initial_theta (numpy.array): initial value for the variables of
            obj_fun
        SPSA_parameters (list[float]) :  the parameters of the SPSA
            optimization routine
        max_trials (int) : the maximum number of trial steps ( = function
            calls/2) in the optimization
        save_steps (int) : stores optimization outcomes each 'save_steps'
            trial steps
        last_avg (int) : number of last updates of the variables to average
            on for the final obj_fun
    Returns:
        list: a list with the following elements:
            cost_final : final optimized value for obj_fun
            theta_best : final values of the variables corresponding to
                cost_final
            cost_plus_save : array of stored values for obj_fun along the
                optimization in the + direction
            cost_minus_save : array of stored values for obj_fun along the
                optimization in the - direction
            theta_plus_save : array of stored variables of obj_fun along the
                optimization in the + direction
            theta_minus_save : array of stored variables of obj_fun along the
                optimization in the - direction
        list[QuantumCircuit]: the circuits used in optimization
    
Minimizes obj_fun(theta) with a simultaneous perturbation stochastic
approximation algorithm.
Minimizes obj_fun ( theta ) with a simultaneous perturbation stochastic approximation algorithm .
def SPSA_optimization(obj_fun, initial_theta, SPSA_parameters, max_trials,
                      save_steps=1, last_avg=1):
    theta_plus_save = []
    theta_minus_save = []
    cost_plus_save = []
    cost_minus_save = []
    theta = initial_theta
    theta_best = np.zeros(initial_theta.shape)
    circuits = []
    for k in range(max_trials):
        a_spsa = float(SPSA_parameters[0]) / np.power(k + 1 +
                                                      SPSA_parameters[4],
                                                      SPSA_parameters[2])
        c_spsa = float(SPSA_parameters[1]) / np.power(k + 1,
                                                      SPSA_parameters[3])
        delta = 2 * np.random.randint(2, size=np.shape(initial_theta)[0]) - 1
        theta_plus = theta + c_spsa * delta
        theta_minus = theta - c_spsa * delta
        cost_plus, circuits_plus = obj_fun(theta_plus)
        cost_minus, circuits_minus = obj_fun(theta_minus)
        g_spsa = (cost_plus - cost_minus) * delta / (2.0 * c_spsa)
        theta = theta - a_spsa * g_spsa
        if k % save_steps == 0:
            print('objective function at theta+ for step # ' + str(k))
            print("%.7f" % cost_plus)
            print(('objective function at theta- for step # ' + str(k)))
            print("%.7f" % cost_minus)
            theta_plus_save.append(theta_plus)
            theta_minus_save.append(theta_minus)
            cost_plus_save.append(cost_plus)
            cost_minus_save.append(cost_minus)
        circuits += circuits_plus
        circuits += circuits_minus
        if k >= max_trials - last_avg:
            theta_best += theta / last_avg
    cost_final, circuits_final = obj_fun(theta_best)
    circuits += circuits_final
    print('Final objective function is: %.7f' % cost_final)
    return [cost_final, theta_best, cost_plus_save, cost_minus_save,
            theta_plus_save, theta_minus_save], circuits
def SPSA_optimization ( obj_fun , initial_theta , SPSA_parameters , max_trials , save_steps = 1 , last_avg = 1 ) : theta_plus_save = [ ] theta_minus_save = [ ] cost_plus_save = [ ] cost_minus_save = [ ] theta = initial_theta theta_best = np . zeros ( initial_theta . shape ) circuits = [ ] for k in range ( max_trials ) : a_spsa = float ( SPSA_parameters [ 0 ] ) / np . power ( k + 1 + SPSA_parameters [ 4 ] , SPSA_parameters [ 2 ] ) c_spsa = float ( SPSA_parameters [ 1 ] ) / np . power ( k + 1 , SPSA_parameters [ 3 ] ) delta = 2 * np . random . randint ( 2 , size = np . shape ( initial_theta ) [ 0 ] ) - 1 theta_plus = theta + c_spsa * delta theta_minus = theta - c_spsa * delta cost_plus , circuits_plus = obj_fun ( theta_plus ) cost_minus , circuits_minus = obj_fun ( theta_minus ) g_spsa = ( cost_plus - cost_minus ) * delta / ( 2.0 * c_spsa ) theta = theta - a_spsa * g_spsa if k % save_steps == 0 : print ( 'objective function at theta+ for step # ' + str ( k ) ) print ( "%.7f" % cost_plus ) print ( ( 'objective function at theta- for step # ' + str ( k ) ) ) print ( "%.7f" % cost_minus ) theta_plus_save . append ( theta_plus ) theta_minus_save . append ( theta_minus ) cost_plus_save . append ( cost_plus ) cost_minus_save . append ( cost_minus ) circuits += circuits_plus circuits += circuits_minus if k >= max_trials - last_avg : theta_best += theta / last_avg cost_final , circuits_final = obj_fun ( theta_best ) circuits += circuits_final print ( 'Final objective function is: %.7f' % cost_final ) return [ cost_final , theta_best , cost_plus_save , cost_minus_save , theta_plus_save , theta_minus_save ] , circuits
Minimizes obj_fun(theta) with a simultaneous perturbation stochastic
approximation algorithm.
Minimizes obj_fun ( theta ) with a simultaneous perturbation stochastic approximation algorithm .
"""Minimizes obj_fun(theta) with a simultaneous perturbation stochastic
    approximation algorithm.

    Args:
        obj_fun (callable): the function to minimize
        initial_theta (numpy.array): initial value for the variables of
            obj_fun
        SPSA_parameters (list[float]) :  the parameters of the SPSA
            optimization routine
        max_trials (int) : the maximum number of trial steps ( = function
            calls/2) in the optimization
        save_steps (int) : stores optimization outcomes each 'save_steps'
            trial steps
        last_avg (int) : number of last updates of the variables to average
            on for the final obj_fun
    Returns:
        list: a list with the following elements:
            cost_final : final optimized value for obj_fun
            theta_best : final values of the variables corresponding to
                cost_final
            cost_plus_save : array of stored values for obj_fun along the
                optimization in the + direction
            cost_minus_save : array of stored values for obj_fun along the
                optimization in the - direction
            theta_plus_save : array of stored variables of obj_fun along the
                optimization in the + direction
            theta_minus_save : array of stored variables of obj_fun along the
                optimization in the - direction
        list[QuantumCircuit]: the circuits used in optimization
    """ # SPSA Paramaters # plus and minus directions # cost fuction for the two directions # derivative estimate # updated theta # saving # record circuits ran # final cost update
a list with the following elements:
cost_final : final optimized value for obj_fun
theta_best : final values of the variables corresponding to
cost_final
cost_plus_save : array of stored values for obj_fun along the
optimization in the + direction
cost_minus_save : array of stored values for obj_fun along the
optimization in the - direction
theta_plus_save : array of stored variables of obj_fun along the
optimization in the + direction
theta_minus_save : array of stored variables of obj_fun along the
optimization in the - direction
list[QuantumCircuit]: the circuits used in optimization
a list with the following elements : cost_final : final optimized value for obj_fun theta_best : final values of the variables corresponding to cost_final cost_plus_save : array of stored values for obj_fun along the optimization in the + direction cost_minus_save : array of stored values for obj_fun along the optimization in the - direction theta_plus_save : array of stored variables of obj_fun along the optimization in the + direction theta_minus_save : array of stored variables of obj_fun along the optimization in the - direction list [ QuantumCircuit ] : the circuits used in optimization
list
obj_fun
the function to minimize
the function to minimize
initial_theta
initial value for the variables of
obj_fun
initial value for the variables of obj_fun
SPSA_parameters
the parameters of the SPSA
optimization routine
the parameters of the SPSA optimization routine
max_trials
the maximum number of trial steps ( = function
calls/2) in the optimization
the maximum number of trial steps ( = function calls / 2 ) in the optimization
save_steps
stores optimization outcomes each 'save_steps'
trial steps
stores optimization outcomes each ' save_steps ' trial steps
last_avg
number of last updates of the variables to average
on for the final obj_fun
number of last updates of the variables to average on for the final obj_fun