Python
Introductory lectures by J.R. Johansson
W3Schools Tutorial with possibility to run Python code interactively
Quick start
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).
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):
check version of conda: conda --version
udate conda: conda update conda
create environment and intall package: conda create --name environment_name package_name
see the list of environments with the active current environment being marked with asteriks: conda info --envs
create environment with different version of python: conda create --name environment_name python=3.5
verify version of python in the current environment: python --version
activate environment (this worked on my Windows 10): activate environment_name
check if the packges available from Anaconda repository: conda search package_name
install package into the current environment: conda install package_name
get the list of installed packages for current environment: conda list
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:
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:
How to change starting folder for Jupyter
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
Line specification: color, type, markers.
Formatting labels with LaTeX globally: matplotlib.rcParams.update({'font.size': 18, 'text.usetex': True, 'font.family': 'Latin Modern Roman'})
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:
from datetime import datetime
datetime.now().strftime('%Y-%m-%d %H_%M_%S')
Colour blindness palettes recommended for journal publications: viridis, magma, plasma, and inferno. See also colormaps of cmocean package.
Reading and writing text files
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.
f-strings or so-called formatted string literals can be used to format the content of the data in the text file upon writting.
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
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:
Coh S., Vanderbilt D. Python Tight Binding (PythTB) (2010)
D. Moldovan, M. Anelkovi´c, F. Peeters, Pybinding v0.9.4: a Python package for tight-binding calculations (2017). doi:10.5281/zenodo.826942.
Dynamics of open quantum systems
Schrodinger equation with a time-dependent Hamiltonian can be solved by QuTip.
QuTiP installation on Windows:
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).
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)
Create conda-forge channel via Anaconda Prompt: conda config --append channels conda-forge.
Install ‘Community’ edition of Visual Studio 2015
Activate conda environment in Anaconda Prompt: activate qutip-env
Install QuTiP using conda in Anaconda Prompt: conda install qutip
Launch Spyder or Jupyter from set up qutip-env environment
Create a test script:
import qutip.testing as qt
qt.run()
Run the script to test QuTiP