<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Lab 1 - Vectors and Matrices
This notebook demonstrates the use of vectors and matrices in IPython. Note that the basis is not explicit in any of these operations. You must keep track of the basis yourself (using variable names, or notes etc).
Step1:  Two kinds of vector products we'll see
Step2:  2) Use the function outer(vector1, vector2) to find the outer product of rv and cv. Does the order of the arguments matter?
Step3:  II. Complex vectors
Step4:  The complex conjugate changes the sign of the imaginary part
Step5:  3) Use dot() and .conjugate() to find the dot product of v1 and it's own conjugate
Step6:  III. Matrices
Step7:  Can also use the matrix type which is like array but restricts to 2D. Also, matrix adds .H and .I methods for hermitian and inverse, respectively. For more information, see Stack Overflow question #4151128
Step8:  Examples
Step9:  Interpret this result
Step10:  Signs are opposite compared to the book, but it turns out that (-) doesn't matter in the interpretation of eigenvectors
Step11:  Part 2
Step12:  Vector products in QuTiP
Only need to know one operator
Step13:  Matrix in QuTiP
Step14:  Example from class
Step15:  Some additional examples we have already done by hand
Step16:  Problem 1.8
Find the constant $c$ that normalizes the probability density
<ASSISTANT_TASK:>
Python Code:
from numpy import array, dot, outer, sqrt, matrix
from numpy.linalg import eig, eigvals
from matplotlib.pyplot import hist
%matplotlib inline
rv = array([1,2])  # a row vector
rv
cv = array([[3],[4]])  # a column vector
cv
rv
Explanation: Lab 1 - Vectors and Matrices
This notebook demonstrates the use of vectors and matrices in IPython. Note that the basis is not explicit in any of these operations. You must keep track of the basis yourself (using variable names, or notes etc).
End of explanation
dot(rv,cv)
dot(cv,rv)
Explanation: Two kinds of vector products we'll see: inner product (dot product) and outer product
1) Use the function dot(vector1, vector2) to find the dot product of rv and cv. Does the order of the arguments matter?
End of explanation
outer(rv,cv)
outer(cv,rv)
Explanation: 2) Use the function outer(vector1, vector2) to find the outer product of rv and cv. Does the order of the arguments matter?
End of explanation
# Complex numbers in python have a j term:
a = 1+2j
a
v1 = array([1+2j, 3+2j, 5+1j, 4+0j])
v1
Explanation: II. Complex vectors
End of explanation
v1.conjugate()
Explanation: The complex conjugate changes the sign of the imaginary part:
End of explanation
dot(v1.conjugate(),v1)
Explanation: 3) Use dot() and .conjugate() to find the dot product of v1 and it's own conjugate:
End of explanation
# a two-dimensional array
m1 = array([[0,1],[-2,3]])
m1
# can find transpose with the T method:
m1.T
# find the eigenvalues and eigenvectors of a matrix:
eig(m1)
Explanation: III. Matrices
End of explanation
m2 = matrix( [[2,1],[2,1]])
m2.H
evals, evec = eig(m2)
evals
evec
# use a question mark to get help on a command
eig?
Explanation: Can also use the matrix type which is like array but restricts to 2D. Also, matrix adds .H and .I methods for hermitian and inverse, respectively. For more information, see Stack Overflow question #4151128
End of explanation
M14 = array([[0,1],[-2,3]])
eig(M14)
Explanation: Examples:
Example 1.4
Find the eigenvalues and eigenvectors of M = ([0,1],[-2,3]])
End of explanation
1/sqrt(2)  # this is the value for both entries in the first eigenvector
1/sqrt(5)  # this is the first value in the second eigenvector
2/sqrt(5)  # this is the second value in the second eigenvector
eig(M14)
Explanation: Interpret this result:
the two eigenvalues are 1 and 2
the eigenvectors are strange decimals, but we can check them against the stated solution:
End of explanation
M16 = array([[0,-1j],[1j,0]])
evals, evecs = eig(M16)
evecs
evecs[:,0]
evecs[:,1]
dot(evecs[:,0].conjugate(),evecs[:,1])
Explanation: Signs are opposite compared to the book, but it turns out that (-) doesn't matter in the interpretation of eigenvectors: only "direction" matters (the relative size of the entries).
Example: Problem 1.16 using Python functions
End of explanation
from qutip import *
# Create a row vector:
qv = Qobj([[1,2]])
qv
# Find the corresponding column vector
qv.dag()
qv2 = Qobj([[1+2j,4-1j]])
qv2
qv2.dag()
Explanation: Part 2: Using QuTiP
Keeping track of row and column vectors in Ipython is somewhat artificial and tedious. The QuTiP library is designed to take care of many of these headaches
End of explanation
qv2*qv2.dag()  # inner product (dot product)
qv2.dag()*qv2  # outer product
Explanation: Vector products in QuTiP
Only need to know one operator: "*"
The product will depend on the order, either inner or outer
End of explanation
qm = Qobj([[1,2],[2,1]])
qm
qm.eigenenergies()  # in quantum (as we will learn) eigenvalues often correspond to energy levels
evals, evecs = qm.eigenstates()
evecs
evecs[0]
Explanation: Matrix in QuTiP
End of explanation
qv = Qobj([[0,1],[-2,3]])
qv
evals, evecs = qv.eigenstates()
evals
evecs
qv*evecs[0]
qv*evecs[0] == evals[0]*evecs[0]
qv*evecs[1] == evals[1]*evecs[1]
Explanation: Example from class:
End of explanation
# Solution
data = [10,13,14,14,6,8,7,9,12,14,13,11,10,7,7]
# Fill in the hist() function:
n, bins, patches = hist(data, bins=9, range=(5,14))
Explanation: Some additional examples we have already done by hand:
Problem 1.2 using the hist() function.
Create a histogram of the data and from it estimate the probability distribution. Use min and max values for the range of 5 and 14, and use 5 bins (5 and 6 go in the first bin).
End of explanation
# Partial Solution:
from sympy import *
c,a,x = symbols("c a x")
Q.positive((c,a))
first = integrate( c*exp(-a*x) ,(x,0,oo),conds='none')
first
check = integrate( a*exp(-a*x) ,(x,0,oo),conds='none')
check
Explanation: Problem 1.8
Find the constant $c$ that normalizes the probability density:
$$p(x) =
\begin{cases}
ce^{-ax},  & x \geq 0 \[2ex]
0 & x < 0
\end{cases}
$$
Hint: using sympy, we can calculate the relevant integral. The conds='none' asks the solver to ignore any strange conditions on the variables in the integral. This is fine for most of our integrals. Usually the variables are real and well-behaved numbers.
End of explanation
<END_TASK>