kaiwu.sampler package#
Module Contents#
Module: sampler
Features: Provides a series of data post-processing tools
- kaiwu.sampler.binarizer(c_list, var_type='spin', unique=True)#
binarization.
- Args:
c_list (np.ndarray): The data to be binarized obtained from the solver.
var_type (str): Variable type.
unique (bool): Deduplicate.
- Returns:
np.ndarray: Binarized result.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> solutions = np.array([[-0.0032379, -0.00302381, -0.00080672, -0.00226754, 0.00400546], ... [-0.00063725, -0.00769352, 0.00140344, -0.00234206, -0.00034028], ... [0.0040677, -0.01139299, 0.00474858, 0.00274578, -0.00405366], ... [0.00765783, -0.00636498, 0.00964927, 0.00262433, -0.00175035], ... [0.01104296, -0.00444736, 0.01377671, 0.0061532, 0.00074683]]) >>> c_set = kw.sampler.binarizer(solutions) >>> c_set 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]])
- kaiwu.sampler.binary_to_spin(c_set)#
Binary to spin.
- Args:
c_set (np.ndarray): Binary variable configuration.
- Returns:
np.ndarray: Spin variable configuration.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> output = np.array([[0, 0, 0, 0], ... [0, 0, 0, 1], ... [0, 0, 1, 0], ... [0, 0, 1, 1], ... [0, 1, 0, 0], ... [0, 1, 0, 1], ... [0, 1, 1, 0], ... [0, 1, 1, 1], ... [1, 0, 0, 0], ... [1, 0, 0, 1], ... [1, 0, 1, 0], ... [1, 0, 1, 1], ... [1, 1, 0, 0], ... [1, 1, 0, 1], ... [1, 1, 1, 0], ... [1, 1, 1, 1]]) >>> kw.sampler.binary_to_spin(output) 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, -1, -1], [ 1, 1, -1, 1], [ 1, 1, 1, -1], [ 1, 1, 1, 1]])
- kaiwu.sampler.calculate_qubo_value(qubo_matrix, offset, binary_configuration)#
Q value calculator.
- Args:
qubo_matrix (np.ndarray): QUBO matrix.
offset (float): Constant term.
binary_configuration (np.ndarray): Binary configuration
- Returns:
output (float): Q value.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> matrix = np.array([[1., 0., 0.], ... [0., 1., 0.], ... [0., 0., 1.]]) >>> offset = 1.8 >>> binary_configuration = np.array([0, 1, 0]) >>> qubo_q = kw.sampler.calculate_qubo_value(matrix, offset, binary_configuration) >>> print(qubo_q) 2.8
- kaiwu.sampler.constraint_sampler(matrix, offset, var_index, c_set)#
Constraint sampler
- Args:
matrix (np.ndarray): QUBO matrix of constraints.
offset (float): Constant term.
var_index (list): Variables contained in the constraints, index table in all variables.
c_set (np.ndarray): Variable configuration.
- Returns:
output (np.ndarray, np.ndarray): Variable configuration sorted by relaxation degree, relaxation degree.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> output = np.array([[0, 0, 0, 0], ... [0, 0, 0, 1], ... [0, 0, 1, 0], ... [0, 0, 1, 1], ... [0, 1, 0, 0], ... [0, 1, 0, 1], ... [0, 1, 1, 0], ... [0, 1, 1, 1], ... [1, 0, 0, 0], ... [1, 0, 0, 1], ... [1, 0, 1, 0], ... [1, 0, 1, 1], ... [1, 1, 0, 0], ... [1, 1, 0, 1], ... [1, 1, 1, 0], ... [1, 1, 1, 1]]) >>> matrix = np.array([[1., 0., 0.], ... [0., 1., 0.], ... [0., 0., 1.]]) >>> offset = -3 >>> var_index = [1, 2, 3] >>> kw.sampler.constraint_sampler(matrix, offset, var_index, output) (array([[0, 1, 1, 1], [1, 1, 1, 1], [0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0], [1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 1, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0]]), array([0., 0., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 3., 3.]))
- kaiwu.sampler.hamiltonian(matrix, c_list)#
Hamiltonian calculation
- Args:
matrix (np.ndarray): CPQC Ising matrix.
c_list (np.ndarray): The set of variable combinations to calculate the Hamiltonian.
- Returns:
np.ndarray: Hamiltonian 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. ]]) >>> rng = np.random.default_rng(10) >>> solver = kw.classical.SimulatedAnnealingOptimizer() >>> output = solver.solve(matrix) >>> h = kw.sampler.hamiltonian(matrix, output) >>> h array([-0.60179257, -0.60179257, -0.60179257, -0.60179257, -0.60179257, -1.20358514, -0.60179257, -0.60179257, -0.60179257, -1.20358514])
- kaiwu.sampler.negtail_flip(c_list)#
Negative tail flip. Negtail flip is need for sampling CPQC Ising results generated by kaiwu.qubo. No negtail flip is needed for solving with direct input matrix.
- Args:
c_list (np.ndarray): Variable combination set for negtail flip.
- Returns:
None: in-place algorithm is used for this method. Modify input directly.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> mat = np.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]]) >>> kw.sampler.negtail_flip(mat) >>> mat 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]])
- kaiwu.sampler.optimal_sampler(matrix, c_set, bias=0.0, negtail_ff=False)#
Optimal solution sampling.
- Args:
matrix (np.ndarray): CPQC Ising matrix.
c_set (np.ndarray): Variable configuration.
bias (float): Constant term.
negtail_ff (bool): negtail_flip flag. The matrix of Ising model with quadratic linear terms requires negative tail flipping
- Returns:
output (np.ndarray, np.ndarray): Variable configuration sorted by energy, energy.
- 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. ]]) >>> c_set = np.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]]) >>> kw.sampler.optimal_sampler(matrix, c_set, 0) (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]]), array([-8., -8., -8., -8., -4., 8.]))
- kaiwu.sampler.random_sampler(matrix, negtail_ff=False)#
Random sampler.
- Args:
matrix (np.ndarray): CPQC Ising matrix.
negtail_ff (bool): negtail_flip flag. The matrix of Ising model with quadratic linear terms requires negative tail flipping
- Returns:
np.ndarray: Binary configuration
- 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. ]]) >>> kw.sampler.random_sampler(matrix, False) array([[ 1, 1, -1, -1, 1]])
- kaiwu.sampler.spin_to_binary(c_set)#
Spin to binary
- Args:
c_set (np.ndarray): Spin variable configuration.
- Returns:
np.ndarray: Binary variable configuration.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> output = np.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, -1, -1], ... [ 1, 1, -1, 1], ... [ 1, 1, 1, -1], ... [ 1, 1, 1, 1]]) >>> kw.sampler.spin_to_binary(output) array([[0., 0., 0., 0.], [0., 0., 0., 1.], [0., 0., 1., 0.], [0., 0., 1., 1.], [0., 1., 0., 0.], [0., 1., 0., 1.], [0., 1., 1., 0.], [0., 1., 1., 1.], [1., 0., 0., 0.], [1., 0., 0., 1.], [1., 0., 1., 0.], [1., 0., 1., 1.], [1., 1., 0., 0.], [1., 1., 0., 1.], [1., 1., 1., 0.], [1., 1., 1., 1.]])