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)[source]#

Bases: IsingSolver, QuboSolver, JsonSerializableMixin

Simulated annealing solver for the 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()[source]#

Update matrix related information

get_ha_history()[source]#

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)[source]#

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

get_hamiltonian()#
Returns:

hamiltonian (np.ndarray): Hamiltonian value of the current solution

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

solve(ising_matrix=None, negtail_flip=True, sort_solutions=False)#

Solve the Ising matrix

Args:

ising_matrix (np.ndarray): Isingmatrix

negtail_flip (bool): Whether to perform negative-tail flipping

sort_solutions (bool): Whether to sort solutions

Returns:

output (np.ndarray): solution vector

solve_qubo(*args, **kwargs)#
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)[source]#

Bases: IsingSolver, QuboSolver

Tabu search solver for the 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)[source]#

Set up the matrix and update the relevant content

init_solution(solution)[source]#

Initialize the solution vector

Args:

solution (np.ndarray): initial solution vector

solve(ising_matrix=None, negtail_flip=True, sort_solutions=False, solution=None)[source]#

Solve interface that calls the _solve method

Args:

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

negtail_flip (bool): Whether to perform negative-tail flipping

sort_solutions (bool): Whether to sort solutions

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

Returns:

np.ndarray: solution vector

get_hamiltonian()#
Returns:

hamiltonian (np.ndarray): Hamiltonian value of the current solution

on_matrix_change()#

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

solve_qubo(*args, **kwargs)#
class kaiwu.classical.BruteForceOptimizer[source]#

Bases: IsingSolver, QuboSolver

A brute-force solver for the Ising model matrix, slow but accurate.

get_hamiltonian()#
Returns:

hamiltonian (np.ndarray): Hamiltonian value of the current solution

on_matrix_change()#

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

set_matrix(ising_matrix)#

Set up the matrix and update the relevant content

solve(ising_matrix=None, negtail_flip=True, sort_solutions=False)#

Solve the Ising matrix

Args:

ising_matrix (np.ndarray): Isingmatrix

negtail_flip (bool): Whether to perform negative-tail flipping

sort_solutions (bool): Whether to sort solutions

Returns:

output (np.ndarray): solution vector

solve_qubo(*args, **kwargs)#