kaiwu.cim package#

Module contents#

Module: cim

Function: Provide a series of CIM solver related tools

class kaiwu.cim.PrecisionReducer(component: OptimizerBase, precision=8, target_bits=None, only_feasible_solution=True, truncated_precision=20)#

Bases: OptimizerBase

Decorator class for reduce of precision

You can open the log to see detailed output: kw.common.set_log_level (“DEBUG”)

Args:

component (OptimizerBase): base class.

precision (int): Ising matrix target precision

target_bits (int): Matrix target number of bits. Default is None, no control

only_feasible_solution (bool): This option determines whether only feasible solutions are allowed. The default value is True. An exception will be thrown if only_feasible_solution=True and none of the solutions are feasible.

only_feasible_solution (bool): Whether only feasible solutions are needed. The default is True. When only_feasible_solution=True and all solutions are not feasible solutions, an exception will be thrown.

Examples:
>>> import numpy as np
>>> import kaiwu as kw
>>> matrix = -np.array([[ 0. ,  1.23 ,  0. ,  1. ,  1. ],
...                     [ 1.23 ,  0. ,  0. ,  1.,   1. ],
...                     [ 0. ,  0. ,  0. ,  1.,   1. ],
...                     [ 1. ,  1.,   1. ,  0. ,  1. ],
...                     [ 1. ,  1.,   1. ,  1. ,  0. ]])
>>> optimizer = kw.classical.SimulatedAnnealingOptimizer(initial_temperature=100,
...                                                      alpha=0.99,
...                                                      cutoff_temperature=0.001,
...                                                      iterations_per_t=10,
...                                                      size_limit=5)
>>> new_optimizer = kw.cim.PrecisionReducer(optimizer, precision=4)
>>> new_optimizer.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]])

solve(ising_matrix=None)#

solve

on_matrix_change()#

Update matrix related information, which can be implemented when inheriting OptimizerBase. When the processed ising matrix changes, the implementation of this function will be called, so that there is a chance to take corresponding actions

set_matrix(ising_matrix)#

Set up the matrix and update the relevant content

class kaiwu.cim.CIMOptimizer(task_name, wait=False, interval=1, project_no=None, task_mode='quota', sample_number=10)#

Bases: OptimizerBase

CIM Optimizer Interface

CIMOptimizer is an optimizer for solving Ising computational problems. It submits tasks to a Coherent Optical Computer (CIM, Coherent Ising Machine) for computation and returns the optimal solution.

Key features include:

  1. Task submission: Upload the Ising matrix task to the CIM computing platform and create a computing task.

  2. Task Query: Regularly check the task calculation status and obtain the calculation results.

  3. Cache Management: Locally cache the results of calculated tasks to avoid repeated submission.

Args:

task_name (str): 任务名称

wait (bool, optional): Whether to wait for the calculation to complete. The default value is False.

interval (int, optional): Polling interval (minutes), default value is 1, minimum value is 1 minute.

project_no (str, optional): Project number, value is the project ID in the CPQC-X project list, used to create tasks under the project.

task_mode (str): 计算模式,默认为TaskMode.Quota, 可选值为 TaskMode.QUOTA, TaskMode.SAMPLE

sample_number (int): 采样次数, task_mode=TaskMode.SAMPLE时必填,默认值10,最小值10, 最大值2000

Example:
>>> import numpy as np
>>> import kaiwu as kw
>>> kw.common.CheckpointManager.save_dir = '/tmp'
>>> 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. ]])
>>> optimizer = kw.cim.CIMOptimizer(task_name='cim_optimizer_test')  
>>> solution = optimizer.solve(matrix)  
>>> print(solution)  
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]], dtype=int8)
>>> kw.common.CheckpointManager.save_dir = None
Notes:
  1. You need to set the intermediate file save path (save_dir) through CheckpointManager.

  2. 任务的唯一标识由 ising_matrixtask_name 共同决定,任何一项的变化都会创建新的任务。

  3. 同一个矩阵可以通过修改 task_name 创建不同的任务,若仅需查询结果,请确保 task_name 不变。

  4. 实例化CIMOptimizer时 task_name 必传

solve(ising_matrix=None)#

Entry function

Args:

ising_matrix (np.ndarray): ising matrix

Returns:
np.ndarray | None:
  • Solution vector set (when the task is completed)

  • None (The task is still being calculated)

get_task_result(ising_matrix: ndarray) dict#

Get task results

on_matrix_change()#

Update matrix related information, which can be implemented when inheriting OptimizerBase. When the processed ising matrix changes, the implementation of this function will be called, so that there is a chance to take corresponding actions

set_matrix(ising_matrix)#

Set up the matrix and update the relevant content