neuralqx.experimental.operators.symbolic.dsl package¶
User-facing declarative symbolic operator DSL.
- class DOperator(hilbert, name='operator', *, dtype='float64', hermitian=False)¶
Bases:
objectFluent builder for declarative symbolic quantum operators.
The builder accumulates one or more terms. Each term consists of an iterator (which sites to visit), an optional predicate (which visits to activate), and one or more emissions (how to rewrite the configuration and what matrix element to assign per active visit).
Calling any iterator method (
for_each_site,for_each_pair, …,globally) seals the previous term (if any) and begins a new one..whereand.emitalways target the current open term.- Parameters:
hilbert (
DiscreteHilbert) – NetKetDiscreteHilbertspace.name (
str) – Readable operator name (accessible as.nameon the resultingSymbolicOperator).dtype (
str) – Matrix-element dtype string (default"float64").hermitian (
bool) – Whether to declare the operator Hermitian.
- class Update(_program=None)¶
Bases:
objectImmutable, chainable site-update program builder.
Every instance method appends one operation and returns a new
Updateobject, the original is never mutated. The canonical entry points are the module-level free functions (shift(),write(),swap(),permute(),affine(),scatter(),identity()) which avoid theUpdate()boilerplate.- Parameters:
_program (
UpdateProgram|None) – Internal update program (do not pass manually).
- shift(site_ref, delta)¶
Appends
x'[i] = x[i] + delta.- Parameters:
site_ref (
str|SiteSelector|int|AmplitudeExpr) – Target site (label string, selector, or flat index).delta (
Any) – Shift amount, numeric or amplitude expression.
- Return type:
- Returns:
New
Updatewith this operation appended.
- shift_mod(site_ref, delta)¶
Appends a Hilbert-aware wrapped shift.
Semantics are resolved from the enclosing operator’s Hilbert space at build/compile time. For now this requires contiguous unit-spaced integer local_states such as [-m_max, …, m_max].
Resulting runtime semantics:
x’[i] = ((x[i] + delta - state_min) % mod_span) + state_min
- Return type:
- write(site_ref, value)¶
Appends
x'[i] = value.- Parameters:
site_ref (
str|SiteSelector|int|AmplitudeExpr) – Target site.value (
Any) – New quantum number, numeric or amplitude expression.
- Return type:
- Returns:
New
Updatewith this operation appended.
- swap(site_a, site_b)¶
Appends
x'[a], x'[b] = x[b], x[a].- Parameters:
site_a (
str|SiteSelector|int|AmplitudeExpr) – First site.site_b (
str|SiteSelector|int|AmplitudeExpr) – Second site.
- Return type:
- Returns:
New
Updatewith this operation appended.
- permute(*site_refs)¶
Appends a cyclic rotation over K sites.
After the operation:
x'[s0] ← x[s1], x'[s1] ← x[s2], ..., x'[sK-1] ← x[s0]All K source values are captured from the current
x'state before any writes are applied, so the rotation is atomic.- Parameters:
*site_refs (
str|SiteSelector|int|AmplitudeExpr) – Two or more site references in rotation order.- Return type:
- Returns:
New
Updatewith this operation appended.- Raises:
ValueError – If fewer than 2 site references are provided.
- affine(site_ref, *, scale, bias=0)¶
Appends
x'[i] = scale * x[i] + bias.- Parameters:
site_ref (
str|SiteSelector|int|AmplitudeExpr) – Target site.scale (
Any) – Multiplicative scale, numeric or amplitude expression.bias (
Any) – Additive bias, numeric or amplitude expression (default 0).
- Return type:
- Returns:
New
Updatewith this operation appended.
- scatter(flat_indices, values)¶
Appends bulk writes to static flat site indices.
For each
(flat_index, value)pair:x'[flat_index] = valueIndices must be compile-time-constant integers (baked into the IR). Values may be arbitrary amplitude expressions.
- Parameters:
- Return type:
- Returns:
New
Updatewith this operation appended.- Raises:
ValueError – If flat_indices and values have different lengths.
- affine(site_ref, *, scale, bias=0)¶
Returns an
Updatecomputingx'[i] = scale * x[i] + bias.Example:
affine("i", scale=2, bias=-1) # x'[i] = 2*x[i] - 1 affine(0, scale=-1, bias=0) # negate flat site 0
- Return type:
- identity()¶
Returns the identity (no-op)
Update.Use for diagonal operators where
x' = x:DOperator(hi, "diagonal").globally().emit(identity(), amplitude=my_expr)- Return type:
- permute(*site_refs)¶
Returns an
Updateperforming a cyclic rotation over K sites.Example:
permute("i", "j", "k") # x'[i]←x[j], x'[j]←x[k], x'[k]←x[i] permute(0, 5, 10) # same with flat indices
- Return type:
- scatter(flat_indices, values)¶
Returns an
Updateperforming bulk writes to static flat indices.Example:
scatter([0, 10, 20], [1, -1, 0]) # write constant values scatter([0, 10], [site("i").value, 0]) # mixed expr / constant
- Return type:
- shift(site_ref, delta)¶
Returns an
Updatethat shifts site site_ref by delta.Example:
shift("i", +1) # raise site i by 1 shift(0, -1) # lower flat site 0 by 1 shift("j", site("i").value) # shift j by x[i]
- Return type:
- shift_mod(site_ref, delta)¶
Returns an Update performing a Hilbert-aware wrapped modular shift.
Example:
shift_mod("i", +1) shift_mod(0, -2)
- Return type:
- swap(site_a, site_b)¶
Returns an
Updatethat swaps sites site_a and site_b.Example:
swap("i", "j") # exchange x[i] and x[j] swap(0, 10) # exchange flat sites 0 and 10
- Return type:
- write(site_ref, value)¶
Returns an
Updatethat writes value to site site_ref.Example:
write("i", 0) # zero site i write(5, site("j").value) # copy x[j] into flat site 5
- Return type:
- class ExpressionContext(*args, **kwargs)¶
Bases:
objectUtility context passed to DSL callables at build time.
This context only builds IR expression nodes and never captures Python-runtime callbacks.
Example
>>> def my_amplitude(ctx): ... i = ctx.site("i") ... return ctx.sqrt(i.value + 1)
- symbol(name)¶
Returns a free symbolic amplitude expression.
- Return type:
- site(label)¶
Returns a site selector by label.
- Return type:
- emitted(label)¶
Returns an emitted-state selector by label.
- Return type:
- site(label)¶
Returns a symbolic site selector.
- Parameters:
label (
str) – Iterator label bound byfor_each_site(label)orfor_each_pair(label_a, label_b).- Return type:
- Returns:
Site selector handle.
Example
from neuralqx.experimental.operators.symbolic.dsl import site s = site("i") print(s.value) # AmplitudeExpr, x[i] print(s.index) # AmplitudeExpr, i print(s.value < 3) # PredicateExpr, x[i] < 3 print(s.value + 1) # AmplitudeExpr, x[i] + 1
- emitted(label)¶
Returns a symbolic selector bound to the emitted or connected state
x'.- Return type:
Example
from neuralqx.experimental.operators.symbolic.dsl import emitted e = emitted("i") print(e.value) # AmplitudeExpr, x'[i] print(e.index) # AmplitudeExpr, i
- symbol(name)¶
Returns a free symbolic amplitude expression by name.
Free symbols are not bound to any site iterator, they are resolved at operator-evaluation time from external parameter dictionaries.
- Parameters:
name (
str) – Symbol name.- Return type:
- Returns:
Symbolic amplitude expression.
- class SiteSelector(label, namespace='site')¶
Bases:
objectSymbolic selector for one Hilbert-space site iterator.
SiteSelector is created by
site()and used inside DSL predicates, amplitude rules, and update programs. Attribute access on a selector returns symbolicAmplitudeExprnodes that are resolved by the compiler at lowering time.- Parameters:
label (
str) – Iterator label bound byfor_each_site(label).