Generate QUBO which can be input to dwave-neal simulated annealing solver and reconstruct the solution of the original problem.
Project description
quboassist
This is a package to generate QUBO which can be input to dwave-neal simulated annealing solver and reconstruct the solution of the original problem from the output.
What problems is it applicable to?
This package converts the problem below into QUBO form which can be input directly to dwave-neal package:
$$ {\rm minimize}\ \ \ f(x) =x^t Ax\ $$
$$ s.t. \ \ \ \forall i, a_i \leq x_i\leq b_i,x_i\in\mathbb Z $$
$$ \forall j, \sum_ kc_{jk} x_k \geq d_j\ \ (c_{jk}, d_j \in \mathbb Z) $$
where $A$ is a symmetric real matrix. I.e. problems where the objective function is quadratic, all variables are bounded and integer, all constraints are linear and their all coefficients are integer.
How to use
First, import the all classes of quboassist.
from quboassist import *
import neal
There are three classes: Variable
, Formula
, Problem
. Using Variable
class, we can define variables.
x = [Variable("x{}".format(i), 2, 5) for i in range(10)]
The fist component of input is the name of the variable. The second is the minimum value and the last is the maximum value, so this variable "x" takes $2,3,4,5$.
Formula
is a class whose instance is generated automatically when variables are operated. For example,
f = - x[0]**2 - 3 * x[1]
g = x[0] > x[1]
then f
, g
are instances of Formula
. Finally, we can define a problem using Problem
.
P = Problem()
P.add_objective(f)
P.add_constraint(g)
Finally, we can get QUBO by compile
method.
qubo = P.compile([10])
sampler = neal.SimulatedAnnealingSampler()
result = sampler.sample_qubo(qubo.todict()).first.sample
solution = P.solution(result, "neal")
print(solution)
where the input of compile
method of Problem
class is the weights vector of the added constraints. The solution is almost always below .
({'x0': 5, 'x1': 4}, [True])
The second component means whether each solution satisfies each constraint conditions. In the above case, because $5 > 4$, the return is true. Note that heuristic algorithms do not necessarily return an exact solution, so we always need to pay attention to the second component.
In general, increasing the weight $w_i$ tends to make it easier to satisfy the condition, but the objective function becomes relatively smaller. Therefore we propose to use a library called optuna to tune these hyperparameters $w_i$.
A sample code is showed below.
import neal
import quboassist
import optuna
import numpy as np
from copy import copy
x = [quboassist.Variable("x{}".format(i), 0, 3) for i in range(10)]
best_solution = []
best_val = np.inf
P = quboassist.Problem()
f = - x[0]**2 - x[1]
P.add_objective(f)
g = x[0] > x[1]
P.add_constraint(g)
h = x[0] + x[1] == x[2]
P.add_constraint(h)
sampler = neal.SimulatedAnnealingSampler()
def objective(trial):
w = [trial.suggest_float("w{}".format(i), 0, 5) for i in range(2)]
qubo = P.compile(w)
result = sampler.sample_qubo(qubo.todict()).first
solution = P.solution(result.sample)
print("\n")
print(solution)
obj = w[0] + w[1] + 10 * sum(np.logical_not(solution[1]))
val = result.energy
# Note that result.energy and the value of objective function may differ by a constant which appears when expanding the product of variables!
global best_solution, best_val
if np.all(solution[1]) and val < best_val:
best_val = val
best_solution = copy(solution)
return obj
study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=20)
print("\n\nBest Solutuion")
print(best_solution)
What kind of technique is used?
The core is as below. We leave it to the reader to consider how to use this to represent any bounded integer variable as a linear combination of a constant and a binary variable, and to convert an inequality into an equality.
Algorithm
Input: $n$
Output: $A(n)$
Define $A_0(n) \leftarrow 2^{\lfloor \log_2 (n + 1) \rfloor - 1}, n_1 \leftarrow n - A_0(n), k \leftarrow 0$
while $n_{k+1} \neq 0$ do
$A_{k + 1}(n) \leftarrow 2^{\lfloor \log_2 (n_{k + 1} + 1) \rfloor - 1}$
$k \leftarrow k + 1$
$n_{k + 1}\leftarrow n_k - A_k(n)$
end while
Lemma
For all $n \in \mathbb N$, the sequence $A(n)$ is finite and the length is at most
$$ 2\lfloor\log_2(n +1)\rfloor. $$
Moreover, a function $f: [0,1]^k \rightarrow \mathbb N$ defined as:
$$ f(x_1,x_2,...,x_k):= \sum_{i =1}^k A_i(n) x_i $$
takes the all numbers $0,...,n$ and no other values.
Proof.
Since $A_0(n)$ is the only power of two which satisfies $4A_0(n)> n+1 \geq 2 A_0(n)$,
$$ n - A_0(n) \geq A_0(n)-1 $$
i.e.
$$ A_1(n)=2^{\lfloor\log_2(n_1 +1)\rfloor-1}\geq \frac{1}{2}A_0(n) $$
holds if $n_1 \neq 0$. Therefore if $A_0(n) \geq 2$, then $A_1(n) = A_0(n)$ or $A_1(n) =\frac{1}{2} A_0(n)$. Moreover, in the case that the first two numbers of $A(n)$ is same,
$$ n_2=n-2A_1(n)<2A_1(n)-1 $$
i.e.
$$ A_2(n)=2^{\lfloor\log_2(n_2 +1)\rfloor-1}<A_1(n). $$
Hence the same number appears at most two times in $A(n)$ and the exponent $\lfloor\log_2(n_k +1)\rfloor-1$ is monotonically non-increasing, thus the length of $A(n)$ is at most
$$ 2\lfloor\log_2(n +1)\rfloor, $$
moreover by the same reason, we also conclude the sequence $A(n)$ includes all powers of two less than or equal to
$$ 2^{\lfloor\log_2(n +1)\rfloor-1}(= A_0(n)). $$
Therefore, numbers $0,...,2A_0(n)-1$ can be expressed as
$$ \sum_{i=1}^kA_i(n)x_i $$
and numbers $n-2A_0(n),...,n$ can be expressed as
$$ n-\sum_{i=1}^kA_i(n)y_i=\sum_{i=1}^kA_i(n)(1-y_i). $$
Since
$$ n-2A_0(n)< 2A_0(n) -1, $$
finally all numbers $0,...,n$ can be expressed as
$$ \sum_{i=1}^kA_i(n)x_i. $$
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file quboassist-0.0.18-cp313-none-win_amd64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp313-none-win_amd64.whl
- Upload date:
- Size: 266.7 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3e7dc6bbcb49163aa7bdb56bf050b6a802cdaf7816fd8c5338774ae27175463 |
|
MD5 | 816a7b58857a0abde46fdf48e17f91b2 |
|
BLAKE2b-256 | 8dfc7862e122f5c70a769a18d9c996b8409884e604d4af009bdb73a7d63c9559 |
File details
Details for the file quboassist-0.0.18-cp313-cp313-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp313-cp313-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 429.7 kB
- Tags: CPython 3.13, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b7f5f987b991631dfcdf891c14f1f334e0a6a6fabe371c6bf50bdcb29eb799d |
|
MD5 | 2c60fe49e40ed3fe57b6e3cdda8701b0 |
|
BLAKE2b-256 | 2dc8005f289eacdfc99ec1f7cd13a0c804a3bb706bfaba20a7a71994e9fdf9b8 |
File details
Details for the file quboassist-0.0.18-cp313-cp313-macosx_11_0_arm64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 365.7 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87d1f5ab5821998b457f3e915ccef9f7acd4baec18ca638d0f9e82cb96d6c7e3 |
|
MD5 | 5e1dfadbfabc699972ef8692cf3d6ea3 |
|
BLAKE2b-256 | d865f865840afea524ddc2a3f83e91099f1e37faa1080ed61cf911d9a7214b83 |
File details
Details for the file quboassist-0.0.18-cp312-none-win_amd64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp312-none-win_amd64.whl
- Upload date:
- Size: 266.8 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 648a786c4ed514f6adefc2e7a560d981045ad998d536b78f25f73d3fffb227fa |
|
MD5 | 9f99fcc564fa2ea7ac0ae4a74ba40ead |
|
BLAKE2b-256 | 93f1e09fa96e574dc433bc8a213049f8062d9f4ad9eacac3d89d00b6eb144273 |
File details
Details for the file quboassist-0.0.18-cp312-cp312-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 429.9 kB
- Tags: CPython 3.12, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b910cc89274a673180b29473897360b5b077e11760c9c4e9902e28c048a4996 |
|
MD5 | e693f41758af43ac7d8b42baf9865a55 |
|
BLAKE2b-256 | 441deb6b53280dc7b9645cb57d3e02ca0958fa733ce8ea5992ce1c0ce1b1fc84 |
File details
Details for the file quboassist-0.0.18-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 365.8 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4358793b99edcaf4504bbe8ec3f8c54e15c5748669758ccd05896f54063ed73 |
|
MD5 | ddf44952809626a42019f284e8453395 |
|
BLAKE2b-256 | cf354cd56039b627a5275b407134d12a244b67a9663461492948a24126f8b6eb |
File details
Details for the file quboassist-0.0.18-cp311-none-win_amd64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp311-none-win_amd64.whl
- Upload date:
- Size: 267.4 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5868647367cf6d868af4a9b2b4afba498e9e1eafb1e51443847af81b0a056e1 |
|
MD5 | 3fbb8f7c01bf042119bd50d3fb81d08a |
|
BLAKE2b-256 | 3f5656153444a4b326b863e2e9f3474eb7dbf3e2ac4e0ca867cb4351855e1ad3 |
File details
Details for the file quboassist-0.0.18-cp311-cp311-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp311-cp311-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 429.9 kB
- Tags: CPython 3.11, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 822161a565c6c4d670de38d0b9e280f50670877d58abcfc944ca0691e27d9748 |
|
MD5 | 0e2ac7f6fd73485fa5e9f3462d676197 |
|
BLAKE2b-256 | 1cf2be305f98a3027caf3fec07d83967a2b70d4520fbcc63e996158fd838fce3 |
File details
Details for the file quboassist-0.0.18-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 366.9 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3393aec5593907ebcb7c0c2229ae56854ac5c1f2c0158d371bb484d49ed7e986 |
|
MD5 | b4214360779afad4be5afd2d92b13970 |
|
BLAKE2b-256 | a5f2b370f9e65a4f11a899a44f67d6133b74b005b3ec80a4ac28d6266a3c8fc5 |
File details
Details for the file quboassist-0.0.18-cp310-none-win_amd64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp310-none-win_amd64.whl
- Upload date:
- Size: 267.9 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fc58f52870d68834ed534630af5f0e20526ffc1ce1fb82940d0f7d668324a5f |
|
MD5 | 2dbeb079b8bd2d13e05d402dd39d3b9c |
|
BLAKE2b-256 | 3d6139fab29fdeaad10ea35c901f1e1fb332913c6c71c274c1927cb993be838d |
File details
Details for the file quboassist-0.0.18-cp310-cp310-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp310-cp310-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 430.4 kB
- Tags: CPython 3.10, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ec40787efa286dc2747b5ac666d3e9a0ef456664bf7ab6db852ca0568c33435 |
|
MD5 | 9e96ad3454d03db5d8ddb4da8a0e1e92 |
|
BLAKE2b-256 | 25644c02e37f3fdacfbd4bd0fb30df375260f013cd25e5e8909819e38f226847 |
File details
Details for the file quboassist-0.0.18-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 365.9 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8076cfe8f5873519547c5bf67bbe98b6b1ad6da806ec03ffa1cda6507c9164ac |
|
MD5 | b253d82009b05c6141d108ded4996a44 |
|
BLAKE2b-256 | 0387ecc87de5c63b94e344870531b5ca389efc604927a2580cfa1f0492966146 |
File details
Details for the file quboassist-0.0.18-cp39-none-win_amd64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp39-none-win_amd64.whl
- Upload date:
- Size: 268.0 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d62a04401b98678daec6367723140fc544b8da126b6391d893b4023ee790d175 |
|
MD5 | 6fb8b1506f0e8a7b42e78751d34a0613 |
|
BLAKE2b-256 | ba3bc823b0f66095caa86223a2fb34027611e9856a9ac4bfbe7fbd43bcf68607 |
File details
Details for the file quboassist-0.0.18-cp39-cp39-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp39-cp39-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 431.2 kB
- Tags: CPython 3.9, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5decd6250268cc8aae2d33c7fdc17c7dc3f1ee4ea905c87b218ca5f33fe5012f |
|
MD5 | 37934248fb8bae7f3abf4485453873a0 |
|
BLAKE2b-256 | e86a32514537513a944f07409fb373c76056123c516375cfcf820e0cd6bb04d4 |
File details
Details for the file quboassist-0.0.18-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 366.3 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dde65dd9bd4fc65dae942e041fc6e2c76e5221dec59efda03ba7b33fb9f78bd2 |
|
MD5 | 961335f64a820189614f8e56e9992fa1 |
|
BLAKE2b-256 | fa3f0458d082484e05904b0d30099f606eccd3e390b47c86413de51f65dead33 |
File details
Details for the file quboassist-0.0.18-cp38-none-win_amd64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp38-none-win_amd64.whl
- Upload date:
- Size: 268.1 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed6c5c69cfaa9394036c644fe75f430a054b0c764233b2c664b6fad77f35fe30 |
|
MD5 | 66786077df2fdf28ec3404bd3b6b5193 |
|
BLAKE2b-256 | 860e3fc7a4afd446c9d68a9145de6d998e5737eb8002d78b15ad06154057834e |
File details
Details for the file quboassist-0.0.18-cp38-cp38-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp38-cp38-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 431.0 kB
- Tags: CPython 3.8, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba631cb9571ff1ab87eff0503dbe30dc08ec24bc1094072ccc2e4091d7872ae6 |
|
MD5 | 343baf2ae79b4fd42bb6b1b7e00876fa |
|
BLAKE2b-256 | 04bd854f14b7eb79c67ea12b46e8e9f411f9af480daa643e46b1ac8700420def |
File details
Details for the file quboassist-0.0.18-cp38-cp38-macosx_11_0_arm64.whl
.
File metadata
- Download URL: quboassist-0.0.18-cp38-cp38-macosx_11_0_arm64.whl
- Upload date:
- Size: 366.3 kB
- Tags: CPython 3.8, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80271aef6c3d5212b199f6c5798ad557323292fd65faee5ebb1cff5d785d08bf |
|
MD5 | 066f181fb55bcc7a95b781e1e57ae8cb |
|
BLAKE2b-256 | 740442c3bd36d8f68086550fb1c8eae06ade8d346e3e6bbd24d4844e4a71d141 |