# Correlation measurements in the directed loop and worm codes

In this tutorial we will look at measuring correlation functions in the directed loop and worm codes.

## Two-dimensional Heisenberg square lattice

### Preparing and running the simulation from the command line

The parameter file parm4 sets up Monte Carlo simulations of the quantum mechanical S=1/2 Heisenberg model on a square lattice and turns various measurement options on

```MODEL="spin";
LATTICE="square lattice";
REPRESENTATION="SSE";
MEASURE[Correlations]=true;
MEASURE[Structure Factor]=true;
MEASURE[Green Function]=true;
THERMALIZATION=10000;
SWEEPS=500000;
J= 1;
L=4;
W=4;
T=0.3;
{h=0.1;}
```

Using the standard sequence of commands you can run the simulation using the quantum SSE code and then look at the results in the XML output files

```parameter2xml parm4
dirloop_sse --Tmin 10 --write-xml parm4.in.xml
```

### Preparing and running the simulation using Python

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

```import pyalps
```
```parms = [{
'LATTICE'                   : "square lattice",
'MODEL'                     : "spin",
'MEASURE[Correlations]'     : True,
'MEASURE[Structure Factor]' : True,
'MEASURE[Green Function]'   : True,
'local_S'                   : 0.5,
'T'                         : 0.3,
'J'                         : 1 ,
'THERMALIZATION'            : 10000,
'SWEEPS'                    : 500000,
'L'                         : 4,
'h'                         : 0.1
}]
```
```input_file = pyalps.writeInputFiles('parm4',parms)
res = pyalps.runApplication('dirloop_sse',input_file,Tmin=5)
```

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.

### Evaluating the simulation and preparing plots using Python

To look at the results we now load ALL measurements from output files starting with parm4. The script is again in tutorial4.py

```data = pyalps.loadMeasurements(pyalps.getResultFiles())
```

We now loop through all measurements and print them:

```for s in pyalps.flatten(data):
if len(s.x)==1:
print s.props['observable'], ' : ', s.y
else:
for (x,y) in zip(s.x,s.y):
print  s.props['observable'], x, ' : ', y
```

The if statement checks whether the measured quantity is a scalar or vector-valued quantity.

### Setting up and running the simulation in Vistrails

To run the simulation in Vistrails open the file mc-04-measurements.vt.