{ "cells": [ { "cell_type": "markdown", "id": "82310d98", "metadata": {}, "source": [ "# Ising model\n", "\n", "A compact transverse-field Ising example using the same four-step pattern that underlies the rest of the DSL: iterate, filter if needed, emit updates, and compile." ] }, { "cell_type": "markdown", "id": "c2375c9e", "metadata": {}, "source": [ "We consider the one-dimensional transverse-field Ising Hamiltonian\n", "\n", "$$H = J \\sum_{\\langle i,j \\rangle} \\sigma_i^z \\sigma_j^z - h \\sum_i \\sigma_i^x$$\n", "\n", "The first term is diagonal in the computational basis. The second flips one spin at a time. In the DSL that becomes one bond iterator emitting ``identity()`` branches and one site iterator emitting a single-site rewrite." ] }, { "cell_type": "markdown", "id": "5bb593a56e3bd1cb", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "id": "39c40633", "metadata": { "ExecuteTime": { "end_time": "2026-05-02T11:56:19.263998Z", "start_time": "2026-05-02T11:56:18.301244Z" } }, "source": [ "import netket as nk\n", "import nkdsl\n", "\n", "L = 6\n", "J = 1.2\n", "h = 0.7\n", "\n", "hi = nk.hilbert.Spin(s=0.5, N=L)\n", "g = nk.graph.Chain(length=L, pbc=True)\n", "edges = g.edges()\n" ], "outputs": [ { "data": { "text/plain": [ "\u001B[1;36m∣NK⟩ Tip: \u001B[0mPrefer the new nk.driver.VMC_SR over VMC which supports minSR and SPRING.\n" ], "text/html": [ "
∣NK⟩ Tip: Prefer the new nk.driver.VMC_SR over VMC which supports minSR and SPRING.\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data",
"jetTransient": {
"display_id": null
}
}
],
"execution_count": 1
},
{
"cell_type": "markdown",
"id": "3a2920ac5b0a0b09",
"metadata": {},
"source": [
"## Construct the symbolic operator"
]
},
{
"cell_type": "code",
"id": "20dc20ba",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-02T11:56:20.410864Z",
"start_time": "2026-05-02T11:56:19.265750Z"
}
},
"source": [
"H = (\n",
" nkdsl.SymbolicDiscreteJaxOperator(hi, \"ising_sym\", hermitian=True)\n",
" .for_each((\"i\", \"j\"), over=edges)\n",
" .emit(\n",
" nkdsl.identity(),\n",
" matrix_element=J * nkdsl.site(\"i\").value * nkdsl.site(\"j\").value,\n",
" )\n",
" .for_each_site(\"i\")\n",
" .emit(\n",
" nkdsl.write(\"i\", -nkdsl.site(\"i\").value),\n",
" matrix_element=-h,\n",
" )\n",
" .compile()\n",
")"
],
"outputs": [],
"execution_count": 2
},
{
"cell_type": "markdown",
"id": "483d74f54e25d525",
"metadata": {},
"source": [
"## Construct the state"
]
},
{
"cell_type": "code",
"id": "a3b17cdffd9794e8",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-02T11:56:20.697232Z",
"start_time": "2026-05-02T11:56:20.439795Z"
}
},
"source": [
"model = nk.models.RBM(alpha=1, param_dtype=float)\n",
"state = nk.vqs.FullSumState(hi, model, seed=101)\n",
"opt = nk.optimizer.Sgd(learning_rate=0.01)\n",
"driver = nk.driver.VMC(H, opt, variational_state=state)"
],
"outputs": [],
"execution_count": 3
},
{
"cell_type": "markdown",
"id": "b39f50e0ab3d2a62",
"metadata": {},
"source": [
"## Run the VMC"
]
},
{
"cell_type": "code",
"id": "72e4999a",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-02T11:56:21.229504Z",
"start_time": "2026-05-02T11:56:20.697616Z"
}
},
"source": [
"driver.run(50, out=None, show_progress=False, timeit=True)\n",
"print(\"Final energy statistics:\", state.expect(H))"
],
"outputs": [
{
"data": {
"text/plain": [],
"text/html": [
"\n"
]
},
"metadata": {},
"output_type": "display_data",
"jetTransient": {
"display_id": null
}
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"╭────────────────────────────────────────────── Timing Information ───────────────────────────────────────────────╮\n",
"│ Total: 0.384 │\n",
"│ └── (92.2%) | VMC._forward_and_backward : 0.354 s │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\n",
"Final energy statistics: -4.580e+00 [σ²=8.4e+00]\n"
]
}
],
"execution_count": 4
},
{
"cell_type": "markdown",
"id": "6771ce0ce2a27d89",
"metadata": {},
"source": [
"## Compare with NetKet native"
]
},
{
"cell_type": "markdown",
"id": "1b65d8b05b477fba",
"metadata": {},
"source": [
"### Operator"
]
},
{
"cell_type": "code",
"id": "9508e036aa8d2ea3",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-02T11:56:21.245514Z",
"start_time": "2026-05-02T11:56:21.234831Z"
}
},
"source": [
"ising_nk = nk.operator.IsingJax(hi, g, h=h, J=J)"
],
"outputs": [],
"execution_count": 5
},
{
"cell_type": "markdown",
"id": "ab874f4e5a49df0c",
"metadata": {},
"source": [
"### State"
]
},
{
"cell_type": "code",
"id": "c5e4ff91a4e79aed",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-02T11:56:21.249543Z",
"start_time": "2026-05-02T11:56:21.245824Z"
}
},
"source": [
"model = nk.models.RBM(alpha=1, param_dtype=float)\n",
"state = nk.vqs.FullSumState(hi, model, seed=101)\n",
"opt = nk.optimizer.Sgd(learning_rate=0.01)\n",
"driver = nk.driver.VMC(ising_nk, opt, variational_state=state)"
],
"outputs": [],
"execution_count": 6
},
{
"cell_type": "markdown",
"id": "14b640b6c77bebbc",
"metadata": {},
"source": [
"### VMC"
]
},
{
"cell_type": "code",
"id": "e2cef2481dccd960",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-02T11:56:21.334504Z",
"start_time": "2026-05-02T11:56:21.250133Z"
}
},
"source": [
"driver.run(50, out=None, show_progress=False, timeit=True)\n",
"print(\"Final energy statistics:\", state.expect(ising_nk))"
],
"outputs": [
{
"data": {
"text/plain": [],
"text/html": [
"\n"
]
},
"metadata": {},
"output_type": "display_data",
"jetTransient": {
"display_id": null
}
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"╭────────────────────────────────────────────── Timing Information ───────────────────────────────────────────────╮\n",
"│ Total: 0.058 │\n",
"│ └── (73.4%) | VMC._forward_and_backward : 0.043 s │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\n",
"Final energy statistics: -4.580e+00 [σ²=8.4e+00]\n"
]
}
],
"execution_count": 7
},
{
"cell_type": "markdown",
"id": "7c1ae215",
"metadata": {},
"source": [
"## What this notebook shows\n",
"\n",
"This is the complete ``nkdsl`` workflow in one place.\n",
"\n",
"1. Define a Hilbert space and a static set of tuples to iterate over.\n",
"2. Build the Hamiltonian declaratively with iterators, predicates, and emissions.\n",
"3. Compile the symbolic description into a NetKet-compatible JAX operator.\n",
"4. Optimise a variational state directly against that compiled operator.\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"pygments_lexer": "ipython3"
},
"mystnb": {
"execution_mode": "cache"
}
},
"nbformat": 4,
"nbformat_minor": 5
}