{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"raw_mimetype": "text/restructuredtext"
},
"source": [
"(ex-tensorflow-tn-opt)=\n",
"\n",
"# Optimizing a Tensor Network using Tensorflow\n",
"\n",
"In this example we show how a general machine learning\n",
"strategy can be used to optimize arbitrary tensor networks\n",
"with respect to any target loss function.\n",
"\n",
"We'll take the example of maximizing the overlap of some\n",
"matrix product state with periodic boundary conditions\n",
"with a densely represented state, since this does not\n",
"have a simple, deterministic alternative.\n",
"\n",
"`quimb` makes use of `cotengra` which can contract\n",
"tensors with a variety of backends as well as `autoray`\n",
"for handling array operations agnostically. Here we'll use\n",
"`tensorflow-v2` for the actual auto-gradient computation."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_formats = ['svg']\n",
"\n",
"import quimb as qu\n",
"import quimb.tensor as qtn\n",
"from quimb.tensor.optimize import TNOptimizer\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, find a (dense) PBC groundstate, $| gs \\rangle$:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"L = 16\n",
"H = qu.ham_heis(L, sparse=True, cyclic=True)\n",
"gs = qu.groundstate(H)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we convert it to a dense 1D 'tensor network':"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dense1D([\n",
" Tensor(shape=(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), inds=('k0', 'k1', 'k2', 'k3', 'k4', 'k5', 'k6', 'k7', 'k8', 'k9', 'k10', 'k11', 'k12', 'k13', 'k14', 'k15'), tags=oset(['I0', 'I1', 'I2', 'I3', 'I4', 'I5', 'I6', 'I7', 'I8', 'I9', 'I10', 'I11', 'I12', 'I13', 'I14', 'I15'])),\n",
"], tensors=1, indices=16, L=16, max_bond=2)\n"
]
}
],
"source": [
"# this converts the dense vector to an effective 1D tensor network (with only one tensor)\n",
"target = qtn.Dense1D(gs)\n",
"print(target)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we create an initial guess random MPS, $|\\psi\\rangle$:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"