{
"cells": [
{
"cell_type": "markdown",
"id": "48db7bce",
"metadata": {},
"source": [
"# Contracting a large output lazily\n",
"\n",
"In this example we generate perform a contraction with an \n",
"output that would be larger than can fit in memory. \n",
"However we can still generate it in chunks which is \n",
"sufficient to compute for example:\n",
"\n",
"$$\n",
"S = - \\sum_{\\{a, b, c, \\ldots\\}} p_{a, b, c, \\ldots} \\log p_{a, b, c, \\ldots}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9724fa7d-a982-477d-b96b-0170d64fa6a4",
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_formats = ['svg']\n",
"import cotengra as ctg\n",
"import quimb.tensor as qtn\n",
"from autoray import do"
]
},
{
"cell_type": "markdown",
"id": "8a706dc0-3bff-4585-a6c1-b4efb889d96f",
"metadata": {},
"source": [
"Use quimb to make an example factor graph / probability distribution:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7a84d1c9-a922-42ea-b507-a3f46b88ee2b",
"metadata": {},
"outputs": [],
"source": [
"htn = qtn.HTN3D_classical_ising_partition_function(\n",
" 6, 6, 6, beta=0.3,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "8e3c972a",
"metadata": {},
"source": [
"Here we optionally first convert the tensor network's data to cupy GPU arrays:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7ac01629",
"metadata": {},
"outputs": [],
"source": [
"def to_backend(x):\n",
" import cupy\n",
"\n",
" return cupy.asarray(x, dtype=\"float32\")\n",
"\n",
"htn.apply_to_arrays(to_backend)"
]
},
{
"cell_type": "markdown",
"id": "110d1659-4e86-408c-98be-a32c4bd7e5a6",
"metadata": {},
"source": [
"Select a subset of output variables (more than we can store the full tensor for!):"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "80b1d3f3-2d21-4594-885c-b08088d26e03",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"36"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output_inds = tuple(\n",
" f\"s{i},{j},{k}\"\n",
" for i in range(4)\n",
" for j in range(3)\n",
" for k in range(3)\n",
")\n",
"len(output_inds)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0de83219-7eea-4366-a2e0-ab1367970380",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"