# ALPS 2 Tutorials:ED-01 SparseDiagonalization

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

# Sparse diagonalization

In this tutorial we will learn how to use the sparse diagonalization program using the Lanczos algorithm.

## Measurements on a 1-dimensional Heisenberg chain

### Using the command line

The parameter file parm1a sets up an exact diagonalization of the quantum mechanical S=1 chain with 4 sites:

``` MODEL="spin"
LATTICE="chain lattice"
CONSERVED_QUANTUMNUMBERS="Sz"
MEASURE_STRUCTURE_FACTOR[Structure Factor Sz]=Sz
MEASURE_CORRELATIONS[Diagonal spin correlations]=Sz
MEASURE_CORRELATIONS[Offdiagonal spin correlations]="Splus:Sminus"
local_S=1
J=1
{L=4;}
```

New here compared to other codes are the measurement parameters specifying which operator averages, local values, correlations and structure factors should be measured. More details about these custom measurements are available here

Using the standard sequence of commands you can first convert the input parameters to XML and then run the application sparsediag:

``` parameter2xml parm1a
sparsediag --write-xml parm1a.in.xml
```

The lowest eigenvalues and eigenstates are calculated in each sector (Sz,P) where P denotes the total momentum. The output file parm1.task1.out.xml contains all the computed quantities and can be viewed with a standard internet browser. In our case the ground state lies in the Sz=0, P=0 sector. The corresponding diagonal spin correlations, shown in the XML file, look like

``` Diagonal spin correlations[( 0 ) -- ( 0 )]	(0.666667,0)
Diagonal spin correlations[( 0 ) -- ( 1 )]	(-0.5,0)
Diagonal spin correlations[( 0 ) -- ( 2 )]	(0.333333,0)
Diagonal spin correlations[( 0 ) -- ( 3 )]	(-0.5,0)
```

The numbers [( a ) -- ( b )] in the above brackets refer to site indices, i. e. Sz(a)*Sz(b). In the right column one can read off the (complex) value of the correlation function.

The output for the Sz structure factor for this state looks like

``` Structure Factor Sz[( 0 )]	                5.551115123125783e-17
Structure Factor Sz[( 1.570796326794897 )]	0.333333333333333
Structure Factor Sz[( 3.141592653589793 )]	2
Structure Factor Sz[( 4.71238898038469 )]	0.3333333333333329
```

where the number in the brackets [(q)] denotes the wave number.

One can limit the Sz sector explicitly by adding the following line to the parameter file:

``` Sz_total=0
```

### Using Python

To set up and run the simulation in Python we use the script tutorial1a.py. The first parts of this script imports the required modules, prepares the input files as a list of Python dictionaries, writes the input files and runs the application

```import pyalps
```
```parms = [{
'LATTICE'                   : "chain lattice",
'MODEL'                     : "spin",
'local_S'                   : 1,
'J'                         : 1,
'L'                         : 4,
'CONSERVED_QUANTUMNUMBERS'  : 'Sz',
'MEASURE_STRUCTURE_FACTOR[Structure Factor S]'        : 'Sz',
'MEASURE_CORRELATIONS[Diagonal spin correlations]='   : 'Sz',
'MEASURE_CORRELATIONS[Offdiagonal spin correlations]' : 'Splus:Sminus'
}]
```
```input_file = pyalps.writeInputFiles('parm1a',parms)
res = pyalps.runApplication('sparsediag',input_file)
```

To run this, launch your python interpreter using the convenience scripts alpspython or vispython. We now have the same output files as in the command line version.

We next load the measurements for each of the calculated eigenstates:

```data = pyalps.loadEigenstateMeasurements(pyalps.getResultFiles(prefix='parm1a'))
```

and then print the results just for the ground state:

```for sector in data[0]:
print '\nSector with Sz =', sector[0].props['Sz'],
print 'and k =', sector[0].props['TOTAL_MOMENTUM']
for s in sector:
if pyalps.size(s.y[0])==1:
print s.props['observable'], ' : ', s.y[0]
else:
for (x,y) in zip(s.x,s.y[0]):
print  s.props['observable'], '(', x, ') : ', y
```

### Using Vistrails

To run the simulation in Vistrails open the file ed-01-sparsediag.vt.