iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📈

Thinking about Matrix Product States (3)

に公開

Purpose

As a continuation of my previous post Thinking about Matrix Product States (2), I would like to consider Tensor-Train decomposition (often called MPS in fields like quantum chemistry) for more general tensors.

Essentially, I want to implement the contents of SIAM J. Sci. Comput., 33(5), 2295–2317. (23 pages) Tensor-Train Decomposition, I. V. Oseledets.

TT-Decomposition (Tensor-Train Decomposition)

In general, given a d-th order tensor with elements A(i_1,i_2, \cdots, i_d) \ \ (1 \leq i_k \leq n_k), we want to find a sequence of 3rd-order tensors \{ G_1, G_2, \cdots, G_d \} such that each element is:

\begin{align*} A(i_1,i_2, \cdots, i_d) = \sum_{\alpha_0,\cdots,\alpha_{d-1},\alpha_d} G_1(\alpha_0,i_1,\alpha_1) G_2(\alpha_1,i_2,\alpha_2) \cdots G_d(\alpha_{d-1},i_d,\alpha_d) \end{align*}

Each G_k(\alpha_{k-1},i_k,\alpha_k) is a 3rd-order tensor with dimensions r_{k-1} \times n_k \times r_k, called a "core tensor." r_k represents the dimension between adjacent core tensors and is called the "bond dimension." However, we impose the "boundary conditions" r_0 = r_d = 1.

If we align the notation with the paper The density-matrix renormalization group in the age of matrix product states, it might be written as:

\begin{align*} A_{i_1 i_2 \cdots i_d} = \sum_{\alpha_0,\cdots,\alpha_{d-1},\alpha_d} G^{i_1}_{\alpha_1} G^{i_2}_{\alpha_1,\alpha_2} \cdots G^{i_d}_{\alpha_{d-1}} \end{align*}

This notation feels like it conveys the "matrix product" sense more clearly. In this paper, when a quantum state is written in the computational basis, the coefficients of the 2^n basis vectors from \ket{0}^{\otimes n} to \ket{1}^{\otimes n}—i.e., the probability amplitudes—are collected into a sequence (or vector), reshaped into a tensor, and then TT-decomposed.

The proof of TT-decomposition itself is constructively provided in Theorem 2.1 using a matrix decomposition called SD (Skeleton decomposition). Furthermore, while this is an exact decomposition that perfectly reconstructs the original tensor, it induces the TT-SVD algorithm as an approximation algorithm. By narrowing down the bond dimensions, one can approximate the original tensor while compressing the information.

The TT-SVD algorithm itself is found on page 2301 of the paper.

Implementation Example (from Qiita Article)

Actually, an implementation example of this TT-SVD algorithm already exists, written in R in the article I tried tensor train decomposition in R.

Since I don't know R at all, I relied on AI power and tried GitHub Copilot's language translation feature. Although there were some rough edges, the resulting Python code after some manual adjustments is as follows:

from __future__ import annotations

from typing import Sequence
import numpy as np


def TT(x: np.ndarray, r: Sequence[int]):
    D = x.shape
    n = len(D)

    LIST = [None] * n
    for i in range(n-1):
        if i == 0:
            l0 = 1
        else:
            l0 = r[i-1]
        l1 = r[i]
        x = np.reshape(x, (D[i] * l0, np.prod(D[i+1:n])))
        U, _, _ = np.linalg.svd(x, full_matrices=False)
        LIST[i] = np.reshape(U[:, :l1], (l0, D[i], l1))
        x = np.transpose(U[:, :l1]).dot(x)
    LIST[n-1] = np.transpose(x)
    LIST[0] = np.reshape(LIST[0], (D[0], r[0]))
    return LIST

My first impression was that it seemed to capture the general feel of it.

Next, I want to reproduce the experiment from the same Qiita article. I struggled to understand how to read multi-dimensional arrays in R, but eventually, I found it was best to interpret them as follows:

Z = np.array([
    [
        [1.262954, -1.1476570, -0.05710677, 0.9921604, 0.83204713],
        [1.272429, -0.4115108, -0.69095384, -0.2793463, -0.37670272],
        [-0.928567, 0.4356833, -0.23570656, -0.4527840, -0.05487747],
        [2.404653, 0.3773956, -0.64947165, -1.0655906, -0.17262350],
    ],
    [
        [-0.3262334, -0.2894616, 0.5036080, -0.4295131, -0.2273287],
        [0.4146414, 0.2522234, -1.2845994, 1.7579031, 2.4413646],
        [-0.2947204, -1.2375384, -0.5428883, -0.8320433, 0.2501413],
        [0.7635935, 0.1333364, 0.7267507, -1.5637821, -2.2239003],
    ],
    [
        [1.329799263, -0.2992151, 1.08576936, 1.2383041, 0.2661374],
        [-1.539950042, -0.8919211, 0.04672617, 0.5607461, -0.7953391],
        [-0.005767173, -0.2242679, -0.43331032, -1.1665705, 0.6182433],
        [-0.799009249, 0.8041895, 1.15191175, 1.1565370, -1.2636144],
    ],
])

By the way, in that article, the bond dimensions are given as c(2,3), but since the original tensor is small, it is quite sensitive, and you end up with a tensor that is hardly an approximation. c(3,4) is a decent approximation, and c(3,5) reconstructs the original tensor.

Verification

bond_dims = (3, 5)
tt_list = TT(Z, bond_dims)

Z1 = np.einsum("ia,ajb,kb->ijk", *tt_list)
np.allclose(Z, Z1)

True

In this way, we can verify that the original tensor has been reconstructed.

Next, if we run the following as a somewhat milder approximation:

TT(Z, (3, 4))
array([[[ 1.26321438, -1.14774143, -0.06648278,  0.99668222,  0.82531518],
        [ 1.25868211, -0.40705353, -0.19594873, -0.51807543, -0.02129007],
        [-0.92899622,  0.43582247, -0.2202511 , -0.4602378 , -0.04378048],
        [ 2.39952597,  0.37905798, -0.4648549 , -1.15462684, -0.04006905]],

       [[-0.31743688, -0.29231377,  0.18685824, -0.27675227, -0.45475438],
        [ 0.40846868,  0.25422483, -1.06232878,  1.6507073 ,  2.60095445],
        [-0.29416419, -1.23771874, -0.56291648, -0.82238419,  0.23576111],
        [ 0.76143661,  0.13403575,  0.80441711, -1.60123875, -2.16813598]],

       [[ 1.33996203, -0.30251026,  0.71982315,  1.41479121,  0.00338877],
        [-1.55201823, -0.88800813,  0.4812836 ,  0.35116944, -0.48332776],
        [ 0.0033049 , -0.22720941, -0.75998201, -1.00902456,  0.3836937 ],
        [-0.80351403,  0.80565012,  1.31412219,  1.07830678, -1.14714764]]])

The result above is what we get, and you can see that it's reasonably close compared to the original tensor.

As we narrow down the bond dimensions between the core tensors, the dimensions of the core tensors become:

(3, 3) (3, 4, 5) (5, 5): Low compression
(3, 3) (3, 4, 4) (5, 4): Medium compression
(3, 2) (2, 4, 3) (5, 3): High compression

In such a small tensor decomposition, the parameter count actually increases, making it difficult to see; however, it's not as if we can't see the potential for the information volume to be reduced from low → medium → high if handled effectively with a larger tensor.

Implementation Example (Straight from the Paper)

Now, looking closely at the implementation in the Qiita article, there are parts that don't seem to follow the formulas written in the paper exactly, so let's try a straightforward implementation:

def TT_SVD(C: np.ndarray, r: Sequence[int]) -> list[np.ndarray]:
    """TT_SVD algorithm

    Args:
        C (np.ndarray): n-dimensional input tensor
        r (Sequence[int]): a list of bond dimensions

    Returns:
        np.ndarray: a list of core tensors of TT-decomposition
    """

    dims = C.shape
    n = len(dims)  # n-dimensional tensor
    if len(r) != n - 1:
        raise ValueError(f"{len(r)=} must be {n - 1}.")

    tt_cores = []
    for i in range(n - 1):
        if i == 0:
            ri_1 = 1
        else:
            ri_1 = r[i - 1]
        ri = r[i]
        C = C.reshape(ri_1 * dims[i], np.prod(dims[i + 1:]))
        U, S, Vh = np.linalg.svd(C, full_matrices=False)
        # approximation
        U = U[:, :ri]
        S = S[:ri]
        Vh = Vh[:ri, :]
        tt_cores.append(U.reshape(ri_1, dims[i], ri))
        C = np.diag(S) @ Vh
    tt_cores.append(C.T)
    tt_cores[0] = tt_cores[0].reshape(dims[0], r[0])
    return tt_cores

Except for taking the bond dimensions r externally, following the Qiita article, the implementation looks just like the one in the paper, doesn't it?

Note that both the Qiita implementation and this one impose the "boundary conditions" r_0 = r_d = 1, resulting in indices with dimension 1. These have been dropped from the start, as if they were squeezed.

Now, does this match the one from the Qiita article?

Verification

# strict
strict_bond_dims = (3, 5)

# medium
medium_bond_dims = (3, 4)

# loose
loose_bond_dims = (2, 3)

for bond_dims in (strict_bond_dims, medium_bond_dims, loose_bond_dims):
    tt_list = TT(Z, bond_dims)
    tt_list2 = TT_SVD(Z, bond_dims)
    for core1, core2 in zip(tt_list, tt_list2):
        print(np.allclose(core1, core2))
    print("-" * 10)
True
True
True
----------
True
True
True
----------
True
True
True
----------

As we can see, they match perfectly.

Summary

We have examined Tensor-Train decomposition for more general tensors through the paper and the Qiita article.

Reading the paper can be quite daunting, but the parts that feel that way are mostly where the reshape procedures (in NumPy terms) are explained in detail in natural language; once implemented in code, it turns out to be something like this.

References

[O] Tensor-Train Decomposition, SIAM J. Sci. Comput., 33(5), 2295–2317. (23 pages), I. V. Oseledets
[S] The density-matrix renormalization group in the age of matrix product states, arXiv:1008.3477, Ulrich Schollwoeck
[T] I tried tensor train decomposition in R, Qiita, @Yh_Taguchi

GitHubで編集を提案

Discussion