kaiwu_community.common package#

Module contents#

通用工具集合

kaiwu_community.common.check_symmetric(mat, tolerance=1e-08)#

检查矩阵是否为对称矩阵,允许一定误差

Args:

mat (np.ndarray): 矩阵.

tolerance (float): 误差.

Returns:

bool: 是否为对称矩阵.

Examples:
>>> import numpy as np
>>> import kaiwu_community as kw
>>> 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. ]])
>>> print(kw.common.check_symmetric(ising_matrix))
True
kaiwu_community.common.hamiltonian(ising_matrix, c_list)#

计算哈密顿量.

Args:

ising_matrix (np.ndarray): CIM Ising 矩阵.

c_list (np.ndarray): 要计算哈密顿量的变量组合集合.

Returns:

np.ndarray: 哈密顿量集合.

Examples:
>>> import numpy as np
>>> import kaiwu_community as kw
>>> 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. ]])
>>> rng = np.random.default_rng(10)
>>> optimizer = kw.classical.BruteForceOptimizer()
>>> output = optimizer.solve(ising_matrix)
>>> h = kw.common.hamiltonian(ising_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_community.common.set_log_level(level)#

设置SDK日志输出级别

SDK默认输出INFO级别以上的日志,可通过此函数修改输出级别

Args:

level (str or int): 支持传入 logging.ERROR, logging.INFO、... 、logging.ERROR或字符串ERROR、INFO、... 、ERROR

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

设置SDK日志输出文件路径, 需要绝对路径.

Args:

path (str): 自定义日志文件输出路径

Examples:
>>> import kaiwu_community as kw
>>> kw.common.set_log_path("/tmp/output.log")