{
"cells": [
{
"cell_type": "markdown",
"id": "69e58b47-c160-40c4-a12e-e1299918b5af",
"metadata": {},
"source": [
"# Contraction\n",
"\n",
"```{hint}\n",
"This page describes the low level contraction functionality of `cotengra`, see\n",
"the [high level interface functions](high-level-interface) for standard use.\n",
"```\n",
"\n",
"You can pass the `cotengra` optimizers, or \n",
"[paths](cotengra.ContractionTree.get_path) \n",
"generated with them, to \n",
"[`quimb`](https://quimb.readthedocs.io/en/latest/), \n",
"[`opt_einsum`](https://dgasmith.github.io/opt_einsum/), \n",
"and other libraries (generally via the `optimize=` kwarg), but `cotengra` also \n",
"provides its own contraction functionality encapsulated in the \n",
"[`ContractionTree.contract`](cotengra.ContractionTree.contract) method.\n",
"\n",
"For an example let's generate a square lattice contraction:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a079cf1f",
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_formats = ['svg']\n",
"import numpy as np\n",
"import cotengra as ctg\n",
"\n",
"inputs, output, shapes, size_dict = ctg.utils.lattice_equation([10, 10])\n",
"arrays = [np.random.uniform(size=shape) for shape in shapes]"
]
},
{
"cell_type": "markdown",
"id": "ffc62ec9",
"metadata": {},
"source": [
"Then find a high quality tree for it (using `minimize='combo'` is generally \n",
"good for real world contraction performance):"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "02cf1f61",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"log2[SIZE]: 10.00 log10[FLOPs]: 5.28: 100%|██████████| 128/128 [00:04<00:00, 28.46it/s]\n"
]
}
],
"source": [
"opt = ctg.HyperOptimizer(\n",
" minimize='combo',\n",
" reconf_opts={},\n",
" progbar=True,\n",
")\n",
"tree = opt.search(inputs, output, size_dict)"
]
},
{
"cell_type": "markdown",
"id": "b509f66e",
"metadata": {},
"source": [
"We can have a quick look at the tree:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7427229f",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"