kaiwu.sampler package#
Module contents#
Module: sampler
Function: Provide a series of data post-processing tools
- class kaiwu.sampler.SimulatedAnnealingSampler(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:
SimulatedAnnealingSamplerBaseSimulated annealing sampler for solving Ising models. Each solution is solved independently
- Parameters:
initial_temperature (float) – Initial temperature.
alpha (float) – Temperature reduction coefficient.
cutoff_temperature (float) – Cutoff temperature.
iterations_per_t (int) – Iteration depth per temperature.
size_limit (int) – Number of output solutions; defaults to 100 solutions
flag_evolution_history (bool) – Whether to output the Hamiltonian evolution history; defaults to False. When True, the evolution history can be obtained via the get_ha_history method
verbose (bool) – Whether to output calculation progress in the console; defaults to False
rand_seed (int, optional) – Random seed for the numpy random number generator
process_num (int, optional) – Number of parallel processes (-1 automatically uses all available cores, 1 for 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 Hamiltonian evolution history
- Returns:
Hamiltonian evolution history over time, where keys are time in seconds and values are Hamiltonian values
- Return type:
dict
- single_process_solve(ising_matrix=None, init_solution=None, rand_seed=None, size_limit=None)#
Solve 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 the 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)
- set_matrix(ising_matrix)#
Set matrix and update related 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)#