kaiwu.common package#
Module contents#
工具集合
- kaiwu.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 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.SimulatedAnnealingOptimizer() >>> 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.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 as kw >>> kw.common.set_log_level("DEBUG")
- kaiwu.common.recursive_dict_conversion(qubo_expr)#
将expression转成dict.
- Args:
qubo_expr (QUBO): Qubo Expression
- Returns:
dict: 哈密顿量集合.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> a, b, c = kw.qubo.Binary('a'), kw.qubo.Binary('b'), kw.qubo.Binary('c') >>> y = (a + 2 * b) * (a * 3 + c) * 0.5 / 2 >>> y_dict = kw.common.recursive_dict_conversion(y) >>> y_dict {'coefficient': {('bb', 'bc'): 0.5, ('ba', 'bb'): 1.5, ('ba', 'bc'): 0.25, ('ba', 'ba'): 0.75}, 'offset': 0.0, 'hobo_var_dict': {}, 'hobo_constraint': {}}
- class kaiwu.common.CheckpointManager#
基类:
object
管理 checkpoint, 保存对象的运行状态,用于后续断点处恢复运行 通过设置 CheckpointManager.save_dir,可以指定 checkpoint 的保存目录。
- Args:
save_dir (str): checkpoint 的保存目录。
- save_dir = None#
- classmethod get_path(obj)#
获取对象checkpoint的路径
- Args:
obj (Object): 保存的对象
- Returns:
str: checkpoint路径
- classmethod load(obj)#
加载串行化的对象
- Args:
obj (Object): 保存的对象
- Returns:
str: json dict形式的对象
- classmethod dump(obj)#
对象串行化后存储在磁盘上
- Args:
obj (Object): 保存的对象
- Returns:
None
- class kaiwu.common.BaseLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=inf, iterate_per_update=5)#
-
循环控制器,并计算时间。用于调试和测试算法
- Args:
max_repeat_step: 最大步数,默认值为math.inf
target_objective:目标优化函数,达到即停止,默认值为-math.inf
no_improve_limit:收敛条件,指定更新次数没有改进则停止,默认值为math.inf
iterate_per_update:每次更新哈密顿量前运行的次数,默认值为5
- update_status(objective, unsatisfied_constraints_count=None)#
在计算子问题后更新状态
- Args:
objective (float): 目标函数值
unsatisfied_constraints_count (int): 未满足约束项的数量
- is_finished()#
判断是否应该停止
- Returns:
bool: 是否应该停止
- restart()#
重新初始化计数
- to_json_dict(exclude_fields=('timer',))#
转化为json字典
- Returns:
dict: json字典
- load_json_dict(json_dict)#
从json文件读取的dict恢复对象
- Returns:
dict: json字典
- class kaiwu.common.OptimizerLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=20000, iterate_per_update=5)#
-
Optimizer循环控制器,并计算时间。用于调试和测试算法
- Args:
max_repeat_step: 最大步数,默认值为math.inf
target_objective:目标优化函数,达到即停止,默认值为-math.inf
no_improve_limit:收敛条件,指定更新次数没有改进则停止,默认值为20000
iterate_per_update:每次更新哈密顿量前运行的次数,默认值为5
- is_finished()#
判断是否应该停止
- Returns:
bool: 是否应该停止
- load_json_dict(json_dict)#
从json文件读取的dict恢复对象
- Returns:
dict: json字典
- restart()#
重新初始化计数
- to_json_dict(exclude_fields=('timer',))#
转化为json字典
- Returns:
dict: json字典
- update_status(objective, unsatisfied_constraints_count=None)#
在计算子问题后更新状态
- Args:
objective (float): 目标函数值
unsatisfied_constraints_count (int): 未满足约束项的数量
- class kaiwu.common.SolverLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=inf, iterate_per_update=5)#
基类:
BaseLoopController
,JsonSerializableMixin
Solver循环控制器,并计算时间。用于调试和测试算法
- Args:
max_repeat_step (int): 最大步数,默认值为math.inf
target_objective (float): 目标优化函数,达到即停止,默认值为-math.inf
no_improve_limit (int): 收敛条件,指定更新次数没有改进则停止,默认值为20000
iterate_per_update (int): 每次更新哈密顿量前运行的次数,默认值为5
- is_finished()#
判断是否应该停止
- Returns:
bool: 是否应该停止
- load_json_dict(json_dict)#
从json文件读取的dict恢复对象
- Returns:
dict: json字典
- restart()#
重新初始化计数
- to_json_dict(exclude_fields=('timer',))#
转化为json字典
- Returns:
dict: json字典
- update_status(objective, unsatisfied_constraints_count=None)#
在计算子问题后更新状态
- Args:
objective (float): 目标函数值
unsatisfied_constraints_count (int): 未满足约束项的数量
- class kaiwu.common.JsonSerializableMixin#
基类:
object
序列化器
- to_json_dict(exclude_fields=('optimizer',))#
转化为json字典
- Returns:
dict: json字典
- load_json_dict(json_dict)#
从json文件读取的dict恢复对象
- Returns:
dict: json字典