Quick Reference: Variables and Expressions
Data types in PyQBPP
PyQBPP uses Python’s native int type for coefficients, energy values, and constants. Since Python integers have unlimited precision, there is no need to specify coeff_t or energy_t as in the C++ version.
Printing objects
All PyQBPP objects can be printed using print() or converted to strings using str():
print(obj)
s = str(obj)
Variable classes
pyqbpp.Var: A class that holds a unique 32-bit integer ID. The variable name can be retrieved viastr(x).
NOTE A
pyqbpp.Varobject represents a variable symbolically. No specific data type is associated with it. It can be used to represent binary, spin, or other types of variables.
Variable creation functions
The following functions are provided to create variables:
-
pyqbpp.var("name"): Creates apyqbpp.Varobject with the given name"name". -
pyqbpp.var("name", s1): Creates a one-dimensional array ofpyqbpp.Varobjects with the base name"name". Each element is represented asname[i]. The resulting type ispyqbpp.Array. -
pyqbpp.var("name", s1, s2): Creates a two-dimensional array (matrix) ofpyqbpp.Varobjects with the base name"name". Each element is represented asname[i][j]. The resulting type is a nestedpyqbpp.Array. -
pyqbpp.var("name", s1, s2, ...): Creates a higher-dimensional array ofpyqbpp.Varobjects with the base name"name". Each element is represented asname[i][j].... The resulting type is a nestedpyqbpp.Array.
NOTE If
"name"is omitted, numbered names such as"{0}","{1}", … are automatically assigned in creation order.
Examples
import pyqbpp as qbpp
x = qbpp.var("x") # Single variable named "x"
y = qbpp.var("y", 3) # Array: y[0], y[1], y[2]
z = qbpp.var("z", 2, 3) # 2x3 matrix: z[0][0], ..., z[1][2]
a = qbpp.var() # Single unnamed variable
b = qbpp.var(5) # Array of 5 unnamed variables
pyqbpp.Var properties and methods
For a pyqbpp.Var instance x, the following are available:
str(x): Returns the name ofxas a string.
Integer variable class
pyqbpp.VarInt: A class derived frompyqbpp.Exprthat represents an integer variable with a specified range.
Integer variable creation functions
The following functions are provided to create integer variables:
-
pyqbpp.var_int("name"): Returns an internally used helper object (pyqbpp.VarIntCore) and does not create apyqbpp.VarIntby itself. To define apyqbpp.VarInt, the range must be specified using thebetween()function, as shown below. -
pyqbpp.between(pyqbpp.var_int("name"), l, u): Here,landumust be integers. This expression creates apyqbpp.VarIntobject with the name"name", which internally contains apyqbpp.Exprobject representing all integers in the range[l, u]. Internally, this also createspyqbpp.Varobjects used in the underlying expression. -
pyqbpp.between(pyqbpp.var_int("name", s1), l, u): Creates a one-dimensional array ofpyqbpp.VarIntobjects with the base name"name"and the same range[l, u]. Each element is represented asname[i]. The resulting type ispyqbpp.Array. Higher-dimensional arrays ofpyqbpp.VarIntobjects can be created in the same way aspyqbpp.Varobjects.
Examples
import pyqbpp as qbpp
x = qbpp.between(qbpp.var_int("x"), 0, 10) # Integer variable x in [0, 10]
y = qbpp.between(qbpp.var_int("y", 3), -5, 5) # Array of 3 integer variables in [-5, 5]
z = qbpp.between(qbpp.var_int("z", 2, 3), 1, 8) # 2x3 matrix of integer variables in [1, 8]
Integer variable properties
For a pyqbpp.VarInt instance x, the following are available:
-
x.min_val(property): Returns the minimum valuelofx. -
x.max_val(property): Returns the maximum valueuofx. -
x.vars(property): Returns the list ofpyqbpp.Varobjects used to represent the integer variable. -
x.coeffs(property): Returns a list of integer coefficients.
The following expression is equivalent to the expression stored in x:
x.min_val + qbpp.sum(x.vars * x.coeffs)
Comparison with C++ QUBO++
| C++ QUBO++ | PyQBPP |
|---|---|
l <= qbpp::var_int("name") <= u | between(var_int("name"), l, u) |
l <= qbpp::var_int("name", s1) <= u | between(var_int("name", s1), l, u) |
x.name() | x.name |
x.str() | str(x) |
x.min_val() | x.min_val (property) |
x.max_val() | x.max_val (property) |
x.vars() | x.vars (property) |
x.coeffs() | x.coeffs (property) |
クイックリファレンス: 変数と式
PyQBPP のデータ型
PyQBPPは係数、エネルギー値、定数にPythonのネイティブな int 型を使用します。 Pythonの整数は精度に制限がないため、C++版のように coeff_t や energy_t を指定する必要はありません。
オブジェクトの表示
すべてのPyQBPPオブジェクトは print() で表示するか、str() で文字列に変換できます。
print(obj)
s = str(obj)
変数クラス
pyqbpp.Var: 一意な32ビット整数IDを保持するクラスです。 変数名はstr(x)で取得できます。
NOTE
pyqbpp.Varオブジェクトは変数をシンボリックに表現します。 特定のデータ型は関連付けられていません。 バイナリ変数、スピン変数、その他の種類の変数を表現するために使用できます。
変数作成関数
変数を作成するために以下の関数が提供されています。
-
pyqbpp.var("name"): 指定された名前"name"を持つpyqbpp.Varオブジェクトを作成します。 -
pyqbpp.var("name", s1): 基本名"name"を持つpyqbpp.Varオブジェクトの1次元配列を作成します。 各要素はname[i]として表されます。 結果の型はpyqbpp.Arrayです。 -
pyqbpp.var("name", s1, s2): 基本名"name"を持つpyqbpp.Varオブジェクトの2次元配列(行列)を作成します。 各要素はname[i][j]として表されます。 結果の型はネストされたpyqbpp.Arrayです。 -
pyqbpp.var("name", s1, s2, ...): 基本名"name"を持つpyqbpp.Varオブジェクトの高次元配列を作成します。 各要素はname[i][j]...として表されます。 結果の型はネストされたpyqbpp.Arrayです。
NOTE
"name"を省略すると、作成順に"{0}"、"{1}"などの番号付き名前が自動的に割り当てられます。
例
import pyqbpp as qbpp
x = qbpp.var("x") # Single variable named "x"
y = qbpp.var("y", 3) # Array: y[0], y[1], y[2]
z = qbpp.var("z", 2, 3) # 2x3 matrix: z[0][0], ..., z[1][2]
a = qbpp.var() # Single unnamed variable
b = qbpp.var(5) # Array of 5 unnamed variables
pyqbpp.Var のプロパティとメソッド
pyqbpp.Var のインスタンス x に対して、以下が利用可能です。
str(x):xの名前を文字列として返します。
整数変数クラス
pyqbpp.VarInt:pyqbpp.Exprから派生したクラスで、指定された範囲を持つ整数変数を表現します。
整数変数作成関数
整数変数を作成するために以下の関数が提供されています。
-
pyqbpp.var_int("name"): 内部的に使用されるヘルパーオブジェクト(pyqbpp.VarIntCore)を返し、それ自体ではpyqbpp.VarIntを作成しません。pyqbpp.VarIntを定義するには、以下に示すようにbetween()関数を使って範囲を指定する必要があります。 -
pyqbpp.between(pyqbpp.var_int("name"), l, u): ここでlとuは整数でなければなりません。 この式は名前"name"を持つpyqbpp.VarIntオブジェクトを作成し、 内部的に範囲[l, u]のすべての整数を表すpyqbpp.Exprオブジェクトを含みます。 内部的に、基礎となる式で使用されるpyqbpp.Varオブジェクトも作成します。 -
pyqbpp.between(pyqbpp.var_int("name", s1), l, u): 基本名"name"と同じ範囲[l, u]を持つpyqbpp.VarIntオブジェクトの1次元配列を作成します。 各要素はname[i]として表されます。 結果の型はpyqbpp.Arrayです。pyqbpp.VarIntオブジェクトの高次元配列は、pyqbpp.Varオブジェクトと同じ方法で作成できます。
例
import pyqbpp as qbpp
x = qbpp.between(qbpp.var_int("x"), 0, 10) # Integer variable x in [0, 10]
y = qbpp.between(qbpp.var_int("y", 3), -5, 5) # Array of 3 integer variables in [-5, 5]
z = qbpp.between(qbpp.var_int("z", 2, 3), 1, 8) # 2x3 matrix of integer variables in [1, 8]
整数変数のプロパティ
pyqbpp.VarInt のインスタンス x に対して、以下が利用可能です。
-
x.min_val(プロパティ):xの最小値lを返します。 -
x.max_val(プロパティ):xの最大値uを返します。 -
x.vars(プロパティ): 整数変数を表現するために使用されるpyqbpp.Varオブジェクトのリストを返します。 -
x.coeffs(プロパティ): 整数係数のリストを返します。
以下の式は x に格納されている式と等価です。
x.min_val + qbpp.sum(x.vars * x.coeffs)
C++ QUBO++ との比較
| C++ QUBO++ | PyQBPP |
|---|---|
l <= qbpp::var_int("name") <= u | between(var_int("name"), l, u) |
l <= qbpp::var_int("name", s1) <= u | between(var_int("name", s1), l, u) |
x.name() | x.name |
x.str() | str(x) |
x.min_val() | x.min_val (property) |
x.max_val() | x.max_val (property) |
x.vars() | x.vars (property) |
x.coeffs() | x.coeffs (property) |