# SIMPLE DMRG¶

Source code: https://github.com/simple-dmrg/simple-dmrg/

The goal of this tutorial (given at the 2013 summer school on quantum spin liquids, in Trieste, Italy) is to present the density-matrix renormalization group (DMRG) in its traditional formulation (i.e. without using matrix product states). DMRG is a numerical method that allows for the efficient simulation of quantum model Hamiltonians. Since it is a low-entanglement approximation, it often works quite well for one-dimensional systems, giving results that are nearly exact.

Typical implementations of DMRG in C++ or Fortran can be tens of thousands of lines long. Here, we have attempted to strike a balance between clear, simple code, and including many features and optimizations that would exist in a production code. One thing that helps with this is the use of Python. We have tried to write the code in a very explicit style, hoping that it will be (mostly) understandable to somebody new to Python. (See also the included Python cheatsheet, which lists many of the Python features used by simple-dmrg, and which should be helpful when trying the included exercises.)

The four modules build up DMRG from its simplest implementation to more complex implementations and optimizations. Each file adds lines of code and complexity compared with the previous version.

1. Infinite system algorithm (~180 lines, including comments)
2. Finite system algorithm (~240 lines)
3. Conserved quantum numbers (~310 lines)
4. Eigenstate prediction (~370 lines)

Throughout the tutorial, we focus on the spin-1/2 Heisenberg XXZ model, but the code could easily be modified (or expanded) to work with other models.

## Authors¶

• James R. Garrison (UCSB)
• Ryan V. Mishmash (UCSB)

## Contents¶

### Using the code¶

The requirements are:

$wget -O simple-dmrg-master.zip https://github.com/simple-dmrg/simple-dmrg/archive/master.zip  Within a terminal, execute the following to unpack the code: $ unzip simple-dmrg-master.zip
$cd simple-dmrg-master/  Once the relevant software is installed, each program is contained entirely in a single file. The first program, for instance, can be run by issuing: $ python simple_dmrg_01_infinite_system.py


Note

If you see an error that looks like this:

SyntaxError: future feature print_function is not defined


then you are using a version of Python below 2.6. Although it would be best to upgrade, it may be possible to make the code work on Python versions below 2.6 without much trouble.

### Exercises¶

#### Day 1¶

1. Consider a reduced density matrix corresponding to a maximally mixed state in a Hilbert space of dimension . Compute the truncation error associated with keeping only the largest m eigenvectors of . Fortunately, the reduced density matrix eigenvalues for ground states of local Hamiltonians decay much more quickly!

2. Explore computing the ground state energy of the Heisenberg model using the infinite system algorithm. The exact Bethe ansatz result in the thermodynamic limit is . Note the respectable accuracy obtained with an extremely small block basis of size . Why does the DMRG work so well in this case?

3. Entanglement entropy:

1. Calculate the bipartite (von Neumann) entanglement entropy at the center of the chain during the infinite system algorithm. How does it scale with ?

2. Now, using the finite system algorithm, calculate the bipartite entanglement entropy for every bipartite splitting. How does it scale with subsystem size ?

Hint

To create a simple plot in python:

>>> from matplotlib import pyplot as plt
>>> x_values = [1, 2, 3, 4]
>>> y_values = [4, 2, 7, 3]
>>> plt.plot(x_values, y_values)
>>> plt.show()

3. From the above, estimate the central charge of the “Bethe phase” (1D quasi-long-range Néel phase) of the 1D Heisenberg model, and in light of that, think again about your answer to the last part of exercise 2.

The formula for fitting the central charge on a system with open boundary conditions is: where is the von Neumann entropy.

Hint

To fit a line in python:

>>> x_values = [1, 2, 3, 4]
>>> y_values = [-4, -2, 0, 2]
>>> slope, y_intercept = np.polyfit(x_values, y_values, 1)

4. XXZ model:

1. Change the code (ever so slightly) to accommodate spin-exchange anisotropy: .
2. For ( ), the ground state is known to be an Ising antiferromagnet (ferromagnet), and thus fully gapped. Verify this by investigating scaling of the entanglement entropy as in exercise 3. What do we expect for the central charge in this case?

#### Day 2¶

1. Using simple_dmrg_03_conserved_quantum_numbers.py, calculate the “spin gap” . How does the gap scale with ? Think about how you would go about computing the spectral gap in the sector: , i.e., the gap between the ground state and first excited state within the sector.

2. Calculate the total weight of each sector in the enlarged system block after constructing each block of . At this point, it’s important to fully understand why is indeed block diagonal, with blocks labeled by the total quantum number for the enlarged system block.

3. Starting with simple_dmrg_02_finite_system.py, implement a spin-spin correlation function measurement of the free two sites at each step in the finite system algorithm, i.e., calculate for all . In exercise 3 of yesterday’s tutorial, you should have noticed a strong period-2 oscillatory component of the entanglement entropy. With your measurement of , can you now explain this on physical grounds?

Answer: finite_system_algorithm(L=20, m_warmup=10, m_sweep_list=[10, 20, 30, 40, 40]) with should give on the last step.

4. Implement the “ring term” . Note that this term is one of the pieces of the SU(2)-invariant four-site ring-exchange operator for sites ( , , , ), a term which is known to drive the - Heisenberg model on the two-leg triangular strip into a quasi-1D descendant of the spinon Fermi sea (“spin Bose metal”) spin liquid [see http://arxiv.org/abs/0902.4210].

Answer: finite_system_algorithm(L=20, m_warmup=10, m_sweep_list=[10, 20, 30, 40, 40]) with , should give .

### Python cheatsheet¶

[designed specifically for understanding and modifying simple-dmrg]

For a programmer, the standard, online Python tutorial is quite nice. Below, we try to mention a few things so that you can get acquainted with the simple-dmrg code as quickly as possible.

Python includes a few powerful internal data structures (lists, tuples, and dictionaries), and we use numpy (numeric python) and scipy (additional “scientific” python routines) for linear algebra.

#### Basics¶

Unlike many languages where blocks are denoted by braces or special end statements, blocks in python are denoted by indentation level. Thus indentation and whitespace are significant in a python program.

It is possible to execute python directly from the commandline:

$python  This will bring you into python’s real-eval-print loop (REPL). From here, you can experiment with various commands and expressions. The examples below are taken from the REPL, and include the prompts (“>>>” and “...”) one would see there. #### Lists, tuples, and loops¶ The basic sequence data types in python are lists and tuples. A list can be constructed literally: >>> x_list = [2, 3, 5, 7]  and a number of operations can be performed on it: >>> len(x_list) 4 >>> x_list.append(11) >>> x_list [2, 3, 5, 7, 11] >>> x_list 2 >>> x_list = 0 >>> x_list [0, 3, 5, 7, 11]  Note, in particular, that python uses indices counting from zero, like C (but unlike Fortran and Matlab). A tuple in python acts very similarly to a list, but once it is constructed it cannot be modified. It is constructed using parentheses instead of brackets: >>> x_tuple = (2, 3, 5, 7)  Lists and tuples can contain any data type, and the data type of the elements need not be consistent: >>> x = ["hello", 4, 8, (23, 12)]  It is also possible to get a subset of a list (e.g. the first three elements) by using Python’s slice notation: >>> x = [2, 3, 5, 7, 11] >>> x[:3] [2, 3, 5]  ##### Looping over lists and tuples¶ Looping over a list or tuple is quite straightforward: >>> x_list = [5, 7, 9, 11] >>> for x in x_list: ... print(x) ... 5 7 9 11  If you wish to have the corresponding indices for each element of the list, the enumerate() function will provide this: >>> x_list = [5, 7, 9, 11] >>> for i, x in enumerate(x_list): ... print(i, x) ... 0 5 1 7 2 9 3 11  If you have two (or more) parallel arrays with the same number of elements and you want to loop over each of them at once, use the zip() function: >>> x_list = [2, 3, 5, 7] >>> y_list = [12, 13, 14, 15] >>> for x, y in zip(x_list, y_list): ... print(x, y) ... 2 12 3 13 5 14 7 15  There is a syntactic shortcut for transforming a list into a new one, known as a list comprehension: >>> primes = [2, 3, 5, 7] >>> doubled_primes = [2 * x for x in primes] >>> doubled_primes [4, 6, 10, 14]  #### Dictionaries¶ Dictionaries are python’s powerful mapping data type. A number, string, or even a tuple can be a key, and any data type can be the corresponding value. Literal construction syntax: >>> d = {2: "two", 3: "three"}  Lookup syntax: >>> d 'two' >>> d 'three'  Modifying (or creating) elements: >>> d = "four" >>> d {2: 'two', 3: 'three', 4: 'four'}  The method get() is another way to lookup an element, but returns the special value None if the key does not exist (instead of raising an error): >>> d.get(2) 'two' >>> d.get(4)  ##### Looping over dictionaries¶ Looping over the keys of a dictionary: >>> d = {2: "two", 3: "three"} >>> for key in d: ... print(key) ... 2 3  Looping over the values of a dictionary: >>> d = {2: "two", 3: "three"} >>> for value in d.values(): ... print(value) ... two three  Looping over the keys and values, together: >>> d = {2: "two", 3: "three"} >>> for key, value in d.items(): ... print(key, value) ... 2 two 3 three  #### Functions¶ Function definition in python uses the def keyword: >>> def f(x): ... y = x + 2 ... return 2 * y + x ...  Function calling uses parentheses, along with any arguments to be passed: >>> f(2) 10 >>> f(3) 13  When calling a function, it is also possibly to specify the arguments by name (e.g. x=4): >>> f(x=4) 16  An alternative syntax for writing a one-line function is to use python’s lambda keyword: >>> g = lambda x: 3 * x >>> g(5) 15  #### numpy arrays¶ numpy provides a multi-dimensional array type. Unlike lists and tuples, numpy arrays have fixed size and hold values of a single data type. This allows the program to perform operations on large arrays very quickly. Literal construction of a 2x2 matrix: >>> np.array([[1, 2], [3, 4]], dtype='d') array([[ 1., 2.], [ 3., 4.]])  Note that dtype='d' specifies that the type of the array should be double-precision (real) floating point. It is also possibly to construct an array of all zeros: >>> np.zeros([3, 4], dtype='d') array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])  And then elements can be added one-by-one: >>> x = np.zeros([3, 4], dtype='d') >>> x[1, 2] = 12 >>> x[1, 3] = 18 >>> x array([[ 0., 0., 0., 0.], [ 0., 0., 12., 18.], [ 0., 0., 0., 0.]])  It is possible to access a given row or column by index: >>> x[1, :] array([ 0., 0., 12., 18.]) >>> x[:, 2] array([ 0., 12., 0.])  or to access multiple columns (or rows) at once: >>> col_indices = [2, 1, 3] >>> x[:, col_indices] array([[ 0., 0., 0.], [ 12., 0., 18.], [ 0., 0., 0.]])  For matrix-vector (or matrix-matrix) multiplication use the np.dot() function: >>> np.dot(m, v)  Warning One tricky thing about numpy arrays is that they do not act as matrices by default. In fact, if you multiply two numpy arrays, python will attempt to multiply them element-wise! To take an inner product, you will need to take the transpose-conjugate of the left vector yourself: >>> np.dot(v1.conjugate().transpose(), v2)  ##### Array storage order¶ Although a numpy array acts as a multi-dimensional object, it is actually stored in memory as a one-dimensional contiguous array. Roughly speaking, the elements can either be stored column-by-column (“column major”, or “Fortran-style”) or row-by-row (“row major”, or “C-style”). As long as we understand the underlying storage order of an array, we can reshape it to have different dimensions. In particular, the logic for taking a partial trace in simple-dmrg uses this reshaping to make the system and environment basis elements correspond to the rows and columns of the matrix, respectively. Then, only a simple matrix multiplication is required to find the reduced density matrix. #### Mathematical constants¶ numpy also provides a variety of mathematical constants: >>> np.pi 3.141592653589793 >>> np.e 2.718281828459045  #### Experimentation and getting help¶ As mentioned above, python’s REPL can be quite useful for experimentation and getting familiar with the language. Another thing we can do is to import the simple-dmrg code directly into the REPL so that we can experiment with it directly. The line: >>> from simple_dmrg_01_infinite_system import *  will execute all lines except the ones within the block that says: if __name__ == "__main__":  So if we want to use the finite system algorithm, we can (assuming our source tree is in the PYTHONPATH, which should typically include the current directory): $ python
>>> from simple_dmrg_04_eigenstate_prediction import *
>>> finite_system_algorithm(L=10, m_warmup=8, m_sweep_list=[8, 8, 8])


It is also possible to get help in the REPL by using python’s built-in help() function on various objects, functions, and types:

>>> help(sum)   # help on python's sum function

>>> help([])    # python list methods
>>> help({})    # python dict methods

>>> help({}.setdefault)   # help on a specific dict method

>>> import numpy as np
>>> help(np.log)          # natural logarithm
>>> help(np.linalg.eigh)  # eigensolver for hermitian matrices