
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/Models_by_interaction_list.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_Models_by_interaction_list.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_Models_by_interaction_list.py:


Calculate the ground state of a 2d quantum Ising model with local terms added
using CustomSiteTerm.
=======================================================

Simple example on how to set up local terms with CustomSiteTerm

.. GENERATED FROM PYTHON SOURCE LINES 18-24

.. code-block:: Python


    # pylint: disable=invalid-name

    import qtealeaves as qtl
    from qtealeaves import modeling, operators


.. GENERATED FROM PYTHON SOURCE LINES 25-26

We prefer to use a main method to avoid the automatic run when imported

.. GENERATED FROM PYTHON SOURCE LINES 26-43

.. code-block:: Python



    def main(tn_type=5, output_folder=None):
        """
        Main method for the ground state simulation of a
        quantum Ising model in 2D.

        **Arguments**

        tn_type : int, optional
            Choose 5 for TTN, 6 for MPS.
            Default to 5.

        output_folder : str | None, optional
            Output folder. Default to None.
        """


.. GENERATED FROM PYTHON SOURCE LINES 44-45

Defining the 2d model.

.. GENERATED FROM PYTHON SOURCE LINES 45-73

.. code-block:: Python
   :dedent: 1

        dim = 2
        model_name = lambda params: "CustomSite_ex_2d_QIsing_g%2.4f" % (params["g"])
        model = modeling.QuantumModel(dim, "L", name=model_name)
        model += modeling.TwoBodyTerm2D(
            ["sx", "sx"], [0,1], strength="J", prefactor=-1, has_obc=True
        )
        # Here, I add a local term on the site with coordinates [0,0].
        # Being the sytem 2-D, the nested list is required also for local terms.
        model += modeling.CustomSiteTerm("sz",[[0,0]],strength="loc_sz")
        # Here, I add a three-body term on the site with coordinates [0,0], [0,1], [1,0].
        # Being the sytem 2-D, the nested list is required.
        model += modeling.CustomSiteTerm(["sz","sx","sx"],
                                         [[0,0],[0,1],[1,0]],
                                         strength="3_body")
        # Here, I add a local term using a callable that places the term at the center of the lattice.
        local_call = lambda params: [[int(params["L"]/2),int(params["L"]/2)]]
        model += modeling.CustomSiteTerm("sz",local_call, strength="loc_sz")

        # Here, I add an interaction term applying the terms at the four corners of the lattice.
        # I use a callable that takes the size of the system from params.
        interaction_call =  lambda params: [[0,0],
                                            [params["L"]-1,0],
                                            [0,params["L"]-1],
                                            [params["L"]-1,params["L"]-1]]
        model += modeling.CustomSiteTerm(["sz","sz","sz","sz"],
                                         interaction_call,
                                         strength="4_body")
        my_ops = operators.TNSpin12Operators()

.. GENERATED FROM PYTHON SOURCE LINES 74-77

We define **parametric** I/O folder to keep the results more ordered. As you
see, they are parametrized through the size of the chain, i.e. the number
of physical sites of the Tensor network

.. GENERATED FROM PYTHON SOURCE LINES 77-80

.. code-block:: Python
   :dedent: 1

        if output_folder is None:
            output_folder = lambda params: "CustomSite_ex_2d_QIsing_L%d" % (params["L"])


.. GENERATED FROM PYTHON SOURCE LINES 81-89

We define the convergence parameters and the observables: they are really
important:

- The convergence parameters ensure we have a relaiable result. See
  :docs:`/../chapters/convergence` for further informations about them.
- The observables ensure we are measuring (and storing) something at the end
  of the simulation! See :docs:`/../chapters/measurements` for further
  informations about the available observables.

.. GENERATED FROM PYTHON SOURCE LINES 89-95

.. code-block:: Python
   :dedent: 1


        my_conv = qtl.convergence_parameters.TNConvergenceParameters(
            max_iter=5, max_bond_dimension=16
        )
        my_obs = qtl.observables.TNObservables()


.. GENERATED FROM PYTHON SOURCE LINES 96-97

Define the simulation instance

.. GENERATED FROM PYTHON SOURCE LINES 97-108

.. code-block:: Python
   :dedent: 1


        simulation = qtl.QuantumGreenTeaSimulation(
            model,
            my_ops,
            my_conv,
            my_obs,
            tn_type=tn_type,
            folder_name_output=output_folder,
            store_checkpoints=False,
        )


.. GENERATED FROM PYTHON SOURCE LINES 109-113

Define the parameters of the models: here we define the 'L' seen in the
model definition at the beginning! Instead 'J' and 'g' are important model
parameters. We also add terms that are added using the Models_by_interaction_list
term.

.. GENERATED FROM PYTHON SOURCE LINES 113-126

.. code-block:: Python
   :dedent: 1


        params = [
            {
                "L": 4,
                # model parameters
                "J": 1.0,
                "g": 0.5,
                "loc_sz" : 0.3,
                "3_body" : 0.4,
                "4_body" : 0.6
            }
        ]


.. GENERATED FROM PYTHON SOURCE LINES 127-128

We finally run the simulation.

.. GENERATED FROM PYTHON SOURCE LINES 128-139

.. code-block:: Python
   :dedent: 1


        simulation.run(params, delete_existing_folder=True)

        for elem in params:
            tn_energy_0 = simulation.get_static_obs(elem)["energy"]
            print("Ground state energy", tn_energy_0)


        return



.. GENERATED FROM PYTHON SOURCE LINES 140-141

Run the code

.. GENERATED FROM PYTHON SOURCE LINES 141-144

.. code-block:: Python


    if __name__ == "__main__":
        main()


.. _sphx_glr_download_auto_examples_Models_by_interaction_list.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: Models_by_interaction_list.ipynb <Models_by_interaction_list.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: Models_by_interaction_list.py <Models_by_interaction_list.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: Models_by_interaction_list.zip <Models_by_interaction_list.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
