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]])
on_matrix_change()#

Update matrix related information

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.

load_json_dict(json_dict)#

The dict recovery object read from the JSON file

Returns:

Dict: json dictionary

set_matrix(ising_matrix)#

Set up the matrix and update the relevant content

to_json_dict(exclude_fields=('optimizer',))#

Convert to JSON dictionary

Returns:

Dict: json dictionary

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]])
set_matrix(ising_matrix)#

Set up the matrix and update the relevant content

init_solution(solution)#

Initialize the solution vector

Args:

solution (np.ndarray): initial solution vector

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

on_matrix_change()#

Update matrix related information, which can be implemented when inheriting OptimizerBase. When the processed ising matrix changes, the implementation of this function will be called, so that there is a chance to take corresponding actions