ommx-openjij-adapter 1.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ommx_openjij_adapter-1.6.0/PKG-INFO +23 -0
- ommx_openjij_adapter-1.6.0/README.md +3 -0
- ommx_openjij_adapter-1.6.0/ommx_openjij_adapter/__init__.py +87 -0
- ommx_openjij_adapter-1.6.0/ommx_openjij_adapter.egg-info/PKG-INFO +23 -0
- ommx_openjij_adapter-1.6.0/ommx_openjij_adapter.egg-info/SOURCES.txt +9 -0
- ommx_openjij_adapter-1.6.0/ommx_openjij_adapter.egg-info/dependency_links.txt +1 -0
- ommx_openjij_adapter-1.6.0/ommx_openjij_adapter.egg-info/requires.txt +4 -0
- ommx_openjij_adapter-1.6.0/ommx_openjij_adapter.egg-info/top_level.txt +1 -0
- ommx_openjij_adapter-1.6.0/pyproject.toml +30 -0
- ommx_openjij_adapter-1.6.0/setup.cfg +4 -0
- ommx_openjij_adapter-1.6.0/tests/test_sample.py +37 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: ommx_openjij_adapter
|
3
|
+
Version: 1.6.0
|
4
|
+
Summary: OMMX Adapter for OpenJij.
|
5
|
+
Author-email: "Jij Inc." <info@j-ij.com>
|
6
|
+
Project-URL: Repository, https://github.com/Jij-Inc/ommx
|
7
|
+
Project-URL: Issues, https://github.com/Jij-Inc/ommx/issues
|
8
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
15
|
+
Requires-Python: <3.13,>=3.9
|
16
|
+
Description-Content-Type: text/markdown
|
17
|
+
Requires-Dist: ommx<2.0.0,>=1.4.0
|
18
|
+
Requires-Dist: openjij>=0.9.2
|
19
|
+
Provides-Extra: dev
|
20
|
+
|
21
|
+
OMMX Adapter for OpenJij
|
22
|
+
=========================
|
23
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
from __future__ import annotations
|
2
|
+
|
3
|
+
from ommx.v1 import Instance, State, Samples
|
4
|
+
import openjij as oj
|
5
|
+
|
6
|
+
|
7
|
+
def response_to_samples(response: oj.Response) -> Samples:
|
8
|
+
"""
|
9
|
+
Convert OpenJij's `Response` to `ommx.v1.Samples`
|
10
|
+
"""
|
11
|
+
# Filling into ommx.v1.Samples
|
12
|
+
# Since OpenJij does not issue the sample ID, we need to generate it in the responsibility of this OMMX Adapter
|
13
|
+
sample_id = 0
|
14
|
+
entries = []
|
15
|
+
|
16
|
+
num_reads = len(response.record.num_occurrences)
|
17
|
+
for i in range(num_reads):
|
18
|
+
sample = response.record.sample[i]
|
19
|
+
state = State(entries=zip(response.variables, sample)) # type: ignore
|
20
|
+
# `num_occurrences` is encoded into sample ID list.
|
21
|
+
# For example, if `num_occurrences` is 2, there are two samples with the same state, thus two sample IDs are generated.
|
22
|
+
ids = []
|
23
|
+
for _ in range(response.record.num_occurrences[i]):
|
24
|
+
ids.append(sample_id)
|
25
|
+
sample_id += 1
|
26
|
+
entries.append(Samples.SamplesEntry(state=state, ids=ids))
|
27
|
+
return Samples(entries=entries)
|
28
|
+
|
29
|
+
|
30
|
+
def sample_qubo_sa(
|
31
|
+
instance: Instance,
|
32
|
+
*,
|
33
|
+
beta_min: float | None = None,
|
34
|
+
beta_max: float | None = None,
|
35
|
+
num_sweeps: int | None = None,
|
36
|
+
num_reads: int | None = None,
|
37
|
+
schedule: list | None = None,
|
38
|
+
initial_state: list | dict | None = None,
|
39
|
+
updater: str | None = None,
|
40
|
+
sparse: bool | None = None,
|
41
|
+
reinitialize_state: bool | None = None,
|
42
|
+
seed: int | None = None,
|
43
|
+
) -> Samples:
|
44
|
+
"""
|
45
|
+
Sampling QUBO with Simulated Annealing (SA) by [`openjij.SASampler`](https://openjij.github.io/OpenJij/reference/openjij/index.html#openjij.SASampler)
|
46
|
+
|
47
|
+
The input `instance` must be a QUBO (Quadratic Unconstrained Binary Optimization) problem, i.e.
|
48
|
+
|
49
|
+
- Every decision variables are binary
|
50
|
+
- No constraint
|
51
|
+
- Objective function is quadratic
|
52
|
+
- Minimization problem
|
53
|
+
|
54
|
+
You can convert a problem to QUBO via [`ommx.v1.Instance.penalty_method`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/v1/index.html#ommx.v1.Instance.penalty_method) or other corresponding method.
|
55
|
+
|
56
|
+
:param instance: ommx.v1.Instance representing a QUBO problem
|
57
|
+
:param beta_min: minimal value of inverse temperature
|
58
|
+
:param beta_max: maximum value of inverse temperature
|
59
|
+
:param num_sweeps: number of sweeps
|
60
|
+
:param num_reads: number of reads
|
61
|
+
:param schedule: list of inverse temperature
|
62
|
+
:param initial_state: initial state
|
63
|
+
:param updater: updater algorithm
|
64
|
+
:param sparse: use sparse matrix or not.
|
65
|
+
:param reinitialize_state: if true reinitialize state for each run
|
66
|
+
:param seed: seed for Monte Carlo algorithm
|
67
|
+
|
68
|
+
Note that this is a simple wrapper function for `openjij.SASampler.sample_qubo` method.
|
69
|
+
For more advanced usage, you can use `ommx.v1.Instance.as_qubo_format` to get QUBO matrix,
|
70
|
+
and use OpenJij manually, and convert the `openjij.Response` via `response_to_samples` function.
|
71
|
+
"""
|
72
|
+
q, _offset = instance.as_qubo_format()
|
73
|
+
sampler = oj.SASampler()
|
74
|
+
response = sampler.sample_qubo(
|
75
|
+
q, # type: ignore
|
76
|
+
beta_min=beta_min,
|
77
|
+
beta_max=beta_max,
|
78
|
+
num_sweeps=num_sweeps,
|
79
|
+
num_reads=num_reads,
|
80
|
+
schedule=schedule,
|
81
|
+
initial_state=initial_state,
|
82
|
+
updater=updater,
|
83
|
+
sparse=sparse,
|
84
|
+
reinitialize_state=reinitialize_state,
|
85
|
+
seed=seed,
|
86
|
+
)
|
87
|
+
return response_to_samples(response)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: ommx_openjij_adapter
|
3
|
+
Version: 1.6.0
|
4
|
+
Summary: OMMX Adapter for OpenJij.
|
5
|
+
Author-email: "Jij Inc." <info@j-ij.com>
|
6
|
+
Project-URL: Repository, https://github.com/Jij-Inc/ommx
|
7
|
+
Project-URL: Issues, https://github.com/Jij-Inc/ommx/issues
|
8
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
15
|
+
Requires-Python: <3.13,>=3.9
|
16
|
+
Description-Content-Type: text/markdown
|
17
|
+
Requires-Dist: ommx<2.0.0,>=1.4.0
|
18
|
+
Requires-Dist: openjij>=0.9.2
|
19
|
+
Provides-Extra: dev
|
20
|
+
|
21
|
+
OMMX Adapter for OpenJij
|
22
|
+
=========================
|
23
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
README.md
|
2
|
+
pyproject.toml
|
3
|
+
ommx_openjij_adapter/__init__.py
|
4
|
+
ommx_openjij_adapter.egg-info/PKG-INFO
|
5
|
+
ommx_openjij_adapter.egg-info/SOURCES.txt
|
6
|
+
ommx_openjij_adapter.egg-info/dependency_links.txt
|
7
|
+
ommx_openjij_adapter.egg-info/requires.txt
|
8
|
+
ommx_openjij_adapter.egg-info/top_level.txt
|
9
|
+
tests/test_sample.py
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
ommx_openjij_adapter
|
@@ -0,0 +1,30 @@
|
|
1
|
+
[build-system]
|
2
|
+
requires = ["setuptools>=64", "wheel"]
|
3
|
+
build-backend = "setuptools.build_meta"
|
4
|
+
|
5
|
+
[project]
|
6
|
+
name = "ommx_openjij_adapter"
|
7
|
+
version = "1.6.0"
|
8
|
+
|
9
|
+
description = "OMMX Adapter for OpenJij."
|
10
|
+
authors = [{ name = "Jij Inc.", email = "info@j-ij.com" }]
|
11
|
+
readme = "README.md"
|
12
|
+
|
13
|
+
requires-python = ">=3.9, <3.13"
|
14
|
+
classifiers = [
|
15
|
+
"Programming Language :: Python :: 3 :: Only",
|
16
|
+
"Programming Language :: Python :: 3.9",
|
17
|
+
"Programming Language :: Python :: 3.10",
|
18
|
+
"Programming Language :: Python :: 3.11",
|
19
|
+
"Programming Language :: Python :: 3.12",
|
20
|
+
"License :: OSI Approved :: Apache Software License",
|
21
|
+
"License :: OSI Approved :: MIT License",
|
22
|
+
]
|
23
|
+
dependencies = ["ommx>=1.4.0,<2.0.0", "openjij>=0.9.2"]
|
24
|
+
|
25
|
+
[project.urls]
|
26
|
+
Repository = "https://github.com/Jij-Inc/ommx"
|
27
|
+
Issues = "https://github.com/Jij-Inc/ommx/issues"
|
28
|
+
|
29
|
+
[project.optional-dependencies]
|
30
|
+
dev = []
|
@@ -0,0 +1,37 @@
|
|
1
|
+
from ommx.v1 import Instance, DecisionVariable
|
2
|
+
import ommx_openjij_adapter as adapter
|
3
|
+
|
4
|
+
|
5
|
+
def test_minimize():
|
6
|
+
x0 = DecisionVariable.binary(0, name="x", subscripts=[0])
|
7
|
+
x1 = DecisionVariable.binary(1, name="x", subscripts=[1])
|
8
|
+
|
9
|
+
instance = Instance.from_components(
|
10
|
+
decision_variables=[x0, x1],
|
11
|
+
objective=x0 + x1,
|
12
|
+
constraints=[],
|
13
|
+
sense=Instance.MINIMIZE,
|
14
|
+
)
|
15
|
+
samples = adapter.sample_qubo_sa(instance, num_reads=1)
|
16
|
+
sample_set = instance.evaluate_samples(samples)
|
17
|
+
|
18
|
+
# x0 = x1 = 0 is minimum
|
19
|
+
assert sample_set.extract_decision_variables("x", 0) == {(0,): 0.0, (1,): 0.0}
|
20
|
+
|
21
|
+
|
22
|
+
def test_maximize():
|
23
|
+
x0 = DecisionVariable.binary(0, name="x", subscripts=[0])
|
24
|
+
x1 = DecisionVariable.binary(1, name="x", subscripts=[1])
|
25
|
+
|
26
|
+
instance = Instance.from_components(
|
27
|
+
decision_variables=[x0, x1],
|
28
|
+
objective=x0 + x1,
|
29
|
+
constraints=[],
|
30
|
+
sense=Instance.MAXIMIZE,
|
31
|
+
)
|
32
|
+
instance.as_minimization_problem()
|
33
|
+
samples = adapter.sample_qubo_sa(instance, num_reads=1)
|
34
|
+
sample_set = instance.evaluate_samples(samples)
|
35
|
+
|
36
|
+
# x0 = x1 = 1 is maximum
|
37
|
+
assert sample_set.extract_decision_variables("x", 0) == {(0,): 1.0, (1,): 1.0}
|