Getting Started#

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 need a current version of Python 3. You may install it locally or use a cloud environment such as Google Colab. Local installation is recommended for longer simulations and for working with files.

Python#

Install Python 3 and verify that it is available in a terminal:

python3 --version

Create a separate virtual environment for this course, activate it, and install the packages used in the notebooks:

python3 -m venv phys446-env
source phys446-env/bin/activate  # macOS/Linux
python -m pip install --upgrade pip
python -m pip install numpy scipy matplotlib jupyter

On Windows PowerShell, activate the environment with phys446-env\Scripts\Activate.ps1 instead. If you prefer a graphical Python distribution, Anaconda or Miniconda are also reasonable choices; use a current Python 3 environment.

C++#

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

Linux#

Check whether the GNU C++ compiler (g++) is installed by typing:

g++ --version

If it is not installed, use your distribution’s package manager to install a current C++ compiler.

Mac#

Install Apple’s Command Line Tools, then verify that a compiler is available:

g++ --version

If the command is unavailable, run xcode-select --install and follow the prompts.

Windows#

The simplest option is usually Visual Studio Community with the Desktop development with C++ workload. You can also use Windows Subsystem for Linux and install g++ in your Linux environment.

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

python3 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++17 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++17 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

python3 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. One useful tool is version control. If you mess up something in 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 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 versions)