Python For Quantum Mechanics#

Week 3: Exercises#

Exercise 1: Matrix Class#

Create a square matrix class. Overload the operators +, -, and * so basic matrix operations can be easily performed. Also make use of __getitem__() and __setitem__() to extract matrix elements and to assign values to matrix elements. Use __str__() to print the matrix. Make sure it works for right and left multiplication. Make a transpose and conjugate transpose method as well as one that checks if the matrix is symmetric and hermitian.

class Matrix:
    
  Cell In[1], line 2
    
    ^
SyntaxError: incomplete input

Exercise 2: Quibit Gates Again!!!#

To illustrate the usefulness of the class we just created, define gates X,Z and H then show $\( HXH=Z. \)$ Also fee free to try any other identity you might think of.

X = Matrix([[0,1],[1,0]])
Z = Matrix([[1,0],[0,-1]])
H = (2**(-0.5)) * Matrix([[1,1],[1,-1]])

Now check if these gates are Hermitian. Print the answers.

print(X.is_hermitian())
print(Z.is_hermitian())
print(H.is_hermitian())