Selectors and expressions¶
Selectors¶
Selectors are how you refer to site-local data symbolically.
site(label)reads from the source configuration
xemitted(label)reads from the emitted configuration
x'after the update program has been applied for the current branchsymbol(name)creates a free named symbol that is not tied to an iterator label. You can also declare
default,doc, anddtypemetadata.
The selector object is nkdsl.dsl.selectors.SiteSelector.
Useful selector properties¶
.valuethe quantum number at the current site label
.indexthe integer site index bound by the iterator
Example:
from nkdsl import emitted, site, symbol
amplitude = symbol("g") * site("i").value * emitted("j").value
predicate = (site("i").index != site("j").index) & (site("i") > 0)
Symbol declarations¶
Free symbols can be declared with optional metadata:
from nkdsl import symbol
J = symbol(
"J",
default=1.0,
doc="Nearest-neighbor exchange coupling",
dtype="float32",
)
amplitude = J * site("i").value * emitted("j").value
Declaration fields:
defaultfallback value used when no runtime binding for the symbol is provided
dochuman-readable description for tooling/readability
dtypeoptional NumPy-compatible dtype declaration. If omitted and
defaultis provided, dtype is inferred from the default value.
Practical behavior:
symbol("J")is unresolved and can triggerNKDSL-E001at compile time.symbol("J", default=1.0)is considered resolved and does not trigger unresolved-symbol linting.if
dtypeis declared, runtime values are coerced to that dtype in the lowered evaluator.
Amplitude expressions¶
Numeric expressions are represented by nkdsl.ir.AmplitudeExpr. In user
code they are usually built through normal Python operators.
expr = 0.5 * site("i").value + 2 * site("j").value
Common helpers exposed through the IR and expression context include:
sqrtconjabs_powwrap_modsource_indexandtarget_indexfor flat-index source/target accessstatic_indexandstatic_emitted_indexremain available onAmplitudeExpr
ExpressionContext¶
For callback-based construction, nkDSL exposes
nkdsl.dsl.context.ExpressionContext.
def amplitude(ctx):
i = ctx.site("i")
return ctx.sqrt(i.value + 1)
The context API mirrors symbol declarations:
def amplitude(ctx):
J = ctx.symbol("J", default=1.0, dtype="float32")
i = ctx.site("i")
return J * i.value
This is mainly useful when a symbolic expression is easier to write as a small function than as one inline statement.