Tutorial#

Excercise 1: Python as a Calculator#

Let us introduce the following constants

\(h \approx 6.62607015 \times 10^{-34}\) J/Hz

\(c = 299792458\) m/s

\(\pi \approx 3.14159265\)

\(\hbar = \frac{h}{2\pi}\)

\(e \approx 1.60217663 \times 10^{-19} \) C

\(m_e \approx 9.10938370 \times 10^{-31}\) kg

\(\varepsilon_0 \approx 8.85418781\times 10^{-12}\) F/m

two dice

Save them in python as variables

Put them in a data structure.

Think about what might be best…

Check that this in fact works.

Use these to calculate the fine structure constant, $\(\alpha = \frac{1}{4 \pi \varepsilon_0} \frac{e^2}{\hbar c}\)$

Exercise 2: Nested Lists and Matrices#

Take the matrix

\[\begin{split}A = \begin{bmatrix} 3.14 & 42 \\ 2.71&11 \end{bmatrix}\end{split}\]

Using nested lists as a matrix representation, find and print it’s determinant, then find and print it’s inverse.

Exercise 3: What is True and/or False?#

Discuss and think about the following statements and see if you can predict what will happen.

First we will start with casting numbers to Boolean values.

  • bool(0)

  • bool(1)

  • bool(0.0)

  • bool(1.0)

  • bool(24)

  • bool(-76.2)

We can do the same thing with strings.

  • bool('')

  • bool('hello')

We can cast data structures into Boolean.

  • bool([])

  • bool({})

  • bool()

  • bool((1))

  • bool([5,6,1])

  • bool({'hello',2.5,9})

  • bool({})

Can we cast None?

  • bool(None)

When we use not it casts the proceeding value into boolean.

  • not True

  • not False

  • not 1

  • not 0.0

  • not 72

  • not -17.3

  • not ''

  • not "hi"

  • not ()

  • not None

And we can use two nots

  • not not True

  • not not False

  • not not {2,4}

We will move on and look at and.

  • True and True

  • True and False

  • False and True

  • False and False

But what happens when we using things that aren’t Boolean?

  • False and 'hello'

  • 'hello' and False

  • 'hello' and True

  • True and 'hello'

That’s a little strange… Lets see what else we can do!

  • None and {}

  • '' and True

  • 25 and {25}

  • {0} and 0

Lets try with or.

  • None or {}

  • '' or True

  • 25 or {25}

  • {0} or 0

Finally, we can also play with operator presidence.

  • not (True or False) or 'hello'

  • 'first' and 'second' or False

  • False or not None and 1+0*2-1

  • (0) or not 0

  • (0,) or not 0

Exercise 4: Periodic Function#

Observe the following function, used in the famous Shor’s algorithm.

\(f(x) = a^x mod N\)

This function is periodic, with \(r\) being the smallest(non-zero) integer such that

\(a^r mod N = 1\)

Let \(a=2\) and \(N=31\).

Create a list that contains the values of this function for \(x=0,1,...,10\). two dice

List = [] #Creating an empty list

List.append((2**0)%31)
List.append((2**1)%31)
List.append((2**2)%31)
List.append((2**3)%31)
List.append((2**4)%31)
List.append((2**5)%31)
List.append((2**6)%31)
List.append((2**7)%31)
List.append((2**8)%31)
List.append((2**9)%31)
List.append((2**10)%31)

print(List)
[1, 2, 4, 8, 16, 1, 2, 4, 8, 16, 1]

Using this list or otherwise, observe and print the value of r.

Now convert this list to a set and print it to observe the values taken by the function in a single period.