Terms, Maps and Comparisons
Terms
Variables can be combined algebraically to form expressions
known as Terms. For example:
from qilisdk.core.variables import BinaryVariable, Bitwise, Domain, SpinVariable, Variable
x = Variable("x", domain=Domain.REAL, bounds=(1, 2), encoding=Bitwise, precision=1e-1)
s = SpinVariable("s")
b = BinaryVariable("b")
t1 = 2 * x + 3
print("t1:", t1)
t2 = 3 * x**2 + 2 * x + 4
print("t2:", t2)
t3 = 2 * x + b - 1
print("t3:", t3)
t4 = t1 - t2
print("t4:", t4)
Salida:
t1: (2) * x + (3)
t2: (3) * (x^2) + (2) * x + (4)
t3: (2) * x + b + (-1)
t4: (-1.0) + (-3.0) * (x^2)
Terms can be evaluated by providing values for the involved variables:
t3.evaluate({
x: 1.5,
b: 0
})
Salida:
2.0
Advertencia
To evaluate a term, all participating variables must be assigned valid values within their respective domains and bounds.
Mathematical Maps
Use MathematicalMap helpers to apply common
functions to a parameter or term while keeping expressions symbolic.
Sin and Cos
wrap a Parameter, Term,
or any other base variable and defer evaluation until values are provided.
from qilisdk.core.variables import Parameter, Sin, Cos
theta = Parameter("theta", 0.5)
expr = Sin(theta) + Cos(2 * theta)
print(expr) # sin[theta] + cos[(2) * theta]
print(expr.evaluate({})) # uses theta.value automatically
# You can also supply a different value at evaluation time:
print(expr.evaluate({theta: 1.0}))
These maps compose naturally with other terms, so you can include them in constraints or objectives and rely on the same evaluation and encoding rules as other symbolic expressions.
Comparison Terms
Each ComparisonTerm defines a constraint using mathematical comparisons.
Use the following operators to construct them:
Operación de comparación |
Método QiliSDK |
Alias |
|---|---|---|
Igualdad |
||
Distinto de |
||
Menor que |
||
Menor o igual que |
||
Mayor que |
||
Mayor o igual que |
Nota: lhs y rhs hacen referencia a las expresiones del lado izquierdo y del lado derecho, respectivamente.
Ejemplo:
from qilisdk.core.variables import BinaryVariable, LT
x = BinaryVariable("x")
LT(2 * x - 1, 1)
Salida:
(2) * x < (2.0)
Cuando se crea un término de comparación, las constantes se mueven automáticamente al lado derecho y los términos con variables al lado izquierdo.