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)

Sortida:

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
})

Sortida:

2.0

Avís

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. Each wraps a Parameter, Term, or any other base variable and defers 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) # prints: "sin[theta] + cos[(2) * theta]"
print(expr.evaluate({})) # prints: "0.5" (using default parameter values)

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

The list of possible mathematical maps includes:

  • Abs for absolute value

  • Exp for exponential

  • Log for logarithm

  • Pow for power functions

  • Sqrt for square root

  • Inv for inverse

  • Sin for sine

  • Cos for cosine

  • Tan for tangent

Comparison Terms

Each ComparisonTerm defines a constraint using mathematical comparisons. Use the following operators to construct them:

Operació de comparació

Mètode QiliSDK

Àlies

Igualtat

Equal(lhs, rhs)

EQ(lhs, rhs)

Diferent de

NotEqual(lhs, rhs)

NEQ(lhs, rhs)

Menor que

LessThan(lhs, rhs)

LT(lhs, rhs)

Menor o igual que

LessThanOrEqual(lhs, rhs)

LEQ(lhs, rhs)

Major que

GreaterThan(lhs, rhs)

GT(lhs, rhs)

Major o igual que

GreaterThanOrEqual(lhs, rhs)

GEQ(lhs, rhs)

Nota: lhs i rhs fan referència a les expressions del costat esquerre i del costat dret, respectivament.

Exemple:

from qilisdk.core.variables import BinaryVariable, LT
x = BinaryVariable("x")
LT(2 * x - 1, 1)

Sortida:

(2) * x < (2.0)

Quan es crea un terme de comparació, les constants es mouen automàticament al costat dret i els termes amb variables al costat esquerre.