Integer

Summary of each integer encoding, whose value takes [0, n].

Encoding Value Constraint #vars Max abs. coeff of value
UnaryEncInteger \(\sum_{i=1}^{n}x_{i}\) No constraint \(n\) \(1\)
LogEncInteger \(\sum_{i=1}^{d}2^i x_{i}\) No constraint \(\lceil\log_{2}n\rceil(=d)\) \(2^d\)
OneHotEncInteger \(\sum_{i=0}^{n}ix_{i}\) \((\sum_{i=0}^{n}x_{i}-1)^2\) \(n+1\) \(n\)
OrderEncInteger \(\sum_{i=1}^{n}x_{i}\) \(\sum_{i=1}^{n-1}x_{i+1}(1-x_{i})\) \(n\) \(1\)

UnaryEncInteger

class UnaryEncInteger(label, lower, upper)[source]

Unary encoded integer. The value that takes \([0, n]\) is represented by \(\sum_{i=1}^{n}x_{i}\) without any constraint.

Parameters:
  • label (str) – Label of the integer.
  • lower (int) – Lower value of the integer.
  • upper (int) – Upper value of the integer.

Examples

This example finds the value a, b such that \(a+b=3\) and \(2a-b=1\).

>>> from pyqubo import UnaryEncInteger
>>> import dimod
>>> a = UnaryEncInteger("a", 0, 3)
>>> b = UnaryEncInteger("b", 0, 3)
>>> M=2.0
>>> H = (2*a-b-1)**2 + M*(a+b-3)**2
>>> model = H.compile()
>>> q, offset = model.to_qubo()
>>> sampleset = dimod.ExactSolver().sample_qubo(q)
>>> response, broken, e  = model.decode_dimod_response(sampleset, topk=1)[0]
>>> print("a={},b={}".format(sum(response["a"].values()), sum(response["b"].values())))
a=1,b=2

LogEncInteger

class LogEncInteger(label, lower, upper)[source]

Log encoded integer. The value that takes \([0, n]\) is represented by \(\sum_{i=1}^{\lceil\log_{2}n\rceil}2^ix_{i}\) without any constraint.

Parameters:
  • label (str) – Label of the integer.
  • lower (int) – Lower value of the integer.
  • upper (int) – Upper value of the integer.

Examples

This example finds the value a, b such that \(a+b=5\) and \(2a-b=1\).

>>> from pyqubo import LogEncInteger
>>> import dimod
>>> a = LogEncInteger("a", 0, 4)
>>> b = LogEncInteger("b", 0, 4)
>>> M=2.0
>>> H = (2*a-b-1)**2 + M*(a+b-5)**2
>>> model = H.compile()
>>> q, offset = model.to_qubo()
>>> sampleset = dimod.ExactSolver().sample_qubo(q)
>>> response, broken, e  = model.decode_dimod_response(sampleset, topk=1)[0]
>>> sol_a = sum(2**k * v for k, v in response["a"].items())
>>> sol_b = sum(2**k * v for k, v in response["b"].items())
>>> print("a={},b={}".format(sol_a, sol_b))
a=2,b=3

OneHotEncInteger

class OneHotEncInteger(label, lower, upper, strength)[source]

One-hot encoded integer. The value that takes \([0, n]\) is represented by \(\sum_{i=1}^{n}ix_{i}\). Also we have the penalty function \(strength \times (\sum_{i=1}^{n}x_{i}-1)^2\) in the Hamiltonian.

Parameters:
  • label (str) – Label of the integer.
  • lower (int) – Lower value of the integer.
  • upper (int) – Upper value of the integer.
  • strength (float/Placeholder) – Strength of the constraint.

Examples

This example is equivalent to the following Hamiltonian.

\[H = \left(\left(\sum_{i=1}^{3}ia_{i}+1\right) - 2\right)^2 + strength \times \left(\sum_{i=1}^{3}a_{i}-1\right)^2\]
>>> from pyqubo import OneHotEncInteger
>>> a = OneHotEncInteger("a", 1, 3, strength=5)
>>> H = (a-2)**2
>>> model = H.compile()
>>> q, offset = model.to_qubo()
>>> sampleset = dimod.ExactSolver().sample_qubo(q)
>>> solution, broken, e  = model.decode_dimod_response(sampleset, topk=1)[0]
>>> print("a={}".format(1+sum(k*v for k, v in solution["a"].items())))
a=2
equal_to(k)[source]

Variable representing whether the value is equal to k.

Note

You cannot use this method alone. You should use this variable with the entire integer.

Parameters:k (int) – Integer value.
Returns:Express

OrderEncInteger

class OrderEncInteger(label, lower, upper, strength)[source]

Order encoded integer. This encoding is useful when you want to know whether the integer is more than k or not. The value that takes \([0, n]\) is represented by \(\sum_{i=1}^{n}x_{i}\). Also we have the penalty function \(strength \times \left(\sum_{i=1}^{n-1} \left(x_{i+1}-x_{i}x_{i+1}\right)\right)\) in the Hamiltonian. See the reference [TaTK09] for more details.

Parameters:
  • label (str) – Label of the integer.
  • lower (int) – Lower value of the integer.
  • upper (int) – Upper value of the integer.
  • strength (float/Placeholder) – Strength of the constraint.

Examples

Create an order encoded integer a that takes [0, 3] with the strength = 5.0. Solution of a represents 2 which is the optimal solution of the Hamiltonian.

>>> from pyqubo import OrderEncInteger
>>> import dimod
>>> a = OrderEncInteger("a", 0, 3, strength = 5.0)
>>> model = ((a-2)**2).compile()
>>> q, offset = model.to_qubo()
>>> response = dimod.ExactSolver().sample_qubo(q)
>>> solution, broken, e  = model.decode_dimod_response(response, topk=1)[0]
>>> print("a={}".format(sum(solution["a"].values())))
a=2
less_than(k)[source]

Binary variable that represents whether the value is less than k.

Note

You cannot use this method alone. You should use this variable with the entire integer. See an example below.

Parameters:k (int) – Integer value.
Returns:Express

Examples

This example finds the value of integer a and b such that \(a=b\) and \(a>1\) and \(b<3\). The obtained solution is \(a=b=2\).

>>> from pyqubo import OrderEncInteger
>>> import dimod
>>> a = OrderEncInteger("a", 0, 4, strength = 5.0)
>>> b = OrderEncInteger("b", 0, 4, strength = 5.0)
>>> model = ((a-b)**2 + (1-a.more_than(1))**2 + (1-b.less_than(3))**2).compile()
>>> q, offset = model.to_qubo()
>>> sampleset = dimod.ExactSolver().sample_qubo(q)
>>> solution, broken, e  = model.decode_dimod_response(sampleset, topk=1)[0]
>>> solution
{'a': {0: 1, 1: 1, 2: 0, 3: 0}, 'b': {0: 1, 1: 1, 2: 0, 3: 0}}
more_than(k)[source]

Binary variable that represents whether the value is more than k.

Note

You cannot use this method alone. You should use this variable with the entire integer. See an example below.

Parameters:k (int) – Integer value.
Returns:Express

Examples

This example finds the value of integer a and b such that \(a=b\) and \(a>1\) and \(b<3\). The obtained solution is \(a=b=2\).

>>> from pyqubo import OrderEncInteger
>>> import dimod
>>> a = OrderEncInteger("a", 0, 4, strength = 5.0)
>>> b = OrderEncInteger("b", 0, 4, strength = 5.0)
>>> model = ((a-b)**2 + (1-a.more_than(1))**2 + (1-b.less_than(3))**2).compile()
>>> q, offset = model.to_qubo()
>>> sampleset = dimod.ExactSolver().sample_qubo(q)
>>> solution, broken, e  = model.decode_dimod_response(sampleset, topk=1)[0]
>>> solution
{'a': {0: 1, 1: 1, 2: 0, 3: 0}, 'b': {0: 1, 1: 1, 2: 0, 3: 0}}

References

[TaTK09]Tamura, N., Taga, A., Kitagawa, S., & Banbara, M. (2009). Compiling finite linear CSP into SAT. Constraints, 14(2), 254-272.