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)
Output:
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
})
Output:
2.0
Warning
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:
Comparison Operation |
QiliSDK Method |
Alias |
|---|---|---|
Equality |
||
Not Equal |
||
Less Than |
||
Less Than or Equal |
||
Greater Than |
||
Greater Than or Equal |
Note: lhs and rhs refer to the left-hand side and right-hand side expressions, respectively.
Example:
from qilisdk.core.variables import BinaryVariable, LT
x = BinaryVariable("x")
LT(2 * x - 1, 1)
Output:
(2) * x < (2.0)
When a comparison term is created, constants are automatically moved to the right-hand side, and variable terms to the left-hand side.