Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bqm_constructor(self):
# Test BinaryQuadraticModel constructor
bqm = oj.BinaryQuadraticModel(self.h, self.J)
self.assertEqual(type(bqm.ising_interactions()), np.ndarray)
self.assertEqual(bqm.var_type, oj.SPIN)
dense_graph = bqm.get_cxxjij_ising_graph(sparse=False)
self.assertTrue(isinstance(dense_graph, cj.graph.Dense))
bqm_qubo = oj.BinaryQuadraticModel.from_qubo(self.Q)
self.assertEqual(bqm_qubo.var_type, oj.BINARY)
def test_cast(self):
spin_var = oj.cast_var_type('SPIN')
self.assertEqual(spin_var, oj.SPIN)
binary_var = oj.cast_var_type('BINARY')
self.assertEqual(binary_var, oj.BINARY)
H = q ^ T Q q
= 1/4 (1+s) ^ T Q(1+s)
= s ^ T Q/4 s + 1 ^ T Q/4 s + s ^ T Q/4 1 + 1 ^ T Q/4 1
= s ^ T nondiag(Q)/4 s + 1 ^ T Q/4 s + s ^ T Q/4 1 + 1 ^ T(Q + diag(Q))/4 1
= \sum_{i < j} Q_{ij}/4 s_i s_j
+ \sum{i <= j}(Q_{ij} + Q_{ji}) s_i
+ sum_{i <= j}(Q_{ij} + Q_{ii})/4
Therefore
J_{ij} = Q_{ij}/4
h_i = sum{i <= j}(Q_{ij} + Q_{ji})/4
constant_term = sum_{i <= j} Q_{ij}/4 + Tr(Q)(energy bias)
"""
if self.var_type == openjij.SPIN:
return self.interactions()
interactions = self.interactions()/4
if self.var_type == openjij.BINARY:
# convert to the Ising interaction
self.energy_bias = (
np.sum(np.triu(interactions)) + np.trace(interactions))
for i in range(len(interactions)):
interactions[i, i] = np.sum(
interactions[i, :]) + interactions[i, i]
return interactions
def calc_energy(self, state, need_to_convert_from_spin=False):
"""calculate energy from state
Args:
state(list, numpy.array): BINARY or SPIN state
need_to_convet_to_spin(bool): if state is SPIN and need
to convert to BINARY from SPIN
"""
_state = np.array(state)
if need_to_convert_from_spin and self.var_type == openjij.BINARY:
_state = (1+_state)/2
if self.var_type == openjij.BINARY:
int_mat = np.triu(self.interactions())
return np.dot(_state, np.dot(int_mat, _state))
elif self.var_type == openjij.SPIN:
int_mat = self.ising_interactions()
linear_term = np.diag(int_mat)
energy = (np.dot(_state, np.dot(int_mat, _state)) -
np.sum(linear_term))/2
energy += np.dot(linear_term, _state)
return energy
def _dict_to_model(self, var_type, h=None, J=None, Q=None, **kwargs):
if 'unit_num_L' in kwargs:
self.unit_num_L = kwargs['unit_num_L']
elif not self.unit_num_L:
raise ValueError(
'Input "unit_num_L" to the argument or the constructor of GPUSASampler.')
chimera = openjij.ChimeraModel(h=None, J=None, Q={(
(0, 0, 1), (0, 0, 4)): -1}, var_type=openjij.BINARY, unit_num_L=2, gpu=True)
return chimera
Examples:
This example submits a two-variable QUBO model.
>>> import openjij as oj
>>> sampler = oj.SQASampler()
>>> Q = {(0, 0): -1, (4, 4): -1, (0, 4): 2}
>>> response = sampler.sample_qubo(Q)
>>> for sample in response.samples(): # doctest: +SKIP
... print(sample)
...
{0: 0, 4: 1}
"""
var_type = openjij.BINARY
bqm = openjij.BinaryQuadraticModel(Q=Q, var_type=var_type)
return self.sampling(bqm,
initial_state=initial_state, updater=updater,
reinitilize_state=reinitilize_state,
seed=seed,
**kwargs
)
Examples:
This example submits a two-variable QUBO model.
>>> import openjij as oj
>>> sampler = oj.SQASampler()
>>> Q = {(0, 0): -1, (4, 4): -1, (0, 4): 2}
>>> response = sampler.sample_qubo(Q)
>>> for sample in response.samples(): # doctest: +SKIP
... print(sample)
...
{0: 0, 4: 1}
"""
model = self._dict_to_model(
var_type=openjij.BINARY, Q=Q, **kwargs)
return self.sampling(model,
initial_state=initial_state, updater=updater,
reinitilize_state=reinitilize_state,
seed=seed,
**kwargs
)