{
"cells": [
{
"cell_type": "markdown",
"id": "b4ea66a9-262e-4909-bfd6-8025935ec0b8",
"metadata": {},
"source": [
"# Approximate/compressed contraction\n",
"\n",
"In this example we use `cotengra` to find a 'compressed' contraction tree\n",
"(as detailed in [2206.07044](https://arxiv.org/abs/2206.07044)) for a $10\\times10\\times10$\n",
"tensor network representation of the partition function of the 3D classical\n",
"ising model. We then use `quimb` to actually perform the compressed\n",
"contraction."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2100490c-2f63-4eaa-b63a-2c02c8e7550d",
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_formats = ['svg']\n",
"import cotengra as ctg\n",
"import quimb.tensor as qtn"
]
},
{
"cell_type": "markdown",
"id": "e971cc09-ffc9-45e4-a350-dab40aa3bf02",
"metadata": {},
"source": [
"First we get the tensor network from `quimb`, though note we could also specify it as usual via\n",
"`inputs`, `output` and `size_dict`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ac8d2ffd-84de-4e3b-80e0-e2e0405607a8",
"metadata": {},
"outputs": [],
"source": [
"tn = qtn.TN3D_classical_ising_partition_function(\n",
" 10, 10, 10, beta=0.3\n",
")"
]
},
{
"cell_type": "markdown",
"id": "7fccd6bc-5b44-4f3a-ad3e-f3a8939e5c92",
"metadata": {},
"source": [
"Then we set-up our *compressed* contraction tree finder:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "82b8e3e8-7296-4510-a25b-05335aa5abda",
"metadata": {},
"outputs": [],
"source": [
"chi = 16\n",
"copt = ctg.ReusableHyperCompressedOptimizer(\n",
" chi,\n",
" max_repeats=256,\n",
" minimize='combo-compressed', \n",
" progbar=True,\n",
" # # save paths to disk:\n",
" # directory=True \n",
")"
]
},
{
"cell_type": "markdown",
"id": "89aa8156-c988-4f05-874f-3636d84ad616",
"metadata": {},
"source": [
"`quimb` knows about `cotengra` and will directly return the \n",
"`ContractionTreeCompressed` generated during the search:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0f604391-7678-4a3d-b12b-2b33cc6e52ca",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"log2[SIZE]: 21.00 log10[FLOPs]: 10.89: 100%|████████████████████████| 256/256 [00:15<00:00, 16.02it/s]\n"
]
}
],
"source": [
"tree = tn.contraction_tree(copt)"
]
},
{
"cell_type": "markdown",
"id": "8229a18e-a5e1-4705-bfef-2116c662017e",
"metadata": {},
"source": [
"We can perform various bits of introspection on both the compressed contraction tree and optimizer now\n",
"before we actually perform the contraction."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ebccaf7f-a3fa-4bc4-90df-a9227d3cf99b",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"