kaiwu.common package#

Module contents#

tool collection

kaiwu.common.hamiltonian(ising_matrix, c_list)#

Calculating the Hamiltonian

Parameters:
  • ising_matrix (np.ndarray) – Ising matrix.

  • c_list (np.ndarray) – The set of variable combinations for which the Hamiltonian is to be computed.

Returns:

Calculating the Hamiltonian

Return type:

np.ndarray

Examples

>>> import numpy as np
>>> from kaiwu.common import hamiltonian
>>> ising_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. ]])
>>> c_list = np.array([[1, 1, 1, 1, 1], [1, -1, 1, -1, 1]])
>>> h = hamiltonian(ising_matrix, c_list)   
kaiwu.common.check_symmetric(mat, tolerance=1e-08)#

Check if the matrix is a symmetric matrix, allowing for a certain margin of error.

kaiwu.common.set_log_level(level)#

Set SDK log output level

The SDK outputs logs above the info level by default, and the output level can be modified through this function

Parameters:

level (str or int) – supports logging. ERROR, logging.INFO,…, logging. ERROR or string ERROR, INFO,…, ERROR, case insensitive

Examples

>>> import kaiwu as kw
>>> kw.common.set_log_level(level="DEBUG")  
kaiwu.common.set_log_path(path='/tmp/output.log')#

Set the path to the SDK log output file; an absolute path is required.

Parameters:

path (str) – Custom log file output path

Examples

>>> import kaiwu as kw
>>> kw.common.set_log_path("/tmp/output.log")  
class kaiwu.common.CheckpointManager#

Bases: object

Manage checkpoints, saving the running state of objects for subsequent resumption from breakpoints. The directory for saving checkpoints can be specified by setting CheckpointManager.save_dir.

Parameters:

save_dir (str) – The directory where the checkpoint is saved.

save_dir = None#
classmethod get_path(obj)#

Get the path to the object checkpoint

Parameters:

obj (Object) – The object to be stored

Returns:

Get the path to the object checkpoint

Return type:

str

classmethod load(obj)#

Load serialized objects

Parameters:

obj (Object) – The object to be stored

Returns:

An object in JSON dictionary format

Return type:

str

classmethod dump(obj)#

The object is serialized and stored on the disk.

Parameters:

obj (Object) – The object to be stored

Returns:

Object instance

class kaiwu.common.BaseLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=inf, iterate_per_update=5)#

Bases: JsonSerializableMixin

A loop controller is used to calculate the time. This is for debugging and testing the algorithm.

Parameters:
  • max_repeat_step – Maximum number of steps, default is math.inf

  • target_objective – Target optimization function, stop when reached, default value is -math.inf

  • no_improve_limit – Object instance

  • iterate_per_update – Object instanceThe number of times to run before each update of the Hamiltonian, the default value is 5

update_status(objective, unsatisfied_constraints_count=None)#

Update status after calculating subissues

Parameters:
  • objective (float) – objective function value

  • unsatisfied_constraints_count (int) – number of unmet constraints

is_finished()#

Determine whether to stop

Returns:

Determine whether to stop

Return type:

bool

restart()#

Reinitialize count

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

Convert to JSON dictionary

Returns:

json dict

Return type:

dict

load_json_dict(json_dict)#

The dict recovery object read from the JSON file

Returns:

json dict

Return type:

dict

class kaiwu.common.OptimizerLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=20000, iterate_per_update=5)#

Bases: BaseLoopController

Optimizer loop controller and calculate time. For debugging and testing algorithms

Parameters:
  • max_repeat_step – Maximum number of steps, default is math.inf

  • target_objective – Target optimization function, stop when reached, default value is -math.inf

  • no_improve_limit – Convergence condition, stop if the specified number of updates does not improve, the default value is 20000

  • iterate_per_update – Object instanceThe number of times to run before each update of the Hamiltonian, the default value is 5

is_finished()#

Determine whether to stop

Returns:

Determine whether to stop

Return type:

bool

load_json_dict(json_dict)#

The dict recovery object read from the JSON file

Returns:

json dict

Return type:

dict

restart()#

Reinitialize count

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

Convert to JSON dictionary

Returns:

json dict

Return type:

dict

update_status(objective, unsatisfied_constraints_count=None)#

Update status after calculating subissues

Parameters:
  • objective (float) – objective function value

  • unsatisfied_constraints_count (int) – number of unmet constraints

class kaiwu.common.SolverLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=inf, iterate_per_update=5, stop_after_feasible_count=None)#

Bases: BaseLoopController, JsonSerializableMixin

Solver loop controller and calculate time. For debugging and testing algorithms

Parameters:
  • max_repeat_step (int) – Maximum number of steps, default is math.inf

  • target_objective (float) – Target optimization function, stop when reached, default value is -math.inf

  • no_improve_limit (int) – Convergence condition, stop if the specified number of updates does not improve, the default value is 20000

  • iterate_per_update (int) – Object instanceThe number of times to run before each update of the Hamiltonian, the default value is 5

  • stop_after_feasible_count (int) – Stop after finding the specified number of feasible solutions.

update_status(objective, unsatisfied_constraints_count=None)#

Update status after calculating subissues

Parameters:
  • objective (float) – objective function value

  • unsatisfied_constraints_count (int) – number of unmet constraints

is_finished()#

Determine whether to stop

Returns:

Determine whether to stop

Return type:

bool

load_json_dict(json_dict)#

The dict recovery object read from the JSON file

Returns:

json dict

Return type:

dict

restart()#

Reinitialize count

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

Convert to JSON dictionary

Returns:

json dict

Return type:

dict

class kaiwu.common.JsonSerializableMixin#

Bases: object

Serializer

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

Convert to JSON dictionary

Returns:

json dict

Return type:

dict

load_json_dict(json_dict)#

The dict recovery object read from the JSON file

Returns:

json dict

Return type:

dict

class kaiwu.common.HeapUniquePool(mat, size, size_limit)#

Bases: JsonSerializableMixin

Solution. Use a heap for maintenance

extend(solutions)#

Insert multiple solutions

push(solution, hamilton)#

Add a solution

get_solutions()#

Returns the maintained solution, the number of which is self.size_limit

clear()#

Clear solution set

to_json_dict(exclude_fields=None)#

Convert to JSON dictionary

Returns:

json dict

Return type:

dict

load_json_dict(json_dict)#

Construct a HeapUniquePool object from a dictionary read from a JSON file.

Parameters:

json_dict (dict) – json dict

Returns:

Object instance

Return type:

HeapUniquePool

class kaiwu.common.ArgpartitionUniquePool(mat, size, size_limit)#

Bases: object

Solution set. Use the linear expected complexity k of argpartition to maintain

extend(solutions, final=False)#

Insert multiple solutions

get_solutions()#

Get solution set

clear()#

Clear solution set

to_json_dict()#

Convert to JSON dictionary

Returns:

json dict

Return type:

dict

classmethod from_json_dict(json_dict)#

Construct a HeapUniquePool object from a dictionary read from a JSON file.

Parameters:

json_dict (dict) – json dict

Returns:

Object instance

Return type:

ArgpartitionUniquePool