Python

Introductory lectures by J.R. Johansson

W3Schools Tutorial with possibility to run Python code interactively

Quick start

    1. Install Anaconda distribution. It contains not only Python itself but also interpreters, packages, package managers (Anaconda Navigator, conda) and integrated development environments (IDEs) (Spyder , Jupyter Notebook).

    2. Getting started using Anaconda Navigator. This also explains how to run Spyder and Jupyter Notebook and create first simple programm of "Hello world" type.

Using conda

conda is a package and environment manager that is controlled via Anaconda Prompt in Windows. Some useful prompt commands for using conda (type it the Anaconda Prompt):

    1. check version of conda: conda --version

    2. udate conda: conda update conda

    3. create environment and intall package: conda create --name environment_name package_name

    4. see the list of environments with the active current environment being marked with asteriks: conda info --envs

    5. create environment with different version of python: conda create --name environment_name python=3.5

    6. verify version of python in the current environment: python --version

    7. activate environment (this worked on my Windows 10): activate environment_name

    8. check if the packges available from Anaconda repository: conda search package_name

    9. install package into the current environment: conda install package_name

    10. get the list of installed packages for current environment: conda list

  1. get the list of set up environments (the current active environment is marked with an asteriks in the list): conda env list

Runing python scripts from Anaconda Prompt

Some useful tips:

    1. Running python scripts in Anaconda Prompt from any folder under Windows requires adding the path to the folder with the python scripts to the Windows environmental variables.

Using Jupyter

Some useful tips:

    1. How to change starting folder for Jupyter

    2. To run Jypyter from a folder that is different from the starting folder (in case you need this once or twice and do not want to change the starting folder), you need to open Anaconda Prompt, then navigate to the desired folder and type in the prompt jupyter-noteboook

Some basic elements of python programing

Making plots with matplotlib

    1. Line specification: color, type, markers.

    2. Formatting labels with LaTeX globally: matplotlib.rcParams.update({'font.size': 18, 'text.usetex': True, 'font.family': 'Latin Modern Roman'})

    3. Scientific research usually produces many figures and data. To keep track of these figures and data files we include 'date string' into their names. In Python this string can be invoked as follows:

    4. from datetime import datetime

    5. datetime.now().strftime('%Y-%m-%d %H_%M_%S')

    6. Colour blindness palettes recommended for journal publications: viridis, magma, plasma, and inferno. See also colormaps of cmocean package.

Reading and writing text files

    1. To open a text file for reading, writing or appendding use open(path,mode), with mode='r','w' or 'a', respectively. Note that path is a string, where directories are separated by forward slashes, therefore on Windows one must be careful with copying and pasting string paths from explorer.

    2. f-strings or so-called formatted string literals can be used to format the content of the data in the text file upon writting.

    3. minimal example: stream.write(f'this text is a f-string that includes number {scipy.pi:.4f}.'), where 'stream' is the object returned by the function open

    4. Note that f is used in front of the string. Curly brakets are used for marking up python expressions within the f-string.

Global optimization problem

For Differential Evolution Method see tutorial by Pablo Rodriguez-Mier and original paper: K. Price and R. Storn, J. Glob. Optim. 341 (1997).

Tight-binding calculations

There are several packages that realize electronic structure calculations in the tight-binding approximation:

Dynamics of open quantum systems

Schrodinger equation with a time-dependent Hamiltonian can be solved by QuTip.

QuTiP installation on Windows:

    1. Install Anaconda since QuTiP is designed to work best when using the Anaconda that supports the conda package management system (see information about conda and Anaconda above).

    2. Set up conda environment using Anaconda Prompt: (a) conda create -n qutip-env; (b) conda install numpy scipy cython matplotlib pytest pytest-cov jupyter notebook spyder (recommended)

    3. Create conda-forge channel via Anaconda Prompt: conda config --append channels conda-forge.

    4. Install ‘Community’ edition of Visual Studio 2015

    5. Activate conda environment in Anaconda Prompt: activate qutip-env

    6. Install QuTiP using conda in Anaconda Prompt: conda install qutip

    7. Launch Spyder or Jupyter from set up qutip-env environment

    8. Create a test script:

    9. import qutip.testing as qt

    10. qt.run()

    11. Run the script to test QuTiP