ommx-openjij-adapter 1.6.0rc2__tar.gz → 1.6.1__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.0rc2 → ommx_openjij_adapter-1.6.1}/PKG-INFO +2 -2
- ommx_openjij_adapter-1.6.1/ommx_openjij_adapter/__init__.py +87 -0
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/ommx_openjij_adapter.egg-info/PKG-INFO +2 -2
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/pyproject.toml +1 -1
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/tests/test_sample.py +1 -0
- ommx_openjij_adapter-1.6.0rc2/ommx_openjij_adapter/__init__.py +0 -37
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/README.md +0 -0
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/ommx_openjij_adapter.egg-info/SOURCES.txt +0 -0
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/ommx_openjij_adapter.egg-info/dependency_links.txt +0 -0
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/ommx_openjij_adapter.egg-info/requires.txt +0 -0
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/ommx_openjij_adapter.egg-info/top_level.txt +0 -0
- {ommx_openjij_adapter-1.6.0rc2 → ommx_openjij_adapter-1.6.1}/setup.cfg +0 -0
@@ -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)
|
@@ -1,37 +0,0 @@
|
|
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)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|