kaiwu.cim package#

Module Contents#

Module: cim

including Ising precision calculation and other tools

kaiwu.cim.adjust_ising_matrix_precision(ising_matrix, bit_width=8)#

Adjust the precision of the ising matrix. After adjustment through this interface, the matrix may have a large precision loss. For example, if one number in the matrix is much larger than the other numbers, the precision loss of the adjusted matrix is serious and cannot be used.

Args:

ising_matrix(np.ndarray): target matrix

bit_width(int): Precision range, currently only supports 8 bits, one of which is the sign bit

Returns:

np.ndarray: ising matrix that meets the accuracy requirements

Examples:
>>> import numpy as np
>>> import kaiwu as kw
>>> ori_ising_mat1 = np.array([[0, 0.22, 0.198],
...                            [0.22, 0, 0.197],
...                            [0.198, 0.197, 0]])
>>> ising_mat1 = kw.cim.adjust_ising_matrix_precision(ori_ising_mat1)
>>> ising_mat1
array([[  0, 127, 114],
       [127,   0, 114],
       [114, 114,   0]])
>>> ori_ising_mat2 = np.array([[0, 0.22, 0.198],
...                            [0.22, 0, 50],
...                            [0.198, 50, 0]])
>>> ising_mat2 = kw.cim.adjust_ising_matrix_precision(ori_ising_mat2)
>>> ising_mat2  # The solutions obtained by qubo_mat2 and ori_qubo_mat2 matrices are quite different
array([[  0,   1,   1],
       [  1,   0, 127],
       [  1, 127,   0]])
kaiwu.cim.calculate_ising_matrix_bit_width(ising_matrix, bit_width=8)#

Calculate the parameter width of the ising matrix

Args:

ising_matrix (np.ndarray): ising matrix

bit_width (int): Maximum bit width limit

Returns:

dict: Returns the precision and scaling factors of the Ising matrix

  • precision (int): Ising matrix precision

  • multiplier (float): scaling factor

Example 1:
>>> import numpy as np
>>> import kaiwu as kw
>>> _matrix = -np.array([[ -0., 127., -12.,  -5.],
...                      [127.,  -0., -12., -12.],
...                      [-12., -12.,  -0.,  -9.],
...                      [ -5., -12.,  -9.,  -0.]])
>>> kw.cim.calculate_ising_matrix_bit_width(_matrix)
{'precision': 8, 'multiplier': 1.0}
Example 2 (scaled to meet the requirements):
>>> import numpy as np
>>> import kaiwu as kw
>>> _matrix = -np.array([[ -0., 12.7, -1.2,  -0.5],
...                      [12.7,  -0., -1.2, -1.2],
...                      [-1.2, -1.2, -0.,   -0.9],
...                      [-0.5, -1.2, -0.9,  -0.]])
>>> kw.cim.calculate_ising_matrix_bit_width(_matrix)
{'precision': 8, 'multiplier': 10.0}
Example 3 (scaling does not meet the requirements):
>>> import numpy as np
>>> import kaiwu as kw
>>> _matrix = -np.array([[-488.,  516.,  -48.],
...                      [ 516., -516.,  -48.],
...                      [ -48.,  -48.,   60.]])
>>> kw.cim.calculate_ising_matrix_bit_width(_matrix)
{'precision': inf, 'multiplier': inf}