ommx-openjij-adapter 1.6.0rc2__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.
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 2.1
2
+ Name: ommx_openjij_adapter
3
+ Version: 1.6.0rc2
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,3 @@
1
+ OMMX Adapter for OpenJij
2
+ =========================
3
+
@@ -0,0 +1,37 @@
1
+ from ommx.v1 import Instance, State, Samples
2
+ import openjij as oj
3
+
4
+
5
+ def sample_qubo_sa(instance: Instance, *, num_reads: int = 1) -> Samples:
6
+ """
7
+ Sampling QUBO with Simulated Annealing (SA) by [`openjij.SASampler`](https://openjij.github.io/OpenJij/reference/openjij/index.html#openjij.SASampler)
8
+
9
+ Note that input `instance` must be a QUBO (Quadratic Unconstrained Binary Optimization) problem, i.e.
10
+
11
+ - Every decision variables are binary
12
+ - No constraint
13
+ - Objective function is quadratic
14
+
15
+ 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.
16
+ """
17
+ q, c = instance.as_qubo_format()
18
+ if instance.sense == Instance.MAXIMIZE:
19
+ q = {key: -val for key, val in q.items()}
20
+ sampler = oj.SASampler()
21
+ response = sampler.sample_qubo(q, num_reads=num_reads) # type: ignore
22
+
23
+ # Filling into ommx.v1.Samples
24
+ # Since OpenJij does not issue the sample ID, we need to generate it in the responsibility of this OMMX Adapter
25
+ sample_id = 0
26
+ entries = []
27
+ for i in range(num_reads):
28
+ sample = response.record.sample[i]
29
+ state = State(entries=zip(response.variables, sample)) # type: ignore
30
+ # `num_occurrences` is encoded into sample ID list.
31
+ # For example, if `num_occurrences` is 2, there are two samples with the same state, thus two sample IDs are generated.
32
+ ids = []
33
+ for _ in range(response.record.num_occurrences[i]):
34
+ ids.append(sample_id)
35
+ sample_id += 1
36
+ entries.append(Samples.SamplesEntry(state=state, ids=ids))
37
+ return Samples(entries=entries)
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 2.1
2
+ Name: ommx_openjij_adapter
3
+ Version: 1.6.0rc2
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,4 @@
1
+ ommx<2.0.0,>=1.4.0
2
+ openjij>=0.9.2
3
+
4
+ [dev]
@@ -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.0rc2"
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,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,36 @@
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
+ samples = adapter.sample_qubo_sa(instance, num_reads=1)
33
+ sample_set = instance.evaluate_samples(samples)
34
+
35
+ # x0 = x1 = 1 is maximum
36
+ assert sample_set.extract_decision_variables("x", 0) == {(0,): 1.0, (1,): 1.0}