kaiwu.conversion package#
Module contents#
A collection of conversion utilities.
- kaiwu.conversion.ising_matrix_to_qubo_matrix(ising_mat, remove_linear_bit=True)#
Convert an Ising matrix to a QUBO matrix.
- Args:
ising_mat (np.ndarray): The Ising matrix.
remove_linear_bit (bool): When converting QUBO to Ising, an auxiliary variable is introduced for linear terms. Whether to remove the last spin variable. Default is True.
- Returns:
tuple: A QUBO matrix and the corresponding bias term.
qubo_mat (np.ndarray): The QUBO matrix.
bias (float): The constant value representing the difference between the QUBO and Ising formulations.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> 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. ]]) >>> _qubo_mat, _ = kw.conversion.ising_matrix_to_qubo_matrix(matrix) >>> _qubo_mat array([[-4., 8., 0., 8.], [-0., -4., 0., 8.], [-0., -0., -0., 8.], [-0., -0., -0., -8.]])
- kaiwu.conversion.qubo_matrix_to_ising_matrix(qubo_mat)#
Convert a QUBO matrix to an Ising matrix.
- Args:
qubo_mat (np.ndarray): The QUBO matrix.
- Returns:
- tuple: An Ising matrix and the corresponding bias term.
ising_mat (np.ndarray): The Ising matrix.
bias (float): The constant value representing the difference between the QUBO and Ising formulations.
- Examples:
>>> import numpy as np >>> import kaiwu as kw >>> matrix = -np.array([[-4., 8., 0., 8.], ... [-0., -4., 0., 8.], ... [-0., -0., -0., 8.], ... [-0., -0., -0., -8.]]) >>> _ising_mat, _ = kw.conversion.qubo_matrix_to_ising_matrix(matrix) >>> _ising_mat 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.]])
- kaiwu.conversion.qubo_model_to_ising_model(qubo_model)#
Convert a QUBO model to a CIM Ising model.
- Args:
qubo_model (QuboModel): The QUBO model.
- Returns:
CimIsing: The resulting CIM Ising model.
- Examples:
>>> import kaiwu as kw >>> b1, b2 = kw.core.Binary("b1"), kw.core.Binary("b2") >>> q = b1 + b2 + b1*b2 >>> q_model = kw.qubo.QuboModel(q) >>> ci = kw.conversion.qubo_model_to_ising_model(q_model) >>> print(str(ci)) CIM Ising Details: CIM Ising Matrix: [[-0. -0.125 -0.375] [-0.125 -0. -0.375] [-0.375 -0.375 -0. ]] CIM Ising Bias: 1.25 CIM Ising Variables: b1, b2, __spin__