kaiwu.classical package#

Module Contents#

Module: classical

Function: Provides a series of classic solvers

class kaiwu.classical.SimulatedAnnealingOptimizer(initial_temperature=100, alpha=0.99, cutoff_temperature=0.001, iterations_per_t=10, size_limit=100, flag_evolution_history=False, verbose=False, rand_seed=None, process_num=1)#

Bases: OptimizerBase, JsonSerializableMixin

Simulated annealing solver for the CIM Ising model (with stochastic output).

Args:

initial_temperature (float): initial temperature.

alpha (float): cooling coefficient.

cutoff_temperature (float): cutoff temperature.

iterations_per_t (int): Depth of iterations per temperature.

size_limit (int): The number of solutions to output. By default, 100 solutions are output.

flag_evolution_history (bool): Whether to output the Hamiltonian evolution history, the default is False, when the value is True, the evolution history is obtained through the get_ha_history method

verbose (bool): Whether to output the calculation progress in the console, default is False

rand_seed (int, optional): Random seed for numpy random number generator

process_num (int, optional): Number of parallel processes (-1 means automatically using all available cores, 1 means single process). Defaults to 1.

Examples:
>>> import numpy as np
>>> import kaiwu as kw
>>> matrix = -np.array([[ 0. ,  1. ,  0. ,  1. ,  1. ],
...                     [ 1. ,  0. ,  0. ,  1.,   1. ],
...                     [ 0. ,  0. ,  0. ,  1.,   1. ],
...                     [ 1. ,  1.,   1. ,  0. ,  1. ],
...                     [ 1. ,  1.,   1. ,  1. ,  0. ]])
>>> worker = kw.classical.SimulatedAnnealingOptimizer(initial_temperature=100,
...                                                   alpha=0.99,
...                                                   cutoff_temperature=0.001,
...                                                   iterations_per_t=10,
...                                                   size_limit=10)
>>> # This value is random and cannot be predicted
>>> worker.solve(matrix) 
array([[-1,  1, -1,  1,  1],
       [-1,  1,  1, -1,  1],
       [-1,  1,  1, -1, -1],
       [ 1, -1, -1,  1,  1],
       [ 1, -1,  1, -1,  1],
       [ 1, -1,  1, -1, -1],
       [ 1, -1, -1, -1,  1],
       [ 1, -1,  1,  1, -1],
       [ 1,  1,  1, -1, -1],
       [-1, -1, -1,  1,  1]])
get_ha_history()#

Get the Hamiltonian evolution history

Returns:

dict: Hamiltonian evolution history over time, key is time in seconds, value is hamilton

single_process_solve(ising_matrix=None, init_solution=None, rand_seed=None)#

Solving the Ising Matrix in a Single Process

Args:

ising_matrix (np.ndarray, optional): Ising matrix. Defaults to None.

init_solution (np.ndarray, optional): Initial solution vector. Defaults to None.

rand_seed (int, optional): Random seed for numpy random number generator

Returns:

np.ndarray: solution vector

solve(ising_matrix=None, init_solution=None)#

SA solves the Ising matrix solve interface

Args:

ising_matrix (np.ndarray, optional): Ising matrix. Defaults to None.

init_solution (np.ndarray, optional): Initial solution vector. Defaults to None.

Returns:

np.ndarray: Multiple processes merge the solution vectors after deduplication.

class kaiwu.classical.TabuSearchOptimizer(max_iter, recency_size=None, kmax=3, span_control_p1=3, span_control_p2=7, size_limit=1)#

Bases: OptimizerBase

Tabu search solver for the CIM Ising model.

Args:

max_iter (int): maximum number of iterations

recency_size (int): The size of the recency taboo table. If the input is empty, 1/10 of the matrix side length is used and rounded up.

kmax (int): The maximum value of the model parameter variable k. The default value is 3.

span_control_p1 (int): Parameter p1 that affects span changes. The default value is 3.

span_control_p2 (int): Parameter p2 that affects span changes. The default value is 7.

size_limit (int): maintain the size of the solution set

Examples:
>>> import numpy as np
>>> import kaiwu as kw
>>> matrix = -np.array([[ 0. ,  1. ,  0. ,  1. ,  1. ],
...                     [ 1. ,  0. ,  0. ,  1.,   1. ],
...                     [ 0. ,  0. ,  0. ,  1.,   1. ],
...                     [ 1. ,  1.,   1. ,  0. ,  1. ],
...                     [ 1. ,  1.,   1. ,  1. ,  0. ]])
>>> worker = kw.classical.TabuSearchOptimizer(10, size_limit=1)
>>> worker.solve(matrix) 
array([[ 1,  1,  1, -1, -1]])
init_solution(solution)#

Initialize the solution vector

Args:

solution (np.ndarray): initial solution vector

set_matrix(ising_matrix)#

Set up the matrix and update the relevant content

solve(ising_matrix=None, solution=None)#

Solving the Ising Matrix

Args:

ising_matrix (np.ndarray, optional): Ising matrix. Defaults to None.

solution (np.ndarray, optional): Initial solution vector. Defaults to None.

Returns:

np.ndarray: solution vector

kaiwu.classical.simulated_annealing(matrix, s=None, T_init=100, alpha=0.99, T_min=0.001, iterations_per_T=10, size_limit=100, verbose=False)#

Simulated annealing solver for the CIM Ising model.

Args:

matrix (np.ndarray): CIM Ising matrix.

s (np.ndarray or None): Iteration initial value, which is a numpy vector of length len(matrix). If not entered, it is initialized to random by default.

T_init (float): initial temperature.

alpha (float): cooling coefficient.

T_min (float): cutoff temperature.

iterations_per_T (int): Depth of iterations per temperature.

size_limit (int): the number of solutions returned

verbose (bool): Whether to output the calculation progress in the console, default is False

Returns:

np.ndarray: the solution vector obtained by iteration.

Examples:
>>> import numpy as np
>>> import kaiwu as kw
>>> matrix = -np.array([[ 0. ,  1. ,  0. ,  1. ,  1. ],
...                     [ 1. ,  0. ,  0. ,  1.,   1. ],
...                     [ 0. ,  0. ,  0. ,  1.,   1. ],
...                     [ 1. ,  1.,   1. ,  0. ,  1. ],
...                     [ 1. ,  1.,   1. ,  1. ,  0. ]])
>>> output = kw.classical.simulated_annealing(matrix,
...                                           s=None,
...                                           T_init=100,
...                                           alpha=0.5,
...                                           T_min=0.1,
...                                           iterations_per_T=10,
...                                           size_limit=10)
>>> output 
array([[-1,  1, -1, -1,  1],
       [-1,  1, -1,  1,  1],
       [-1,  1, -1,  1, -1],
       [-1,  1,  1, -1, -1],
       [ 1, -1,  1, -1, -1],
       [-1,  1,  1,  1, -1],
       [-1, -1, -1,  1,  1],
       [ 1,  1,  1, -1, -1],
       [-1,  1,  1, -1,  1],
       [ 1, -1,  1, -1,  1]])

Deprecated since version 1.0.0: You can use the standard function kw.classical.SimulatedAnnealingOptimizer().

Tabu search solver for the CIM Ising model.

Args:

ising_matrix (np.ndarray): CIM Ising matrix.

max_iter (int): maximum number of iterations

init (np.ndarray): Iteration initial value, which is a numpy vector of length len(matrix). If the input is empty, a vector is randomly generated.

t (int): The size of the recency taboo table. If the input is empty, 1/10 of the matrix side length is used and rounded up.

kmax (int): The maximum value of the model parameter variable k. The default value is 3.

p1 (int): Parameter p1 that affects span changes. The default value is 3.

p2 (int): Parameter p2 that affects span changes. The default value is 7.

Returns:

np.ndarray: the solution vector obtained by iteration.

Examples:
>>> import numpy as np
>>> import kaiwu as kw
>>> matrix = -np.array([[ 0. ,  1. ,  0. ,  1. ,  1. ],
...                     [ 1. ,  0. ,  0. ,  1.,   1. ],
...                     [ 0. ,  0. ,  0. ,  1.,   1. ],
...                     [ 1. ,  1.,   1. ,  0. ,  1. ],
...                     [ 1. ,  1.,   1. ,  1. ,  0. ]])
>>> output = kw.classical.tabu_search(matrix, 10)
>>> output 
array([[ 1,  1,  1, -1, -1]])

Deprecated since version 1.0.0: You can use the standard function kw.classical.TabuSearchOptimizer().