<commit_before>"""
Quantum Optimization tools.

These are simple tools that are used in our optimization examples

Author: <NAME> and <NAME>
"""
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.extensions.standard import h, ry, barrier, cz
import numpy as np
from tools.pauli import Pauli, label_to_pauli


def SPSA_optimization(obj_fun, initial_theta, SPSA_parameters, max_trials, save_steps = 10,last_avg=1):
    """Minimize the obj_fun(controls).

    initial_theta = the intial controls
    SPSA_parameters = the numerical parameters
    max_trials = the maximum number of trials
    """
    theta_plus_save = []
    theta_minus_save = []
    cost_plus_save = []
    cost_minus_save = []
    theta = initial_theta
    theta_best=np.zeros(initial_theta.shape)
    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 two directions
        cost_plus = obj_fun(theta_plus)[0]
        cost_minus = obj_fun(theta_minus)[0]
        # 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:<commit_msg>save_step default =1<commit_after>"""
Quantum Optimization tools.

These are simple tools that are used in our optimization examples

Author: <NAME> and <NAME>
"""
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.extensions.standard import h, ry, barrier, cz
import numpy as np
from tools.pauli import Pauli, label_to_pauli


def SPSA_optimization(obj_fun, initial_theta, SPSA_parameters, max_trials, save_steps = 1,last_avg=1):
    """Minimize the obj_fun(controls).

    initial_theta = the intial controls
    SPSA_parameters = the numerical parameters
    max_trials = the maximum number of trials
    """
    theta_plus_save = []
    theta_minus_save = []
    cost_plus_save = []
    cost_minus_save = []
    theta = initial_theta
    theta_best=np.zeros(initial_theta.shape)
    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 two directions
        cost_plus = obj_fun(theta_plus)[0]
        cost_minus = obj_fun(theta_minus)[0]
        # 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:
{'fraction_non_alphanumeric': 0.10229330114665057, 'fraction_numerical': 0.011466505733252867, 'mean_word_length': 3.390728476821192, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '211636', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1105, 'n_words': 255}