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:
OptimizerBaseDecorator 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_prefix, wait=False, interval=1, project_no=None)#
Bases:
OptimizerBaseCIM 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:
Task submission: Upload the Ising matrix task to the CIM computing platform and create a computing task.
Task Query: Regularly check the task calculation status and obtain the calculation results.
Cache Management: Locally cache the results of calculated tasks to avoid repeated submission.
- Args:
task_name_prefix (str): Task name prefix, full task name is task_name_prefix_{ising_matrix_hash}
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.
- 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_prefix='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:
You need to set the intermediate file save path (save_dir) through CheckpointManager.
The unique identifier of a task is determined by ising_matrix and task_name_prefix, and a change in either one will create a new task.
The same matrix can be used to create different tasks by modifying task_name_prefix. If you only need to query the results, please ensure that task_name_prefix remains unchanged.
When instantiating CIMOptimizer, task_name_prefix is required; the task name is: {task_name_prefix}_{hash(ising_matrix)}
- 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