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)#

Bases: IsingSolver, QuboSolver

CIM 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:

  1. Task submission: Upload the Ising matrix task to the SPQC 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.

Parameters:
  • 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

  1. You need to set the intermediate file save path (save_dir) through CheckpointManager.

  2. The unique identifier of a task is jointly determined by ising_matrix and task_name; a change in either one creates a new task.

  3. 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.

  4. task_name is required when instantiating CIMOptimizer

get_task_result(ising_matrix: ndarray) dict#

Get task results

get_hamiltonian()#
Returns:

Hamiltonian value of the current solution

Return type:

hamiltonian (np.ndarray)

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

Parameters:
  • ising_matrix (np.ndarray) – ising matrix

  • negtail_flip (bool) – Whether to perform negative-tail flipping

  • sort_solutions (bool) – Whether to sort solutions

Returns:

solution vector

Return type:

output (np.ndarray)

solve_qubo(*args, **kwargs)#
class kaiwu.cim.TaskMode#

Bases: object

Task mode

OPTIMIZATION: optimization mode, formerly QUOTA mode. SAMPLING: sampling mode, formerly SAMPLE mode

OPTIMIZATION = 'optimization'#
SAMPLING = 'sampling'#