qiskit/tools/apps/optimization.py
def SPSA_calibration(obj_fun, initial_theta, initial_c, target_update, stat):
    """Calibrates and returns the SPSA parameters.

    Args:
        obj_fun (callable): the function to minimize.
        initial_theta (numpy.array): initial value for the variables of
            obj_fun.
        initial_c (float) : first perturbation of intitial_theta.
        target_update (float) : the aimed update of variables on the first
            trial step.
        stat (int) : number of random gradient directions to average on in
            the calibration.
    Returns:
        numpy.array: An array of 5 SPSA_parameters to use in the optimization.
        list[QuantumCircuit]: the circuits used in calibration
    """

    SPSA_parameters = np.zeros((5))
    SPSA_parameters[1] = initial_c
    SPSA_parameters[2] = 0.602
    SPSA_parameters[3] = 0.101
    SPSA_parameters[4] = 0
    delta_obj = 0
    circuits = []
    for i in range(stat):
        if i % 5 == 0:
            print('calibration step # ' + str(i) + ' of ' + str(stat))

        delta = 2 * np.random.randint(2, size=np.shape(initial_theta)[0]) - 1
        obj_plus, circuits_plus = obj_fun(initial_theta + initial_c * delta)
        obj_minus, circuits_minus = obj_fun(initial_theta - initial_c * delta)
        delta_obj += np.absolute(obj_plus - obj_minus) / stat
        circuits += circuits_plus
        circuits += circuits_minus

    SPSA_parameters[0] = target_update * 2 / delta_obj \
        * SPSA_parameters[1] * (SPSA_parameters[4] + 1)

    print('calibrated SPSA_parameters[0] is %.7f' % SPSA_parameters[0])

    return SPSA_parameters, circuits
Calibrates and returns the SPSA parameters.

    Args:
        obj_fun (callable): the function to minimize.
        initial_theta (numpy.array): initial value for the variables of
            obj_fun.
        initial_c (float) : first perturbation of intitial_theta.
        target_update (float) : the aimed update of variables on the first
            trial step.
        stat (int) : number of random gradient directions to average on in
            the calibration.
    Returns:
        numpy.array: An array of 5 SPSA_parameters to use in the optimization.
        list[QuantumCircuit]: the circuits used in calibration
    
Calibrates and returns the SPSA parameters.
Calibrates and returns the SPSA parameters .
def SPSA_calibration(obj_fun, initial_theta, initial_c, target_update, stat):
    SPSA_parameters = np.zeros((5))
    SPSA_parameters[1] = initial_c
    SPSA_parameters[2] = 0.602
    SPSA_parameters[3] = 0.101
    SPSA_parameters[4] = 0
    delta_obj = 0
    circuits = []
    for i in range(stat):
        if i % 5 == 0:
            print('calibration step # ' + str(i) + ' of ' + str(stat))
        delta = 2 * np.random.randint(2, size=np.shape(initial_theta)[0]) - 1
        obj_plus, circuits_plus = obj_fun(initial_theta + initial_c * delta)
        obj_minus, circuits_minus = obj_fun(initial_theta - initial_c * delta)
        delta_obj += np.absolute(obj_plus - obj_minus) / stat
        circuits += circuits_plus
        circuits += circuits_minus
    SPSA_parameters[0] = target_update * 2 / delta_obj \
        * SPSA_parameters[1] * (SPSA_parameters[4] + 1)
    print('calibrated SPSA_parameters[0] is %.7f' % SPSA_parameters[0])
    return SPSA_parameters, circuits
def SPSA_calibration ( obj_fun , initial_theta , initial_c , target_update , stat ) : SPSA_parameters = np . zeros ( ( 5 ) ) SPSA_parameters [ 1 ] = initial_c SPSA_parameters [ 2 ] = 0.602 SPSA_parameters [ 3 ] = 0.101 SPSA_parameters [ 4 ] = 0 delta_obj = 0 circuits = [ ] for i in range ( stat ) : if i % 5 == 0 : print ( 'calibration step # ' + str ( i ) + ' of ' + str ( stat ) ) delta = 2 * np . random . randint ( 2 , size = np . shape ( initial_theta ) [ 0 ] ) - 1 obj_plus , circuits_plus = obj_fun ( initial_theta + initial_c * delta ) obj_minus , circuits_minus = obj_fun ( initial_theta - initial_c * delta ) delta_obj += np . absolute ( obj_plus - obj_minus ) / stat circuits += circuits_plus circuits += circuits_minus SPSA_parameters [ 0 ] = target_update * 2 / delta_obj * SPSA_parameters [ 1 ] * ( SPSA_parameters [ 4 ] + 1 ) print ( 'calibrated SPSA_parameters[0] is %.7f' % SPSA_parameters [ 0 ] ) return SPSA_parameters , circuits
Calibrates and returns the SPSA parameters.
Calibrates and returns the SPSA parameters .
"""Calibrates and returns the SPSA parameters.

    Args:
        obj_fun (callable): the function to minimize.
        initial_theta (numpy.array): initial value for the variables of
            obj_fun.
        initial_c (float) : first perturbation of intitial_theta.
        target_update (float) : the aimed update of variables on the first
            trial step.
        stat (int) : number of random gradient directions to average on in
            the calibration.
    Returns:
        numpy.array: An array of 5 SPSA_parameters to use in the optimization.
        list[QuantumCircuit]: the circuits used in calibration
    """
An array of 5 SPSA_parameters to use in the optimization.
list[QuantumCircuit]: the circuits used in calibration
An array of 5 SPSA_parameters to use in the optimization . list [ QuantumCircuit ] : the circuits used in calibration
numpy.array
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 .
initial_c
first perturbation of intitial_theta.
first perturbation of intitial_theta .
target_update
the aimed update of variables on the first
trial step.
the aimed update of variables on the first trial step .
stat
number of random gradient directions to average on in
the calibration.
number of random gradient directions to average on in the calibration .