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: SimulatedAnnealingSamplerBase

Simulated annealing sampler for solving CIM Ising models. Each solution is solved independently

Args:

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:

dict: Hamiltonian evolution history over time, where keys are time in seconds and values are Hamiltonian values

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

Solve 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 the numpy random number generator

Returns:

np.ndarray: Solution vector

set_matrix(ising_matrix)#

Set matrix and update related content

solve(ising_matrix=None, init_solution=None, size_limit=None)#

SA solve interface for solving Ising matrices

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: Solution vectors after merging and deduplication from multiple processes.