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): 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.

truncated_precision (int): truncation precision, the maximum precision that can be calculated, the default value is recommended

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(user_id, sdk_code, task_name=None, machine_name=None, wait=False, interval=1)#

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:

user_id (str): User ID, used for authentication.

sdk_code (str): SDK authorization code used to access CIM computing services.

task_name (str, optional): Task name, which can be used to identify the task. The default value is None.

machine_name (str, optional): Specifies the computer model, the default is CPQC-550 (550-bit real machine).

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

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

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(user_id='1241241515', sdk_code='absd1232')  
>>> 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)
Notes:
  1. The intermediate file save path (save_dir) needs to be set through CheckpointManager.

  2. The unique identifier of a task is determined by both ising_matrix and task_name. Changing either of them will create a new task.

  3. The same matrix can create different tasks by modifying task_name. If you only need to query the results, please make sure task_name remains unchanged.

get_machine_info()#

Get real machine information and corresponding quota

Returns:

dict: real machine information and corresponding quotas, such as:

{

“CPQC-1”: 99, “CPQC-550”: 93

}

set_machine_name(machine_name)#

Set the machine name

Args:

machine_name (str): machine name

Returns:

dict: None

solve(ising_matrix=None, task_name=None)#

Entry function

Args:

ising_matrix (np.ndarray): ising matrix

The task_name setting here has high priority and overrides the task_name set when instantiating. If it is not specified when creating the instance, it must be specified here

Returns:
np.ndarray | None:
  • solution vector collection (upon completion of the task)

  • None (task is still being calculated)

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.OptimizerBase#

Bases: object

Ising Optimizer base class

set_matrix(ising_matrix)#

Set up the matrix and update the relevant content

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

solve(ising_matrix=None)#

solve