.. _sec_tut_ising_statics: Example: Static Ising Model --------------------------- In the following paragraphs we will see how OSMPS integrates with the python environment in a manner that allows for easy visualization of important quantities. We will begin by analyzing the file *01_IsingStatics.py* given in the *Examples* subdirectory of OSMPS. This file performs simulations for many values of the transverse field coupling :math:`g`, and produces a plot illustrating how the spin correlation between far apart sites changes as a function of the disorder inducing transverse magnetic field. The transverse Ising Hamltonian can be written as shown in Eq. :eq:`IsingStaticsH`, in the file *01_IsingStatics.py* we will understand how to implement in this Hamiltonian in OSMPS, and how we can study some of its ground state properties. .. math:: :label: IsingStaticsH \hat{H} = -J \sum_{i=1}^{L-1} \hat{\sigma}_{i}^{z} \hat{\sigma}_{i+1}^{z} - J g \sum_{i=1}^{L} \hat{\sigma}_{i}^{x} The file begins by importing *MPSPyLib* and *NumPy*, and it also imports *matplotlib.pyplot*. This of course requires the installation of *matplotlib*. .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 1-4 :lineno-start: 1 If we call the file from the command line, the main function will be executed, which takes a logic variable as argument. The default value ``PostProcess=False`` means we have not yet run our simulation. If ``PostProcess=True`` is passed as argument, we have run our simulation and are now using the file to organize and plot our outputs. When calling the script from the command line this can be specified via ``--PostProcess=F`` or ``--PostProcess=T``: .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 7-7 :lineno-start: 7 Here we populate ``Operators`` with the spin operators defined in the description of :py:func:`Ops.BuildSpinOperators`. We use these operators to define the Pauli matrices ``Operator['sigmaz']`` and ``Operator['sigmax']`` using well known operator identities. .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 23-26 :lineno-start: 23 Here we construct the Hamiltonian for the transverse Ising model, the first term couples the z component of spin of nearest neighbor qubits, the second term couples the qubits to a transverse magnetic field, this field tends to disorder the qubits with respect to measurement in the z direction. It is important to note that line 32 implicitly sets :math:`J=1.0`, this will be done explicitly on line 43. .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 28-33 :lineno-start: 28 Here we specify that we are interested in the correlation of the z-component of spin among all qubits in the lattice. We associate this observable with the key ``'zz'``, we will use this key in post processing to obtain the correlation data OSMPS has computed and stored in output dictionaries. .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 35-37 :lineno-start: 35 This line specifies the details of the MPS convergence parameters: .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 39-40 :lineno-start: 39 Here we explicitly set :math:`J=1.0`, so that as we increase the strength of the transverse magnetic field coupling :math:`g`, the ground state of the model will transition from ferromagnetic to paramagnetic. Note that line 44 creates a list of values for the parameter :math:`g`, for each value of :math:`g` in the list ``glist`` another dictionary of parameters is created, and appended to the list ``parameters``. .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 42-65 :lineno-start: 42 As mentioned, the simulations will be carried out if ``PostProcess=False``. .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 72-78 :lineno-start: 72 Alternatively, if ``PostProcess=True`` then we have already run our simulations and are only interested in plotting. This brings us to line 72, where, assuming we have already run our simulations and set ``PostProcess=True``, we pass the PostProcess ``if`` statement. We first initialize the list ``magnetizationlist`` as an empty list. We then use ``ReadStaticObservables`` to store the output dictionaries of our simulations in ``Outputs``. Since each entry in ``Outputs`` is a dictionary, we need to supply the proper key to each dictionary in order to receive the observable data our simulations have generated. We accomplish this with lines 80, for each ``Output`` dictionary in our list ``Outputs``, we supply the key ``'zz'``, returned to us is the corresponding observable, in this case a two dimensional array summarizing the correlations between all sites in the system. On line 80 we pick out the spin-spin correlation between sites 4 and 27, these are arbitrary numbers, chosen only to be separated by many sites, and not directly located at a boundary, and use the square root of this quantity as an estimate of the magnetization of the system. The correlation between these two sites is temporarily stored in ``spincorrelation``, each time we pass through the ``for`` loop we append the new value of ``spincorrelation`` to ``spincorr``. Finally lines 61-68 use the plotting function ``scatter`` from ``matplotlib.pyplot`` to create a plot of the magnetization as a function of the disorder inducing transverse magnetic field, the ``usetex=True`` on line 82 allows us to use the built in *LaTeX* capabilities in ``matplotlib.pyplot``. Note that any text within quotes preceded by ``r``, will be parsed as *LaTeX*. So that if lines 91-102 are to run without error, you must install ``matplotlib``. .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 71-104 :lineno-start: 71 Running this example we receive the following plot as output. .. image:: ./../images/Tut1_Ising_Magnetization.* :width: 600px Alternatively, if you would rather use the plotting capabilities provided by other programs, the data can easily be written out by substituting the following code in place of lines 91-99. The key line in the code below is line 89, this line appends each element of the ``Outputs['zz'][3][26]`` to ``magnetizationlist`` where it is used later to build a scatter plot. A note: ``[3][26]`` is a site-pair specification. It indicates that we are grabbing the two-point correlator for sites ``3``, and ``26`` (where the first site is indexed ``0``). .. literalinclude:: ../../../Examples/01_IsingStatics.py :lines: 83-90 :lineno-start: 83 We note that it is a simple matter to add the ``'unique_ID'`` string to the end of a file name using ``+``, which adds numbers and joins strings.