Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import openjij
from openjij.model import BinaryQuadraticModel
import cxxjij as cj
class ChimeraModel(BinaryQuadraticModel):
"""Binary quadnratic model dealing with chimera graph
This model deal with chimera graph.
ChimeraModel provide methods to verify whether a given interaction graph matches a Chimera graph and to convert it to cxxjij.graph.Chimera.
Examples:
# This interactions satisfy chimera topology.
>>> Q={(0, 4): -1, (4, 12): -1}
>>> chimera_model = ChimeraModel(Q, unit_num_L=2) # make
>>> chimera_self.validate_chimera()
"""
def __init__(self, linear=None, quadratic=None,
offset=0.0, var_type=openjij.SPIN,
unit_num_L=None, model=None,
gpu=False):
self.gpu = gpu
def sampling(self, model,
initial_state=None,
reinitialize_state=True, seed=None,
**kwargs):
# Check the system for GPU is compiled
try:
self.system_class = cxxjij.system.ChimeraClassicalGPU
except AttributeError:
raise AttributeError(
'Does the computer you are running have a GPU? Compilation for the GPU has not been done. Please reinstall or compile.')
# convert to ChimeraModel from normal BQM
if isinstance(model, BinaryQuadraticModel):
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_model = ChimeraModel(
model=model, unit_num_L=self.unit_num_L, gpu=True)
else:
chimera_model = model
if chimera_model.unit_num_L % 2 != 0:
raise ValueError('unit_num_L should be even number.')
self.unit_num_L = chimera_model.unit_num_L
self._set_model(chimera_model)
def _make_dense_graph(self, h=None, J=None, Q=None, var_type='SPIN'):
if var_type == 'BINARY':
if Q is None:
raise ValueError('Input QUBO matrix: Q')
model = BinaryQuadraticModel(Q=Q, var_type='BINARY')
elif var_type == 'SPIN':
if h is None or J is None:
raise ValueError('Input h and J')
model = BinaryQuadraticModel(h=h, J=J, var_type='SPIN')
self.indices = model.indices
self.N = len(model.indices)
self.energy_bias = model.energy_bias
self.var_type = model.var_type
dense_graph = model.convert_to_dense_graph()
self.model = model
return dense_graph
def _make_dense_graph(self, h=None, J=None, Q=None, spin_type='ising'):
if spin_type=='qubo':
if Q is None:
raise ValueError('Input QUBO matrix: Q')
model = BinaryQuadraticModel(Q=Q, spin_type='qubo')
elif spin_type=='ising':
if h is None or J is None:
raise ValueError('Input h and J')
model = BinaryQuadraticModel(h=h, J=J, spin_type='ising')
self.indices = model.indices
self.N = len(model.indices)
self.energy_bias = model.energy_bias
self.spin_type = model.spin_type
dense_graph = model.convert_to_dense_graph()
return dense_graph
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from openjij.model import BinaryQuadraticModel
class ChimeraModel(BinaryQuadraticModel):
def __init__(self, h=None, J=None, Q=None, unit_num_L=None, model=None, var_type='SPIN'):
if model:
self.linear = model.linear
self.quad = model.quad
self.indices = model.indices
self.energy_bias = model.energy_bias
self.var_type = model.var_type
self._interaction_matrix = None
else:
super().__init__(h=h, J=J, Q=Q, var_type=var_type)
if not unit_num_L:
raise ValueError('Input unit_num_L which is the length of the side of the two-dimensional grid where chimera unit cells are arranged.')
self.unit_num_L = unit_num_L
self.coordinate = self._validate_indices(self.indices)
def _make_dense_graph(self, h=None, J=None, Q=None, var_type='SPIN'):
if var_type == 'BINARY':
if Q is None:
raise ValueError('Input QUBO matrix: Q')
model = BinaryQuadraticModel(Q=Q, var_type='BINARY')
elif var_type == 'SPIN':
if h is None or J is None:
raise ValueError('Input h and J')
model = BinaryQuadraticModel(h=h, J=J, var_type='SPIN')
self.indices = model.indices
self.N = len(model.indices)
self.energy_bias = model.energy_bias
self.var_type = model.var_type
dense_graph = model.convert_to_dense_graph()
self.model = model
return dense_graph
def _make_dense_graph(self, h=None, J=None, Q=None, spin_type='ising'):
if spin_type=='qubo':
if Q is None:
raise ValueError('Input QUBO matrix: Q')
model = BinaryQuadraticModel(Q=Q, spin_type='qubo')
elif spin_type=='ising':
if h is None or J is None:
raise ValueError('Input h and J')
model = BinaryQuadraticModel(h=h, J=J, spin_type='ising')
self.indices = model.indices
self.N = len(model.indices)
self.energy_bias = model.energy_bias
self.spin_type = model.spin_type
dense_graph = model.convert_to_dense_graph()
return dense_graph
def sampling(self, model,
initial_state=None,
reinitialize_state=True, seed=None,
**kwargs):
# Check the system for GPU is compiled
try:
self.system_class = cxxjij.system.ChimeraTransverseGPU
except AttributeError:
raise AttributeError(
'Does the computer you are running have a GPU? Compilation for the GPU has not been done. Please reinstall or compile.')
# convert to ChimeraModel from normal BQM
if isinstance(model, BinaryQuadraticModel):
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 GPUSQASampler.')
chimera_model = ChimeraModel(
model=model, unit_num_L=self.unit_num_L, gpu=True)
else:
chimera_model = model
if chimera_model.unit_num_L % 2 != 0:
raise ValueError('unit_num_L should be even number.')
self.unit_num_L = chimera_model.unit_num_L
self._set_model(chimera_model)