Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

quboassist-0.0.19-cp313-none-win_amd64.whl (266.7 kB view details)

Uploaded CPython 3.13 Windows x86-64

quboassist-0.0.19-cp313-cp313-manylinux_2_34_x86_64.whl (429.7 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.34+ x86-64

quboassist-0.0.19-cp313-cp313-macosx_11_0_arm64.whl (366.3 kB view details)

Uploaded CPython 3.13 macOS 11.0+ ARM64

quboassist-0.0.19-cp312-none-win_amd64.whl (266.9 kB view details)

Uploaded CPython 3.12 Windows x86-64

quboassist-0.0.19-cp312-cp312-manylinux_2_34_x86_64.whl (429.8 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.34+ x86-64

quboassist-0.0.19-cp312-cp312-macosx_11_0_arm64.whl (366.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

quboassist-0.0.19-cp311-none-win_amd64.whl (267.5 kB view details)

Uploaded CPython 3.11 Windows x86-64

quboassist-0.0.19-cp311-cp311-manylinux_2_34_x86_64.whl (429.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.34+ x86-64

quboassist-0.0.19-cp311-cp311-macosx_11_0_arm64.whl (367.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

quboassist-0.0.19-cp310-none-win_amd64.whl (268.0 kB view details)

Uploaded CPython 3.10 Windows x86-64

quboassist-0.0.19-cp310-cp310-manylinux_2_34_x86_64.whl (430.3 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.34+ x86-64

quboassist-0.0.19-cp310-cp310-macosx_11_0_arm64.whl (367.3 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

quboassist-0.0.19-cp39-none-win_amd64.whl (268.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

quboassist-0.0.19-cp39-cp39-manylinux_2_34_x86_64.whl (431.2 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.34+ x86-64

quboassist-0.0.19-cp39-cp39-macosx_11_0_arm64.whl (367.7 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

quboassist-0.0.19-cp38-none-win_amd64.whl (268.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

quboassist-0.0.19-cp38-cp38-manylinux_2_34_x86_64.whl (431.0 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.34+ x86-64

quboassist-0.0.19-cp38-cp38-macosx_11_0_arm64.whl (367.7 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

File details

Details for the file quboassist-0.0.19-cp313-none-win_amd64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp313-none-win_amd64.whl
Algorithm Hash digest
SHA256 9c0512f823b5cd812a151627e1c11e5a60b776b75f7e281c1cde8021a9658b6e
MD5 58ee01afa4882f881475f2605b404c7b
BLAKE2b-256 5d9facab2d1601fe4879c46844f5b70916337faf0cc7efa195233e90be73c552

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 66c3c3c7cc63ee235392ee151c8f4102bf8b11b05c23e088be0c1c8728a1eee5
MD5 51c1fed62d1ab721ec19b77d9f24e9b5
BLAKE2b-256 f0c7f7e53756ea9c23298a84d441ae800bb5fca9f20b72b75e6b9aaea9a4f14a

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0e5b41069b071046370387673bdce3f0ebda88bd474e99af3fe94655174f17b8
MD5 c19cce27b090fe57bac8c48a31fbc551
BLAKE2b-256 079b29259db79672cad2008c24686c97be6e03b9e442089cafe5e2e1e854efc2

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 b6b817d24987234f99b0069c7f5606add52199685afc5854282e9030813ede8b
MD5 348e7c71e1adf6f550631215f31f521d
BLAKE2b-256 ae978bdd4e394a8d5f79ff08255ab1e4365431e50c0884900166999ca4a20481

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 44ed77a1138830316adf72811bb38e925353793376c8b03d1e1c9aff8ea4dbd4
MD5 894b39400fdd252dea9975ba9ef85abf
BLAKE2b-256 de990a90e4460f032e6608d970e998b7e0efee1ca9f04beff4621d7dee1899bc

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 95454bf677852f7cfff121d5a141c3d9d05bd0835d47b5666ef03a4bb2cd8fa2
MD5 8dfcb2bae5782ed890e2c7da825dea10
BLAKE2b-256 73d5cf6e7248b9e6a403dd9a4b80c890d575f07e61456cc962eb2b02d7181f7c

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 d235dead29256c1fdd964652a6a8fd28f7c6e892b0f597b568154ff63b873f62
MD5 19ec977e25359746f07fb471b3f27244
BLAKE2b-256 475ba28f05cb8e3325c1778422f1b9b73d5b6e3f73081e8d95ef55e8351969fa

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a02b61ede11f478ac58fdffeea44da64a3ed3ab00b0a57351a7c0c56f2ce3d6f
MD5 1bfed043604ac0d06526d8aea6a46a57
BLAKE2b-256 1191390532baa25f1e594fee7e17271b298853e26ebcacb07daf892076c5db70

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4a834d169059f33419dadce86dbd6aaf5cf5e17ea21094365f5e37b59be8eaea
MD5 2a518ef94fb32fcbec4d1c7beb257730
BLAKE2b-256 3bfa3ac0c79549ed06e91ab2727c0cafad3f2996c6f1ffbb6d26c04ca0753ccf

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 88b84af387cc4dbcb0110d2d3314b85e6598f71486f014a36b1b7d69c9616243
MD5 2ceb82994db6f0b6b8f21446935c60ea
BLAKE2b-256 39e2a06471a552b9860c93e462eae724380ad23bbe495bc671daae0770d277da

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d1a436510aead022434b4fea4a79819e278d43c79885abdb42338c8c12b901ca
MD5 04a07947d8c127aa04e7e14dfe89bb7f
BLAKE2b-256 e26b0abed112dcc90935c1b6fafc8aabf8b8a608d2e71c1b58bcbb29faa34118

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 54e57408c0b7d954b10e33373f37c54a30a2a5cde24fe913c9bd65fb04b847a9
MD5 a42e7d21bf7606ceeacf7974772c6ae0
BLAKE2b-256 fca3480aec19ad4b0d8bcf6ca3e6dfa17fe0e6b788040faf5741984f4565976a

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 c76eb27d78d88059cf921fa1cb0e1de622eda33a72376b9132f8a765f7a33aa7
MD5 e9ccb29c2ccee0323023559922a7e669
BLAKE2b-256 cb64daa8ade8ac50513783327241e1c025629a081825ffe03b3b40ba275b429b

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c02bb6f727c017bb61cd068de0703fa0b6c84edafcf267a1d89ce7997c4dc5f5
MD5 4f44ec267bf7e8c0951326524d674f45
BLAKE2b-256 860cc59218aaccb5c4bb8b4a7f5312920cce4f9d7039fd4b5afeda7c1653f2c5

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d0b50ecc7fbf57905bdca5ba3ba646bc1ff04bbe30894e83f38bb388f30f2c15
MD5 61874a2bd422f5fee21abc30f83c1beb
BLAKE2b-256 0d07383fae513ec40f3651fda9d3e1785c5f45a966cb46d4b47a35ed03adff40

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 9bb2543c6785bf89bac74be6002c7f0df7b814b041ca961c3149007a8b449747
MD5 3cfb9fbe93642ea78a074ff75ef079f1
BLAKE2b-256 844f613849ea55697ac42b0345b5071b09549caef4b97dfd27044a3df539791d

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp38-cp38-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a02a192c6d68cf67672ae846ae3c35083bf82549e65e5473de893f3a6fc2cf1b
MD5 cb0d304bb506bde6cdafd73e158dcc4c
BLAKE2b-256 28eb09b1d0f26ee970fd281b03770f840daee6c35861ad1bc002daf1b26eb520

See more details on using hashes here.

File details

Details for the file quboassist-0.0.19-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quboassist-0.0.19-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d44d562ae9bc5676c50757728775a52a4c51eab9fe08c5163066a386f9a14115
MD5 d3f44c6f8424f95e8da9d448e7e4ab4c
BLAKE2b-256 ee6b53620b127ae159436f11dd5daa090a8c8c5796df31dd6a396f4d9df46b62

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page