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

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

.. _sphx_glr_auto_examples_Parallel_Sampling.py:


Ground state simulation of the quantum Ising model.
===================================================

.. GENERATED FROM PYTHON SOURCE LINES 16-102

.. code-block:: Python


    # pylint: disable=invalid-name

    # pylint: disable-next=import-error
    from mpi4py import MPI

    import qtealeaves as qtl
    from qtealeaves.emulator import TTN
    from qtealeaves.models import get_quantum_ising_1d
    from qtealeaves.tensors import TensorBackend

    # pylint: disable-next=c-extension-no-member
    comm = MPI.COMM_WORLD


    def groundstate(tn_type=5):
        """
        Main method for the ground state simulation of 1d quantum
        Ising model.

        **Arguments**

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

        """
        output_folder = lambda params: "QI1d_L%d" % (params["L"])

        model, my_ops = get_quantum_ising_1d()

        my_conv = qtl.convergence_parameters.TNConvergenceParameters(
            max_iter=7, max_bond_dimension=20
        )

        my_obs = qtl.observables.TNObservables()
        my_obs += qtl.observables.State2File("QI1d/psi", "U")

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

        params = []

        params.append({"L": 8, "J": 1.0, "g": 0.5})
        simulation.run(params, delete_existing_folder=True)

        return TTN.read("QI1d/psi.pklttn", TensorBackend())


    def main(last_bit="0"):
        """Calculate groundstate on MPI-thread 0 and then sample in parallel."""
        if comm.Get_rank() == 0:
            psi = groundstate()
        else:
            psi = None

        comm.Barrier()

        py_tensor_backend = TensorBackend()

        # pylint: disable-next=unused-argument
        def filter_func(state, interval, last_bit=last_bit):
            return state[-1] == last_bit

        samples = TTN.mpi_sample_n_unique_states(
            psi,
            100,
            comm,
            py_tensor_backend,
            filter_func=filter_func,
            mpi_final_op="mpi_all_gather",
        )

        print("Rank & samples", comm.Get_rank(), len(samples))


    if __name__ == "__main__":
        main()


.. _sphx_glr_download_auto_examples_Parallel_Sampling.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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