Variables and Parameters
Variables
This module offers tools to define different types of variables:
Continuous Variableswith the following customizable parameters:Domain: Specifies the variable type, one of
REAL,INTEGER,POSITIVE_INTEGER,BINARY, orSPIN.Bounds: Defines the allowed value range of the variable.
Encoding: Specifies how the variable is represented using binary encodings, one of:
Bitwise,DomainWall, orOneHot.Precision: Applicable to
Domain.REALdomain; defines the resolution (e.g., floating-point precision).
An example of creating different types of variables:
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")
Continuous variables support indexing, where each index refers to a component of the binary-encoded form of the variable. For example:
print(x.to_binary())
Output:
(0.1) * x(0) + (0.2) * x(1) + (0.4) * x(2) + (0.30000000000000004) * x(3) + (1.0)
To index the first binary variable from the binary representation of x you can write: x[0].
Each binary variable configuration generates a float within the bounds, based on the defined precision. For instance:
x.evaluate([0, 1, 0, 0])
Output:
1.2
Parameters
Many components in QiliSDK expose symbolic parameters that can be optimized or
re-bound at runtime. The Parameter class
represents a scalar symbol with optional bounds, and
Parameterizable provides a uniform API
(get_parameter_names(), set_parameter_values()…) implemented by circuits,
schedules, models, and more.
from qilisdk.core import Parameter
theta = Parameter("theta", value=0.5, bounds=(0.0, 1.0))
print(theta.value) # 0.5
theta.set_value(0.75)
print(theta.bounds) # (0.0, 1.0)
Parameters behave like symbolic variables in algebraic expressions, so you can
combine them with other variables and evaluate terms without having to pass the
parameter explicitly - its stored value is used automatically.
Objects that inherit from Parameterizable
collect all the Parameter instances they encounter. For example:
from qilisdk.digital import Circuit, RX
circuit = Circuit(nqubits=1)
circuit.add(RX(0, theta=theta))
print(circuit.get_parameter_names()) # ['RX(0)_theta_0']
print(circuit.get_parameter_values()) # [0.75]
circuit.set_parameters({"RX(0)_theta_0": 0.9})
All parameter helper methods accept an optional where predicate to filter
the exposed parameters. This is useful when you want to update only a subset,
such as trainable parameters.
trainable_values = circuit.get_parameter_values(where=lambda p: p.is_trainable)
circuit.set_parameter_values([0.3], where=lambda p: p.is_trainable)
Note
Parameterizable objects expose parameter state through
get_parameters(),
get_parameter_names(),
get_parameter_values(),
and the corresponding set_* methods. Accessing .parameters directly
is not part of the public API.