Skip to content

Commit

Permalink
Fixed ChimeraModel() fails due to missing positional argument 'sparse'
Browse files Browse the repository at this point in the history
…#381  (#433)

## Changes

added sparse arguement where its needed 
## Related issue

- ChimeraModel() fails due to missing positional argument 'sparse' #381 

## Sample code
```
from openjij.model.chimera_model import ChimeraModel
Q={(0, 4): -1, (4, 12): -1}
chimera_model = ChimeraModel(quadratic =Q, unit_num_L=2)
chimera_model.validate_chimera(chimera_model)
```

## Misc
Added dimod types Vartype, SPIN and BINARY as well
  • Loading branch information
j-i-k-o committed Jun 21, 2024
2 parents b405fd2 + 5ac7b78 commit 1ebe26c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
32 changes: 15 additions & 17 deletions openjij/model/chimera_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from dimod import SPIN

from __future__ import annotations
from dimod import SPIN, BINARY, Vartype
import openjij.cxxjij as cj

from openjij.model.model import make_BinaryQuadraticModel


Expand All @@ -28,7 +26,7 @@ def make_ChimeraModel(linear, quadratic):
generated ChimeraModel class
"""

class ChimeraModel(make_BinaryQuadraticModel(linear, quadratic)):
class ChimeraModel(make_BinaryQuadraticModel(linear, quadratic,sparse=True)):
"""Binary quadnratic model dealing with chimera graph This model deal
with chimera graph. ChimeraModel provide methods to verify whether a
Expand All @@ -43,14 +41,14 @@ class ChimeraModel(make_BinaryQuadraticModel(linear, quadratic)):
"""

def __init__(
self,
linear=None,
quadratic=None,
offset=0.0,
vartype=SPIN,
unit_num_L=None,
model=None,
):
self,
linear: dict = {},
quadratic: dict = {},
offset: float = 0.0,
vartype: Vartype = SPIN,
unit_num_L: int = 2,
model = None,
):
if model:
super().__init__(
model.linear, model.quadratic, model.offset, model.vartype
Expand Down Expand Up @@ -316,11 +314,11 @@ def make_ChimeraModel_from_JSON(obj):


def ChimeraModel(
linear: dict = None,
quadratic: dict = None,
linear: dict = {},
quadratic: dict = {},
offset: float = 0.0,
vartype = SPIN,
unit_num_L: int = None,
unit_num_L: int = 2,
model = None,
):
"""Generate ChimeraModel object
Expand Down Expand Up @@ -366,4 +364,4 @@ def ChimeraModel(

ChimeraModel.from_serializable = lambda obj: make_ChimeraModel_from_JSON(
obj
).from_serializable(obj)
).from_serializable(obj)
36 changes: 18 additions & 18 deletions openjij/sampler/csqa_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ class CSQASampler(SQASampler):
"""

def __init__(
self, beta=5.0, gamma=1.0, num_sweeps=1000, schedule=None, num_reads=1
self, beta=5.0, gamma=1.0, num_sweeps=1000, schedule=None, num_reads=1
):

self._default_params = {
"beta": beta,
"gamma": gamma,
"num_sweeps": num_sweeps,
"schedule": schedule,
"num_reads": num_reads,
}
"beta": beta,
"gamma": gamma,
"num_sweeps": num_sweeps,
"schedule": schedule,
"num_reads": num_reads,
}

self._params = {
"beta": beta,
"gamma": gamma,
"num_sweeps": num_sweeps,
"schedule": schedule,
"num_reads": num_reads,
}
"beta": beta,
"gamma": gamma,
"num_sweeps": num_sweeps,
"schedule": schedule,
"num_reads": num_reads,
}

def _get_result(self, system, model):
info = {}
Expand All @@ -62,11 +62,11 @@ def sample_ising(
self,
h,
J,
beta=None,
gamma=None,
num_sweeps=None,
beta:float=5.0,
gamma:float = 1.0,
num_sweeps: int=1000,
schedule=None,
num_reads=None,
num_reads:int=1,
initial_state=None,
updater=None,
reinitialize_state=True,
Expand Down Expand Up @@ -145,7 +145,7 @@ def init_generator():

# choose updater -------------------------------------------
sqa_system = cxxjij.system.make_continuous_time_ising(
init_generator(), ising_graph, self.gamma
init_generator(), ising_graph[0], self._params["gamma"]
)
_updater_name = updater.lower().replace("_", "").replace(" ", "")
if _updater_name == "swendsenwang":
Expand Down
4 changes: 2 additions & 2 deletions openjij/utils/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ def se_success_probability(
sampled_states = response.samples()
se_suc_prob = np.std(
[1 if dict(state) in solutions else 0 for state in sampled_states]
)
)/np.sqrt(len(sampled_states))
else:
sampled_states = response.states
se_suc_prob = np.std(
[1 if list(state) in solutions else 0 for state in sampled_states]
)
)/np.sqrt(len(sampled_states))

return se_suc_prob

Expand Down

0 comments on commit 1ebe26c

Please sign in to comment.