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: IsingSolver, QuboSolver, JsonSerializableMixin

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

Parameters:
  • 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:

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

Return type:

dict

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

Solving the Ising Matrix in a Single Process

Parameters:
  • 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:

solution vector

Return type:

np.ndarray

get_hamiltonian()#
Returns:

Hamiltonian value of the current solution

Return type:

hamiltonian (np.ndarray)

load_json_dict(json_dict)#

The dict recovery object read from the JSON file

Returns:

Convert to JSON dictionary

Return type:

dict

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

Parameters:
  • ising_matrix (np.ndarray) – Isingmatrix

  • negtail_flip (bool) – Whether to perform negative-tail flipping

  • sort_solutions (bool) – Whether to sort solutions

Returns:

solution vector

Return type:

output (np.ndarray)

solve_qubo(*args, **kwargs)#
to_json_dict(exclude_fields=('_optimizer',))#

Convert to JSON dictionary

Returns:

Convert to JSON dictionary

Return type:

dict

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

Bases: IsingSolver, QuboSolver

Tabu search solver for the Ising model.

Parameters:
  • 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

Parameters:

solution (np.ndarray) – initial solution vector

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

Solve interface that calls the _solve method

Parameters:
  • 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:

solution vector

Return type:

np.ndarray

get_hamiltonian()#
Returns:

Hamiltonian value of the current solution

Return type:

hamiltonian (np.ndarray)

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#

Bases: IsingSolver, QuboSolver

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

get_hamiltonian()#
Returns:

Hamiltonian value of the current solution

Return type:

hamiltonian (np.ndarray)

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

Parameters:
  • ising_matrix (np.ndarray) – Isingmatrix

  • negtail_flip (bool) – Whether to perform negative-tail flipping

  • sort_solutions (bool) – Whether to sort solutions

Returns:

solution vector

Return type:

output (np.ndarray)

solve_qubo(*args, **kwargs)#