Getting Setup#

Getting started with python and C++#

In this course, you’re going to be doing a series of four major computational projects (and one warmup). These computational projects can be done in various languages. Historically the languages we have required have been

Historically

  • Project 0: Cellular Automata -> C++ and Python

  • Project 1: Quantum Computing -> Python

  • Project 2: Renormalization Group -> C++

  • Project 3: Machine Learning -> C++

  • Project 4: Topological Insulators -> Python

This year we are going to give a somewhat more relaxed option for what programming language you want to use allowing some mix of python, C++, and Julia. This gives you more opportunity to master the set of languages that you want while building a series of really cool computational projects. In addition, since this course was developed much of machine learning has transitioned from C++ to python and so it doesn’t make as much sense to require it to be done in C++. For each project, we will let you know what languages are recommended, which are supported, and which are options. It should be noted that if you don’t choose one of the recommended or supported languages, we may not be as helpful and you will be more on your own.

Language Options this year

  • Project 0: Cellular Automata

    • Python (recommended)

    • C++ with python (recommended)

  • Project 1: Quantum Computing

    • Python (recommended)

    • Julia (reasonable choice but we won’t help you much with syntax)

  • Project 2: Renormalization Group

    • Python (recommended)

    • C++ (recommended)

    • Julia (reasonable choice but we won’t help you much with syntax)

  • Project 3: Machine Learning

    • Python (recommended)

    • C++ (partially supported)

  • Project 4: Topological Insulators -> Python

    • Python (recommended)

    • Julia (reasonable choice but we won’t help you much with syntax)


Installation#

For this course, you will (at minimum) need to be prepared to run python. This can either be done by installing it on your machine or using it somewhere in the cloud.

There are really two approaches to using python. One of them is through a jupyter notebook. The other is to use an editor and then just run python on the command line. For installing python locally I recommend Anaconda (details below.) If you want to run your code in the cloud and want to use jupyter notebooks, you can use google colab. If you want to work on an IDE and run it through the cloud, one option is repl.it (which has a free tier). The environment there is nice but I’m not sure it’s going to be fast enough to run the simulations we need to run.

Python (on your machine through Anaconda)#

A convenient package of python libraries, which includes important modules like numpy, scipy, and matplotlib, is Anaconda. We recommend you install Anaconda on your machine, so that you have all these necessary packages. Make sure that you get the right version for your machine. Check if your machine is 32-bit or 64-bit and use the corresponding installer. Please use python 3.xx Python has two version (2.7 and 3.6). For what we are doing it probably doesn’t matter much which version you use but, all other things being equal, you should probably use the newer version (3.6) as this will be most useful to learn.

Linux#

To install Anaconda for Linux, follow the instructions listed here.

Mac#

To install Anaconda for Mac, follow the instructions listed here.

Windows#

To install Anaconda for Windows, follow the instructions listed here.

C++#

To turn your C++ code into an executable program, you need a C++ compiler.

Linux#

All Linux distributions should have the standard C++ compiler, the GNU C++ compiler called gcc or g++, already installed. So no extra work for you! To check if it really is installed, type into your terminal

g++ --version

which should show you if you have c++ and what version it is. You probably want to be around version 6.0

Mac#

You essentially have two options. One option is to use the compiler already on your machine. Again type

g++ --version

and make sure it’s there. Another option is to install macports (where you can install more recent compilers, etc.) See here if you want to do that (this is typically what I do on my macs). (The command to run once you have macports installed is sudo port install gcc8.

Windows#

There are various options. One possibility (if you have the right version of windows) is to install ubuntu on windows (see here) and then use

sudo apt-get install [compiler name]

to install the compilers and git, etc. Another possibility is to use the free version of visual studio.

SSH into the engineering workstations.#

If you don’t want to (or have trouble) installing things locally, one option is to “SSH” (secure shell) into the University-provided Linux engineering workstations. These are Linux machines that come with gcc, as well as python, already installed. To access these resources, you need to be able to ssh into netid@linux.ews.illinois.edu, where you replace netid with your specific University net ID.

One approach is to connect through your web browser using fastx (see here)

Another standard SSH program (for Windows) is PuTTY. You can download it here. It is pretty straightforward to use, but just in case some installation and usage instructions can be found here. When you login to netid@linux.ews.illinois.edu, you will be prompted to accept an SSH key and then you will need to enter a password. That password is your current password associated with your Illinois net ID.


Editors#

There are various editors that you can use. These include (but are not limited to)

  • Visual Studio Code (free and here ) (recommended)

  • Pycharm (available free through the university) (recommended)

  • Spyder (comes with Anaconda)

  • emacs (free)

  • Sublime (?free for preview? and here )


Running your code#

Python#

Working in python often consists of writing simple “scripts”, such as script.py. To execute this script, you simply type in

python script.py

and python does its magic.

C++#

Working in C++ is typically more involved and requires writing a collection of “header” (“.h”) and “source” (“.cpp” or “.cc”) files. Say you wrote the files main.cpp, helper.cpp, importantheader.h, otherheader.h as part of your assignment. The source files reference the header files by the #include syntax. To compile your code, type

g++ -O3 -std=c++11 main.cpp helper.cpp -o myExecutable

This creates an executable titled myExecutable, which you can then run in Linux with the command ./myExecutable.

Example code#

At these two links, C++ example and python example, there are two examples of a simple code that computes the Fibonacci numbers and prints them to the command line. There is a version written in C++ called printFibonacci.cc and a python version called printFibonacci.py. To compile the C++ version, you need to execute

g++ -O3 -std=c++11 printFibonacci.cc -o printFibonacci

Then you can run the newly created printFibonacci executable by typing the command

./printFibonacci 20

which should then print the first 20 Fibonacci numbers.

The python script you can run by typing the command

python printFibonacci.py 20

which should produce exactly the same output. Make sure you can get these two examples working.


Code references#

The above information was intentionally very minimal on details. We will not spend much time on introducing you to the syntax of python, C++, or any other language you might want to use. We hope that you will look it up as you need to accomplish the projects. To help with that, we provide here a few python and C++ “cheat sheets” and tutorials that you might find useful. Such documents are often very handy and can help you remember specific commands that might be useful for the task at hand.

Python#

  • A good tutorial from google on python: tutorial

  • A scientific python cheat sheet on basic python, ipython, numpy, scipy, and matplotlib.

  • The official, long, and really comprehensive python tutorial.

  • The official quick start to numpy and scipy.

  • Matplotlib’s official pyplot tutorial.

C++#

  • A C++ cheat sheet.

  • A comprehensive C++ tutorial. The first few sections are very much worth going over if you are not familiar with the language.

  • Another lengthy C++ tutorial website.

General#

  • Google is your friend. Don’t be shy to search for specific coding questions.


Good software practice#

Throughout this course you will learn various techniques for good software practice and software techniques. One useful ting to use is how to do good version control. If you mess up something on your code, you want to be able to go back and see what you’ve done. To do that you need to keep regular snapshots. The best way to go about this is using git. You can get it on your computer and go through the 15 min. tutorial. You may want to use it regularly.

One time Commands:

  • git init (starts your repository)

  • git add myFile (adds a file to your repository)

Frequent Commands

  • git commit -a -m “Message about what you changed” (commits your changes)

  • git add myFile (adds a file to be committed)

  • git status (shows you which files are not yet committed)

  • git log (see the different versions of your code)

  • git diff hash1 hash2 (see the difference between your verions)