
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/QuantumIsing_2d_groundstate.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_QuantumIsing_2d_groundstate.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_QuantumIsing_2d_groundstate.py:


Calculate the ground state of a 2d quantum Ising model.
=======================================================

Simple example on how to set up a Quantum ising 2-dimensional ground
state search

.. GENERATED FROM PYTHON SOURCE LINES 18-27

.. code-block:: Python


    # pylint: disable=invalid-name

    import numpy as np
    import numpy.linalg as nla

    import qtealeaves as qtl
    from qtealeaves.models import get_quantum_ising_2d


.. GENERATED FROM PYTHON SOURCE LINES 28-31

We first define a simple function that returns thr TRUE ground state energy
value previously computed. In this way, we can be sure the final result will
be correct

.. GENERATED FROM PYTHON SOURCE LINES 31-41

.. code-block:: Python



    def get_osmps_reference():
        """
        Return ground state reference value from OSMPS simulation.
        """
        ref_osmps = -24.710085956694
        return ref_osmps



.. GENERATED FROM PYTHON SOURCE LINES 42-43

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

.. GENERATED FROM PYTHON SOURCE LINES 43-149

.. 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 python-TTN, 6 for python-MPS.
            Default to 5.

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

        ###############################################################################
        # Defining the model, in this case, is very simple, since it is one of the
        # already available models of the library. However, defining a model is simple
        # even starting from scratch! For an example see :docs:`other_example` or the
        # code of :func:`get_quantum_ising_2d`

        model, my_ops = get_quantum_ising_2d()

        ###############################################################################
        # 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
        if output_folder is None:
            output_folder = lambda params: "QI2d_L%d" % (params["L"])

        ###############################################################################
        # 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.

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

        ###############################################################################
        # Define the simulation instance

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

        ###############################################################################
        # Define the parameters of the models: here we define the 'L' seen in the
        # I/O definition at the beginning! Instead 'J' and 'g' are important model
        # parameters

        params = [
            {
                "L": 4,
                # model parameters
                "J": 1.0,
                "g": 0.5,
            }
        ]

        ###############################################################################
        # We finally run the simulation, checking that everything is ok

        simulation.run(params, delete_existing_folder=True)

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

            if elem["L"] == 2:
                ed_energy_0, _ = nla.eigh(model.build_ham(my_ops, elem))[0][:2]
                print("ED vs TN ground state energy", ed_energy_0, tn_energy_0)

                assert np.abs(tn_energy_0 - ed_energy_0) < 1e-4
                msg_check = "ground state energy vs ED at least correct up to 1e-4."

            elif elem["L"] == 4:
                # Can compare to OSMPS
                ref_osmps = get_osmps_reference()
                print("QTea vs OSMPS ground state energy", tn_energy_0, ref_osmps)

                assert np.abs(tn_energy_0 - ref_osmps) < 1e-4
                msg_check = "ground state energy vs OSMPS at least correct up to 1e-4."

            else:
                print("Ground state energy", tn_energy_0)
                msg_check = "No checks implemented for this simulation."

        print(f"\nExample `{__file__}` ran successfully; " + msg_check)

        return



.. GENERATED FROM PYTHON SOURCE LINES 150-151

Run the code

.. GENERATED FROM PYTHON SOURCE LINES 151-154

.. code-block:: Python


    if __name__ == "__main__":
        main()


.. _sphx_glr_download_auto_examples_QuantumIsing_2d_groundstate.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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