Periodic DMRG and Calculations

Here we demonstrate 2-site periodic DMRG for finding the groundstate of the spin-1/2 Heisenberg model, and performing a couple of calculations efficiently with the resulting periodic MPS.

[1]:
from quimb import *
from quimb.tensor import *
[2]:
H = MPO_ham_heis(300, cyclic=True)

quimb has the function heisenberg_energy which can calculate the analytic energy we are looking for:

[3]:
E_exact = heisenberg_energy(300)
E_exact
[3]:
-132.94690126514288

Let’s create the core DMRG object that handles all the algorithm:

[4]:
dmrg = DMRG2(H)

DMRG2 internally forms the needed energy and norm overlaps, reusing views of the same data. We can graph, for example, the full energy expectation:

[5]:
%matplotlib inline
dmrg.TN_energy.graph(color=['_KET', '_HAM', '_BRA'])  # might be slow as uses force repulsion
../_images/examples_ex_dmrg_periodic_9_0.png

Or if we want to plot with fixed positions:

[6]:
from cmath import exp, pi
fix = {
    **{(f'I{i}', '_KET'): (100 * exp(2j*pi * i / 300).real, 100 * exp(2j*pi * i / 300).imag) for i in range(300)},
    **{(f'I{i}', '_HAM'): (105 * exp(2j*pi * i / 300).real, 105 * exp(2j*pi * i / 300).imag) for i in range(300)},
    **{(f'I{i}', '_BRA'): (110 * exp(2j*pi * i / 300).real, 110 * exp(2j*pi * i / 300).imag) for i in range(300)},
}
[7]:
dmrg.TN_energy.graph(color=['_KET', '_HAM', '_BRA'], fix=fix, iterations=0)
../_images/examples_ex_dmrg_periodic_12_0.png

The default algorithm settings are reasonable enough to get started with:

[8]:
dmrg.solve(max_sweeps=4, verbosity=1, cutoffs=1e-6)
SWEEP-1, direction=R, max_bond=8, cutoff:1e-06
100%|█████████████████████████████████████████| 300/300 [00:05<00:00, 52.66it/s]
Energy: -132.2070401622953 ... not converged.
SWEEP-2, direction=R, max_bond=16, cutoff:1e-06
100%|█████████████████████████████████████████| 300/300 [00:08<00:00, 35.99it/s]
Energy: -132.87012141240945 ... not converged.
SWEEP-3, direction=R, max_bond=32, cutoff:1e-06
100%|█████████████████████████████████████████| 300/300 [00:14<00:00, 20.87it/s]
Energy: -132.93208452042478 ... not converged.
SWEEP-4, direction=R, max_bond=64, cutoff:1e-06
100%|█████████████████████████████████████████| 300/300 [01:50<00:00,  2.72it/s]
Energy: -132.94517155166892 ... not converged.
[8]:
False

We are getting pretty close to the known energy already (closer than OBC at this length can get). The relative error is:

[9]:
(dmrg.energy - E_exact) / abs(E_exact)
[9]:
1.3010558783268665e-05

Note that for PBC, the algorithm splits the chain into segments, and approximates the other segments with a SVD (the accuracies of the energies above are limited by this). Thus progress appears to pause at these points. The number of singular values kept for this environment approximation is recorded in dmrg.bond_sizes_ham and dmrg.bond_sizes_norm:

[10]:
dmrg.bond_sizes_norm
[10]:
[[1, 1], [1, 2], [1, 5], [4, 12]]
[11]:
dmrg.bond_sizes_ham
[11]:
[[2, 2], [2, 3], [3, 5], [7, 17]]

To progress further might require tweaking the advanced options, for example, setting tighter tolerances for some of the settings found in:

[12]:
dmrg.opts
[12]:
{'default_sweep_sequence': 'R',
 'bond_compress_method': 'svd',
 'bond_compress_cutoff_mode': 'rel',
 'bond_expand_rand_strength': 1e-06,
 'local_eig_tol': 0.001,
 'local_eig_ncv': 4,
 'local_eig_backend': None,
 'local_eig_maxiter': None,
 'local_eig_EPSType': None,
 'local_eig_ham_dense': None,
 'local_eig_norm_dense': None,
 'periodic_segment_size': 0.5,
 'periodic_compress_method': 'isvd',
 'periodic_compress_norm_eps': 1e-06,
 'periodic_compress_ham_eps': 1e-06,
 'periodic_compress_max_bond': -1,
 'periodic_nullspace_fudge_factor': 1e-12,
 'periodic_canonize_inv_tol': 1e-10,
 'periodic_orthog_tol': 1e-06}

See quimb.tensor.tensor_dmrg.get_default_opts for detailed explanations of these quantities. One could also supply custom sequences for the maximum allowed bond dimensions (e.g. dmrg.solve(..., bond_dims=[70, 80, 90])) or bond compression cutoffs (e.g. dmrg.solve(..., cutoffs=[1e-9, 3e-10, 1e-10])).

PBC DMRG error is, in particular, limited by the segment compression tolerances.

The full state can be retrieved from dmrg.state:

[13]:
gs = dmrg.state
gs.max_bond()
[13]:
64

Z-Correlations

We could then calculate the ground-state z-correlations for example. MatrixProductState.correlation internally uses quimb.tensor.expect_TN_1D which can perform transfer matrix compression in order to efficiently compute expectations.

[14]:
sz = spin_operator('Z').real
gs.correlation(sz, 0, 1)
[14]:
-0.143491884670796
[15]:
%debug
ERROR:root:No traceback has been produced, nothing to debug.

However, if one was computing this for many sites, it would make sense to manually reuse parts of each contraction. For example, if we are only interested in the first n sites, we can approximate the rest with an SVD:

[16]:
# Set up an overlap
p = dmrg.state
p.add_tag('KET')
q = p.H.retag({'KET': 'BRA'})
qp = q & p

# Replace all but 20 sites with an SVD
qp.replace_section_with_svd(20, 300, eps=1e-6, inplace=True, ltags='L', rtags='R')

qp.graph(color=['BRA', 'KET', 'L', 'R'])
../_images/examples_ex_dmrg_periodic_28_0.png

Now we can define a correlation function on this much smaller network:

[17]:
def sz_corr(i, j):
    itag = f"I{i}"
    jtag = f"I{j}"

    qp_i = qp.insert_operator(sz, ('KET', itag), ('BRA', itag))
    c_i = qp_i ^ all

    qp_j = qp.insert_operator(sz, ('KET', jtag), ('BRA', jtag))
    c_j = qp_j ^ all

    qp_ij = qp_i.insert_operator(sz, ('KET', jtag), ('BRA', jtag))
    c_ij = qp_ij ^ all

    return c_ij - c_i * c_j

We can then use this to compute the 20 correlations efficiently:

[18]:
js = range(1, 20)
cs = [sz_corr(0, j) for j in js]
[19]:
import matplotlib.pyplot as plt
plt.plot(js, cs)
[19]:
[<matplotlib.lines.Line2D at 0x7fd19c8294e0>]
../_images/examples_ex_dmrg_periodic_33_1.png

Which looks as expected.

Compressed Density Matrix

For operators on more than a few qubits we can compute a compressed density matrix. E.g. for 50 + 50 = 100 qubits:

[20]:
sysa = range(0, 50)
sysb = range(50, 100)

rho_ab = gs.partial_trace_compress(sysa, sysb, max_bond=2**6, method='isvd')
[21]:
rho_ab.ind_sizes()
[21]:
{'_2a2618000040f': 64,
 '_2a2618000043D': 64,
 '_2a2618000043d': 64,
 '_2a26180008706': 64,
 '_2a26180008749': 64,
 '_2a261800086DD': 64,
 '_2a2618000898b': 11,
 'kB': 64,
 'bA': 64,
 'bB': 64,
 'kA': 64}

Let’s plot this:

[22]:
# specify some coordinates to plot the remaining tensors
fix = {('_UP', '_SYSA'): (-1, +1), ('_DOWN', '_SYSA'): (-1, -1), 'kA': (-1, 1.5), 'bA': (-1, -1.5),
       ('_UP', '_SYSB'): (+1, +1), ('_DOWN', '_SYSB'): (+1, -1), 'kB': (+1, 1.5), 'bB': (+1, -1.5)}
[23]:
rho_ab.graph(color=['_SYSA', '_ENVR', '_SYSB'], show_inds=False, fix=fix)
../_images/examples_ex_dmrg_periodic_40_0.png

You can see that because the state has PBC, there is a split ‘environment’ tensor carrying correlations the ‘long-way-round’.

We can also check it’s still normalized:

[24]:
rho_ab.trace(['kA', 'kB'], ['bA', 'bB'])
[24]:
0.9999999999999977

We could also estimate the genuine entanglement between the two subsytems. First we convert the compressed representation into a dense matrix, whilst also partially transposing one side:

[25]:
# form single tensor
rho_ab_d = rho_ab ^ all

# turn tensor into a normal array whilst also partially transposing
rho_ab_pt_d = rho_ab_d.to_dense(['kA', 'bB'],
                                ['bA', 'kB'])
rho_ab_pt_d.shape
[25]:
(4096, 4096)

Finally compute \(\log_2 \left|\rho_{AB}^{T_B} \right|\):

[26]:
E = log2(sum(abs(eigvalsh(rho_ab_pt_d))))

Which gives the logarithmic negativity between the two regions as (approximately because of the limited bond in the compression):

[27]:
E
[27]:
2.3205174047880464