kaiwu.cim package#
Module contents#
Module: cim
Function: Provide a series of SPQC solver related tools
- class kaiwu.cim.CIMOptimizer(task_name, wait=False, interval=1, project_no=None, task_mode='optimization', sample_number=10)[source]#
Bases:
IsingSolver,QuboSolverCIM Optimizer Interface
CIMOptimizer is an optimizer for solving Ising computational problems. It submits tasks to a special-purpose quantum computer (SPQC, Special-Purpose Quantum Computer) for computation and returns the optimal solution.
Key features include:
Task submission: Upload the Ising matrix task to the SPQC 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 (str): task name
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): computing mode, defaulting to TaskMode.OPTIMIZATION; optional values are TaskMode.OPTIMIZATION and TaskMode.SAMPLING
sample_number (int): number of samples; required when task_mode=TaskMode.SAMPLING. Defaults to 10, minimum 10, maximum 2000
- Example:
>>> import numpy as np >>> import kaiwu as kw >>> from kaiwu.cim import TaskMode >>> 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', ... task_mode=TaskMode.OPTIMIZATION ... ) >>> 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 jointly determined by ising_matrix and task_name; a change in either one creates a new task.
Different tasks can be created for the same matrix by changing task_name. If you only need to query results, make sure task_name remains unchanged.
task_name is required when instantiating CIMOptimizer
- get_hamiltonian()#
- Returns:
hamiltonian (np.ndarray): Hamiltonian value of the current solution
- on_matrix_change()#
Update matrix related information, which can be implemented when inheriting IsingSolver. 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
- solve(ising_matrix=None, negtail_flip=True, sort_solutions=False)#
Solve the Ising matrix
- Args:
ising_matrix (np.ndarray): ising matrix
negtail_flip (bool): Whether to perform negative-tail flipping
sort_solutions (bool): Whether to sort solutions
- Returns:
output (np.ndarray): solution vector
- solve_qubo(*args, **kwargs)#