# 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, value_range)[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=0$$.

>>> from pyqubo import UnaryEncInteger
>>> import dimod
>>> a = UnaryEncInteger("a", (0, 3))
>>> b = UnaryEncInteger("b", (0, 3))
>>> M=2.0
>>> H = (2*a-b)**2 + M*(a+b-3)**2
>>> model = H.compile()
>>> bqm = model.to_bqm()
>>> import dimod
>>> sampleset = dimod.ExactSolver().sample(bqm)
>>> decoded_samples = model.decode_sampleset(sampleset)
>>> best_sample = min(decoded_samples, key=lambda s: s.energy)
>>> print(best_sample.subh['a'])
1.0
>>> print(best_sample.subh['b'])
2.0


## LogEncInteger¶

class LogEncInteger(label, value_range)[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()
>>> bqm = model.to_bqm()
>>> import dimod
>>> sampleset = dimod.ExactSolver().sample(bqm)
>>> decoded_samples = model.decode_sampleset(sampleset)
>>> best_sample = min(decoded_samples, key=lambda s: s.energy)
>>> print(best_sample.subh['a'])
2.0
>>> print(best_sample.subh['b'])
3.0


## OneHotEncInteger¶

class OneHotEncInteger(label, value_range, strength)[source]

One-hot encoded integer. The value that takes $$[1, 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()
>>> bqm = model.to_bqm()
>>> import dimod
>>> sampleset = dimod.ExactSolver().sample(bqm)
>>> decoded_samples = model.decode_sampleset(sampleset)
>>> best_sample = min(decoded_samples, key=lambda s: s.energy)
>>> print(best_sample.subh['a'])
2.0

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. Express

## OrderEncInteger¶

class OrderEncInteger(label, value_range, 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
>>> a = OrderEncInteger("a", (0, 3), strength = 5.0)
>>> model = ((a-2)**2).compile()
>>> bqm = model.to_bqm()
>>> import dimod
>>> sampleset = dimod.ExactSolver().sample(bqm)
>>> decoded_samples = model.decode_sampleset(sampleset)
>>> best_sample = min(decoded_samples, key=lambda s: s.energy)
>>> print(best_sample.subh['a'])
2.0

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. 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
>>> 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()
>>> bqm = model.to_bqm()
>>> import dimod
>>> sampleset = dimod.ExactSolver().sample(bqm)
>>> decoded_samples = model.decode_sampleset(sampleset)
>>> best_sample = min(decoded_samples, key=lambda s: s.energy)
>>> print(best_sample.subh['a'])
2.0
>>> print(best_sample.subh['b'])
2.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. 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
>>> 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()
>>> bqm = model.to_bqm()
>>> import dimod
>>> sampleset = dimod.ExactSolver().sample(bqm)
>>> decoded_samples = model.decode_sampleset(sampleset)
>>> best_sample = min(decoded_samples, key=lambda s: s.energy)
>>> print(best_sample.subh['a'])
2.0
>>> print(best_sample.subh['b'])
2.0


References

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