QM introduction
"***************************************"
==> Copy and run this script in eigenmath
"***************************************"
""
""
"*******************************************************************************"
"*         Part I     Quantum mechanic introduction                            *"
"*******************************************************************************"
 
" 1. The state of a quantum mechanical system is represented mathematically"
" by a normalized ket  |psi>  with all information you can know about it"
""
" |psi>  is a unit vector of an Hilbert space  (vector space over complex,"
" with inner product)"
""
" associated with every state  |psi>  there is something called dual  <psi|"
" the inner product of two states is given by   <psi1|psi2>"
""
" if  |psi1>  and  |psi2>  are two states of a system, c1*|psi1> + c2*|psi2> is"
" also a state of the system, linear superposition of states  psi1 and  psi2"
" (c1 and c2 are complex numbers)"
""
" physical values are represented by Hermitian operators with complete spectrum"
" (observables) of a Hilbert space, with  |ai>  eigenstates  (eigenvalues  ai)"
" of the observable    :        Â |ai>  =  ai*|ai>"
" (don't confuse ai and |ai> : ai is a real number, |ai> is a label for the"
" eigenstate corresponding to the eigenvalue ai)"
""
" the set of all possible measurment results S = {..., ai,...} is the  spectrum"
" (may be discret or continuous)"
""
" the eigenstates |ai>  of  Â verifie orthogonality and closure relations :"
"    <ai|aj>  =  kronecker(i,j)        ==> = 1  if  i = j   else = 0"
"    sum(i, |ai><ai|)  =   Î               (Î = unitary operator)"
""
" 2. probabilistic behaviour"
" on a physical system on state |psi>, the result of measurement of a physical"
" value represented by an observable  Â  is an eigenvalue   ai of  "
" and the probability for a result  ai  is  |<ai|psi>|^2,  square of modulus  of"
" probability amplitude :      <ai|psi>     (with  |ai>  eigenstate of  Â )"
""
" 3. quantification postulates"
" if we quantify a classical system, we have to find the classical             "
" properties at the limit  h --> 0.   Quantification postulates have to traduct"
" a kind of analogy betwen classical and quantic mechanics.                    "
" classical mechanics :  { , }  <==>  quantic mechanics : -i/h [ , ]           "
""
" notation :    h            is  h bar =                 h/2pi,   "
"               a*           is complex  conjugate,      a* = conj(a)"
"               A'           is Hermitian conjugate,     A' = hconj(A)"
"               Â or A       operators                   A = ((a1,a2),(a3,a4))"
"               r2                                       r2 = 1/sqrt(2)"
""
"*******************************************************************************"
"*         summary of EVAlgebra QM functions                                   *"
"*******************************************************************************"
 
" scalar c conjugate :                         conj(c)"
" vector ket |a> :                             a = (a1, a2, ..., an )"
" vector bra <a|:                              conj(a)=(a1*, a2*,...,an*)"
" inner product <a|b>:                         dot(conj(a),b)"
" magnitude of vector |a> :                    abs(a)"
" normalize vector |a> :                       normalize(a)"
" outer product  |a><b| :                      dot(a,conj(b))"
" tensor product a x b :                       Tprod(a,b)"
" complex conjugate of operator Â:             conj(A)"
" transpose of matrix A :                      transpose(A)"
" Hermitian conjugate (adjoint) of operator A: hconj(A)"
" identity operator Î :                        unit(n)"
" inner product <a|A|b> :                      dot(conj(a),dot(A,b)) "
r2=1/sqrt(2)
pr(ai,psi)=float(mag(rect(dot(hconj(ai),psi)))^2)
normalize(a)=a/float(sqrt(dot(conj(a),a)))
norm(a)=normalize(a)
hconj(x)=transpose(conj(x))
Tprod(x,y)=do(n=0,z=zero(dim(x)*dim(y)),for(j,1,dim(x),(for(k,1,dim(y),n=n+1,z[n]=x[j]*y[k]))),z)
tprod(x,y)=Tprod(x,y)
"*******************************************************************************"
"*                   Bras, Kets, scalars                                       *"          
"*******************************************************************************"
""
do( a=(a1,a2,a3), b=(b1,b2,b3), bra=("a1*","a2*","a3*"))      
" ket  |a>  :   a = (a1, a2, a3)                       (ai  complex numbers)"
" ket  |b>  :   b = (b1, b2, b3)"
a
" bra  <a|  :   bra = conj(a) = (a1*, a2*, a3*)        (ai* complex conjugate)"
bra
""
" scalar c  :   c  = 3 - 5*i                           (c   complex number)"
" conjugate :   c1 = 3 + 5*i                           (i   -->  -i)"
do(c=3-i*5, c1=conj(c))
c
c1
""
"*******************************************************************************"
"*         inner  products   <a|b>,  <a|a>               (resulting scalar)    *"
"*******************************************************************************"
""
" inner product  <a|b > :   dot(conj(a),b)                with conj(a) = <a|"
dot(conj(a),b)
" inner product  <a|a > :   dot(conj(a),a)"
dot(conj(a),a)
"  (<a|b>)*  =   <b|a>   "
""
" normalization function :  normalize (a) =  a/sqrt(dot(conj(a),a)) "
normalize(a)=a/float(sqrt(dot(conj(a),a)))
norm(a)=normalize(a)
psi=normalize(((1+i),(1-i),(5)))
" ex : psi = normalize (((1+i ),(1-i),(5)))"
psi
" module of psi  =   abs (psi)  "
abs(psi)
""
"*******************************************************************************"
"*       outer product  |b><a|                           (resulting operator)  *"
"*******************************************************************************"
""
"  outer product  |b><a|  :                    A = outer (b, conj(a)) "
A=outer(b,conj(a))
A
"     (|b><a|)' = |a><b| "
 
check(hconj(outer(b,conj(a))=outer(a,conj(b)))
""
"********************************************************************************"
"*        tensor product  |a> x |b>  =  |a>|b> = |ab>                           *"
"********************************************************************************"
""
Tprod(x,y)=do(n=0,z=zero(dim(x)*dim(y)),for(j,1,dim(x),(for(k,1,dim(y),n=n+1,z[n]=x[j]*y[k]))),z)
" |ab>  =   Tprod(a, b),      |abc>  =   Tprod(a, Tprod(b, c) "
Tprod(a, b)
 
"*******************************************************************************"
"                      operators                                               *"
"*******************************************************************************"
" operators are basically linear functions from the Hilbert space into the same"
" space. An operator A acts on a ket and produces a new ket: Â|psi> = |phi>"
" the order is important, the operators act from the left of the kets"
" most of the time, we need linear operators"
" if   |psi> = c1*|psi1> + c2*|psi2>   then     Â|psi> = c1*Â|psi1> + c2*Â|psi2>"
""
" the operators are defined only by their action on the kets, therefore, if two"
" operators have the same action on the kets (if A|psi> = B|psi> for"
" all kets |psi>) then the operators are equal (A = B)"
" the zero operator always produces zero vectors and will be denoted by 0:"
" If A|psi> = 0 for all kets |psi> then A = 0"
" the identity operator, Î, produces the same ket: I|psi> = |psi>"
" for all kets |psi>"
""
" the addition and multiplication with a scalar for operators are defined in the"
" usual way : we say  = c*P +d*Q if Â|psi> = c*P|psi> + d*Q|psi> for all "
" kets |psi>. As a result, the set of operators is a vector space"
""
" by the product AB of two operators A and B we mean an operator in which B acts"
" first and A acts on the result, i.e., (AB)|psi>  = A (B|psi>). For most of the"
" cases, the order is important, the operator product is not commutative,"
" i.e., AB not= BA. We say that A and B commute if AB = BA"
" in all cases, the product is associative, A(BC) = (AB)C"
""
" the inner product of |ai> with Â|aj> is denoted by the symbol <ai|A|aj> and is"
" called a matrix element of Â. If the two kets are same and they are normalized,"
" then the matrix element is also called an expectation value. For example,"
" if <ai|ai> = 1, <ai|A|ai> is the expectation value of  in state/ket |ai>"
" this expectation value can also be expressed as <A>"
""
 
"******************************************************************************"
"                    adjoint, or hermitian conjugate                           *"
"******************************************************************************"
" for each operator A, we define the hermitian conjugate A' by"
 
"    <phi|A psi> = <A' phi|psi>    for all kets |phi> and |psi>"
" there are other ways of expressing the relation above: "
"    <phi|A|psi> = <psi|A'|phi>"
" the hermitian conjugation satisfies the following properties, which can"
" easily be shown :"
" (A')' = A"
" (AB)' = B'A'"
" Antilinearity: (c A + d B)' = c* A' + d* B' "
 
""
" EVAlgebra Hermitian conjugate function :  hconj(x) = transpose(conj(x))"
hconj(x)=transpose(conj(x))
" Hermitian conjugation is denoted by ' (dagger) and is a linear operation "
""
" conjugation of a scalar c,    a linear operator     and a ket |a>    gives"
"      c' (or c*),        Â',        <a|        respectively   "
" two successive conjugations cancel : (c')' = c   (Â')' = Â   (|a>')' = |a> "
" product of operators/states conjugation => factors conjugated and order reversed "
" (A B)'  =  B' A'        (A |a>)'  =  <a| A'          <a| Â|b>' = <b|Â' |a> "
""
" ket |a>  Hermitian conjugate  :  hconj(a)           =  <a|  "
""
" bra <a|  Hermitian conjugate  :  hconj(conj(a))     =  |a>  "
""
" scalar c  Hermitian conjugate   :  hconj(c)         =   c*  "
""
" operator   Hermitian conjugate :  hconj(Â)         =   Â'  "
""
" the eigen ket of  with eigenvalue a is denoted as |phi> with  Â|phi> = a*|phi>"
""    
"*******************************************************************************"
"*                       Hermitian operators                                   *"
"*******************************************************************************"
" an operator  is called hermitian or self adjoint if  = Â'. It can easily be"
" seen that the hermitian operators have real expectation values:"
"   <a|A|a> = <a|A|a>* "
" the opposite is also true. If all expectation values of an operator  is real"
" valued, then it can be shown that the operator  is hermitian"
" we associate hermitian operators with observables in QM"
" an operator A is invertible if there is another operator B such that"
" AB = BA = I"
" the identity. In such a case, B is called the inverse and is shown by B = 1/A"
" obviously the inverse is also invertible and 1/(1/Â) = Â"
""
" an operator Û is called unitary if its inverse is equal to its hermitian"
" conjugate: 1/Û = Û'. Equivalently, we have ÛÛ' = Û'Û = 1. These operators"
" appear frequently in QM as symmetry operators. There are a few important"
" properties of unitary operators which should be noted down in here"
""
" 1. they are norm preserving: If |phi> = Û|psi>, then <phi|phi> = <psi|psi>"
""
" 2. a result which follows directly from (1): If |psi> is normalized, then"
" |phi> = Û|psi> is also normalized. This is the crucial property we use in QM"
" we require the symmetry operators to map the states (normalized vectors)"
" to other states and this condition implies that the symmetry operators are"
" unitary (we also need the conditions of linearity and invertibility)"
""
" 3. they are inner product preserving: If |phi> = Û|psi> and |phi1> = Û|psi1>,"
" then <phi|phi1> = <psi|psi1> "
""
" 4. they map orthonormal basis into orthonormal basis: If the set"
" {psi1, psi2, ..., psin} is an orthonormal basis for the Hilbert space and"
" |phin> = Û|psin> for all n, then {phi1, phi2, ..., phin} is also an"
" orthonormal basis"
""
" an operator A is called normal if it commutes with its hermitian conjugate,"
" ÂÂ' = Â'Â. You can easily check that both hermitian and unitary operators"
" are normal"
 








" Hermitian operator :  Â'  =  Â       ==>    act similarly on bra or ket vectors "
" operator  acting on bra  <a| :  dot(conj(a), A)       -->    result bra"
" operator  acting on ket  Â|b> :  dot (A, b)            -->    result ket"
" scalar product   <a| Â|b> :  dot(conj(a),dot(A, b))     -->    result    scalar"
""
" if {|ai>} are eigenstates of an Hermitian operator, they form a complete set "
"    any state |psi>  can be written in terms of the {|ai>} :  "
"    |psi> = sum(i, ai*|ai>)  with  ai =  <ai|psi>  just like usual vectors "
""
" similarly, operator  can be written as  Â = sum(i,j, Aij|i><j|)"
" with Aij = <i|A|j> .         in particular,  Î  =  sum(i, |i><i| "
"  %%%%%%%%%%%%%%%%%% e1 A e2"
"****************************************************************************"
"*                                 exemples :                               *"
"****************************************************************************"
" for the Hilbert space of n * 1 column matrices, the operators are n * n"
" square matrices."
A=((a11,a12,a13),(a21,a22,a23),(a31,a32,a33))
" A = ((a11,a12,a13),(a21,a22,a23),(a31,a32,a33))  is an operator 3 * 3"
A
" aij are called the matrix elements of Â. i is the row index, j the column"
 
" index. The action of the operator A on vectors is computed by the usual matrix"
" product. For example, if Au = w, then the matrix elements are related by"
"  sum(j, aij*uj) = wi"
do(A=((a11,a12),(a21,a22)),u=(u1,u2))
w=dot(A,u)
w
 

" zero operator (dimention n): 0 = zero(n) --->  always produce zero vectors"
" unit operator (dimention n): I = unit(n) --->  Î|psi> = |psi> "
" zero operator dimention 2 :  0 = zero(2,2)"  
zero(2,2)
" identity operator dimention 2 :  I = unit(2)"  
unit(2)
" the hermitian conjugation operation is the same as the matrix hermitian"
" conjugation: exchange the rows with columns (i.e., take transpose) and take"
" the complex conjugate of every element."
A
Ahc=(("a11*","a21*"),("a12*","a22*"))
Ahc
" the matrix elements of A' can be expressed as (A')ij = (A*)ji"
 

""
"*******************************************************************************",
"                                  dynamics                                    *"
"*******************************************************************************"
" evolution of quantum system is described by a unitary transformation,"
" operator  Û(t) wich depend only on time t2 and t1 : |psi(t2)> = Û |psi(t1)>. "
" The transform occurs without any regard to current state |psi>"
""
"  Û(t) properties : Û(0)     = Î"
"                    Û(t1+t2) =  Û(t1)Û(t2)"
"                    |psi(t)> =  Û(t)|psi(0)>"
"  if |psi1> = Û|psi)>      |psi)> = Û'|psi1)>   -->  Û' is the reverse Û action"
""
" e.g.   Û(t) = exp(-i/h H*t) with H the time-independant Hamiltonian"
" (total energy of the system) "
""
" if an operator  is Hermitian, then exp(i*Â) is unitary "
" rotations generally of the previous form"
" if Û is unitary, then sqrt(Û) is unitary"
""
""
" e.g.: let a two basis state system with canonical basis "
"      |zu> = (1,0) and |zd> = (0,1)"
" a state |psi> = 2|zu>+(1-i)|zd> and operator Û = ((0,1),(1,0)), |psi1> = Û|psi>"
do(zu=(1,0), zd=(0,1), psi=2zu+(1-i)zd)
U=((0,1),(1,0))
psi1=dot(U,psi)
psi
psi1
" |psi> = Û'|psi1>     reverse action ;"
dot(hconj(U),psi1)
""
"********************************************************************************"
"*                        observables                                           *"
"********************************************************************************"
" an observable is a Hermitian operator, i.e. an operator  such that Â' = Â"
""
" an eigenvalue a of an operator  is a scalar for wich there is a ket |psi> such"
" that        Â|psi> = a*|psi> "
" |psi> is called an eigenstate of  corresponding to the eigenvalue a"
""
" the eigenvalues ai of an observable  (Hermitian operator) are all real numbers"
" the eigenstates for distinct eigenvalues of an observable are orthogonal"
""
" an eigenvalue is degenerate if there are at least two linearly independant"
" eigenstates for that eigenvalue. Otherwise it is nondegenerate"
""
" notation : if all eigenvalues ai of an observable  are nondegenerate, then we"
" can label label the correponding eigenstates |ai>. Then we can write"
"                  Â|ai> = ai*|ai>   for each eigenvalues ai"
""
" an observable  is said to be complete if his eigenstates |ai> form a basis of"
" the Hilbert space. Since by convention all the eigenstates are normalized"
" (unit length), the eigenstates of a complete nondegenerate observable  form"
" an orthonormal basis of the underlying Hilbert space  ==>"
" every ket |psi> can be written as :   |psi> = sum(i, |ai><ai|psi>)"
" (just like usual vectors : V = sum(i, vi*ei) with vi = V.ei)"
""
"      |psi> = sum(i, |ai><ai|psi>) = sum(i, |ai><ai|)|psi>     ==> "
"      sum(i, |ai><ai|) = Î       wich express the completness of Â"
""
"********************************************************************************"
"*                        quantum measurement                                   *"
"********************************************************************************"
" Â a complete, non degenerate observable with eigenvalues ai, eigenstates |ai>"
 


" the result of measurement is an eigenvalue of an observable."
" if the system is in the state |psi> before the measurement, then :  "
" probability of a result  ai is :      pr(ai) =  |<ai|psi>|^2 "
""
" if the result ai is obtained,then the system collapses immediately to the state"
" of the corresponding eigenstate |ai>"
""
" actual state :    |psi> = sum(i,|ai><ai|psi>) --> "
"    1st measurement of observable  ---> state |aj> with probability |<aj|psi>|^2"
"    immediate  2nd measurement      ---> state |aj> with probability   1"
"    the measured value is the eigenvalue aj         with probability |<aj|psi>|^2"
""
 

" example for a two dimentional system with basis {|H>,|V>} :"
"        let |H> = (1,0) and |V> = (0,1) a basis for polarized photon    ==>:"
"         <H|V> = 0    <H|H> = 1   <V|V> = 1       |H><H| + |V><V| = Î"
" a polarization device may be mathematically represented by an orthonormal basis"
""
" 1. circulary polarized photon  with |Vpf|  vertical polaroid filter  ==>"
" measurement operator :  M = |V><V|"  
""
" |p> = r2*(|V> + i*|H>     circulary polarized photon state"
""                    
" 1. |p> ---> |V pf| --->  50% Vertical and 0% Horizontal"
" |V pf| measurement operator :  M = |V><V|"
""
do(V=(1,0), H=(0,1), p=r2*(V+i*H), M=outer(conj(V),V))
dot(M,p)
 

" 2. |p> ---> |V pf| ---> 50% V ---> |H pf| --->   nothing"
" |H pf| measurement operator :  M = |H><H|"
 
" 3. |p> ---> |V pf| ---> 50% V ---> |pi/4 pf| ---> |H pf| --->  0% photons"
" |pi/4 pf| measurement operator :  M = |V><V|"
 

"  (Â|psi>)/ sqrt(pr(ai))     or    (Â|psi>)/ sqrt(<psi|Â|psi>)) "
""
" measurement postulate of quantum mechanics states that any device measuring"
" a 2 dimensional system has an associated orthonormal basis with respect to"
" which the quantum measurement takes place. Measurement of a state transforms"
" the state into one of the measuring device associated basis vectors."
""
" The probability that the state is measured as basis vector |ai> is the square"
" of the norm of the amplitude of the component of the original state in the"
" direction of the basis vector |ai>"
""
" different measuring devices have different associated basis, and measurements"
" using these devices will have different outcomes."
""
" As measurements are always made with respect to an orthonormal basis"
" throughout the rest of this script all bases will be assumed to be orthonormal."
 
" furthermore, measurement of the quantum state will change the state to the"
" result of the measurement. That is, if measurement of  |psi> = a*|H> + b*|V>"
" results in |H>, then the state psi changes to |H> and a second measurement"
" with respect to the same basis will return |H> with probability 1. "
" Thus, unless the original state happened to be one of the basis vectors,"
" measurement will change that state, and it is not possible to determine"
" what the original state was."
""
"********************************************************************************"
" the expected value (average value) of a measurement of an observable  "
" is given by     <psi|Â|psi>   when the particule is in state |psi>"
 
pr(ai,psi)=float(mag(dot(hconj(ai),psi))^2)
"********************************************************************************"
" multi Qbit system : the state space of a composite physical system is "
" the tensor product of the state spaces of the component physical system : "
" |psi> =  |psi1> x |psi2> x ... x |psik>       with  x  tensor product"
"********************************************************************************"
 
tprod(x,y)=do(n=0,z=zero(dim(x)*dim(y)),for(j,1,dim(x),(for(k,1,dim(y),n=n+1,z[n]=x[j]*y[k]))),z)
" e.g.: b0=(1,0), b1=(0,1) a 2 state basis,  |psi1> = b0+2b1, |psi2> = 3b0+4b1"
"  then  |psi> = |psi1> x |psi2> = Tprod(|psi1>,|psi2>)"
do(b0=(1,0), b1=(0,1))
psi1 =  b0 + 2b1
psi2 = 3b0 + 4b1
psi = Tprod(psi1, psi2)
print(psi1, psi2, psi)
" we get a new state space with basis  : "
" b00=(1,0,0,0), b01=(0,1,0,0), b10=(0,0,1,0), b11=(0,0,0,1)"
 
"*******************************************************************************"
"*                                                                             *"
"*******************************************************************************"