QC introduction
"***************************************"
==> Copy and run this script in eigenmath
"***************************************"
""
""
"*******************************************************************************"
"*         Part II     Quantum computing tutorial                              *"
"*******************************************************************************"
 
" 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,   "
"               *       is complex  conjugate,"
"               '       is Hermitian conjugate"
"               r2      is 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 matrix A: conj(A), transpose of matrix A : transpose(A)"
" Hermitian conjugate (adjoint) of matrix A: hconj(A), unit operator Î : unit(n)"
" inner product of <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)
 
"********************************************************************************"
"* two states Quantum system  :  e.g  electron spin, photon polarization ...    *"
"********************************************************************************"
r2=1/sqrt(2)
" the Bloch sphère "
""
" a two state quantic system may be represented by a ket |psi> = c1*|0>+ c2*|1>"
" with   c1^2+c2^2 = 1   and     |0>=(1,0)     |1>=(0,1). "
" |psi> =  cos(theta/2)*|0> + exp(i*phi)*sin(theta/2)*|1>  in 3D polar form with "
" theta [0, pi]  =  angle betwen z axis and  |psi> "
" phi   [0, 2pi] =  angle betwen x axis and  |psi>  projection on x y plane "
" represented by points on a surface of an unit sphère, the Bloch sphere. "
" cartesian coordinates are related to polar coordinates by "
"    x  = sin(theta/2)*cos(psi)"
"    y  = sin(theta/2)*sin(psi)"
"    z  = cos(theta/2) "
" the Bloch sphere is a generalization of the representation of a complex number c"
" with |c^2| = 1 on the unit circle on the complex plan."
" |z+>  =  |0>                     with    theta = 0"
(1,0)
" |z->  =  |1>                     with    theta = pi"
(0,1)
" |x+>  =  r2*(|0> + |1>)          with    theta = pi/2,     phi = 0 "
r2*((1,0)+(0,1))
" |x->  =  r2*(|0> - |1>)          with    theta = -pi/2,    phi = pi "
r2*((1,0)-(0,1))
" |y+>  =  r2*(|0> + i*|1>)        with    theta = pi/2,     phi = pi/2 "
r2*((1,0)+(0,i))
" |y->  =  r2*(|0> - i*|1>)        with    theta = -pi/2,    phi = 3pi/2 "
r2*((1,0)-(0,i))
" with r2 = 1/sqrt(2)   normalization factor."
" opposite points on the Bloch sphere are orthogonals"
""
" photon"
 
" a photon is a boson of spin 1"
" A spin has only two directions: in the moving direction (left photon spin = 1)"
" or in the reverse (right photon spin =  -1) "
" the rigth photon is in |R> state and the left photon is in |L> state"
" a ligth polarized circularly to the rigth is composed exclusively by rigth"
" photons and idem for the left"
" a ligth polarized linearly is composed with 50% of rigth photons"
"                                         and 50% of left photons."
""
"  2.  photon crossing a polarizer"
""
do(H=(1,0),V=(0,1))
print(H,V)
" a polarizer puts a photon in state |H>, a state |V> 'orthogonal' exist also."
" this two states may serve to describe a photon :"
" |L> =    r2*(|H> + i*|V>)"
" |R> =    r2*(|H> - i*|V>)"
do(L=r2*(H+i*V), R=r2*(H-i*V))
# print(L,R)
" or"
" |H> =    r2*(|R> + |L>)"
" |V> =  r2*i*(|L> - |R>)"
do(H=r2*(R+L), V=r2*i*(L-R))
# print(H,V)
" amplitud <H|L> for a left photon to give a |H> photon crossing the polarizer"
"  is <H|L> = r2*(<H|H> + i*<H|V>)"
" <H|V> = 0  -->  <H|L> = r2*<H|H> = r2"
" amplitud <H|R> for a rigth photon to give a |H> photon crossing the polarizer"
" is <H|R> = r2*(<H|H> - i*<H|V>)"
" <H|V> = 0  -->  <H|R> = r2*<H|H> = r2"
" the probability is the square of amplitude norm  --> Pr(H) = 1/2"
"                                               pr(H,L)=  "
 pr(H,L)
"                                               pr(H,R)=  "
 pr(H,R)
""
" a rigth or left photon has one chance over two to cross a polarizer."
" with a great number of photon, 50% cross the polarizer and "
" the energy is diveded by two, as tell us classical theory."
 
"  2. linearly polarized photon crossing a polarizer"
 
" after crossing a polarizer a photon is linearly polarized"
" this photon has an amplitud to be rigth and another to be left. it is rigth"
" one time over two and left one time over two."
" state is |H> = r2*(|R> + |L>)"
" crossing a polarizer oriented in a direction H1 having angle theta with H,"
" it will be in a state |H1> wich may be write in function of states |H> and [V>:"
"  |L>  spin 1 is multiplied by exp(i*theta)  and  |R>  spin -1 is multiplied by"
" exp(-i*theta)"
" |H1> =  r2*(exp(-i*theta)*|R>  + exp(i*theta)*|L>)"
" |V1> =  r2*i*(exp(i*theta)*|L> - exp(-i*theta)*|R>)"
do(H1=r2*(exp(-i*theta)*R + exp(i*theta)*L), V1=r2*i*(exp(i*theta)*L - exp(-i*theta)*R))
do(H1=rect(H1), V1=rect(V1))
H1
V1
" the amplitud for a photon |H> to give a photon |H1> is"
" <H1|H> =  r2*(<H1|R> + <H1|L>)"
" <a,b> = <b,a>* ==> "
" <H1|H> = <H|H1>* = r2*(exp(i*theta)*<H|R> + exp(-i*theta)*<H|L>) ="
" r2*(exp(i*theta)*r2  + exp(-i*theta)*r2)"
" <H1|H> =  1/2*(exp(i*theta)  + exp(-i*theta)) = cos(theta)"
"                                                  rect(dot(conj(H1),H)) ="
rect(dot(conj(H1),H))
" photon |H> amplitud crossing the polarizer having an angle theta with H is"
" cos(theta)      ==>"
" the crossing probability is Pr(H1) =  <H1|H><H1|H>*  = cos²(theta)"
"                                                  pr(H1,H) ="
pr(H1,H)
" H polarized photon has a probability cos²(theta) to cross the polarizer"
" with a great number n of photons, there are n*cos²(theta) crossing and the"
" energy is I(H1) =  I(H)*cos²(theta) , as tell classical theory"
""
" the amplitud for a photon |H> to give a photon |V1> is"
" <V1|H> =  r2*(<V1|R> + <V1|L>)"
" <V1|H> = <H|V1>* = r2*i*( exp(i*theta)*<H|L> - exp(-i*theta)*<H|R>) ="
"  r2*i*(exp(i*theta)*r2 - exp(-i*theta)*r2)"
" <V1|H> =  1/2  i*(exp(i*theta) - exp(-i*theta) ) = sin(theta)"
"                                                  rect(dot(conj(V1),H)) ="
rect(dot(conj(V1),H))
" photon |H> amplitud absorved by the polarizer having an angle theta with H is"
" sin(theta) ==> the absorption probability is Pr(V1) = <V1|H><V1|H>*"
" Pr(V1) = sin²(theta)"  
"                                                  pr(V1,H) ="
pr(V1,H)
" H polarized photon has a probability sin²(theta) to be absorved"
" by the polarizer. With a great number n of photons, there are n*sin²(theta)"
" absorved and the energy is I(V1) =  I(V)*sin²(theta) , as tell classical theory",
 






"********************************************************************************"
"*              exemple of two states quantum system : electron spin.           *"
"********************************************************************************"
" canonical (Sz) basis): |zu>=|0>,  |zd>=|1>   spin up/down in z direction "
do(zu=(1,0), zd=(0,1))    
print(zu, zd)
" direction x along wich to measure the spin component  "
do(xu=r2*(1,1), xd=r2*(1,-1))
print(xu, xd)
" direction y along wich to measure the spin component  "
do(yu=r2*(1,i), yd=r2*(1,-i))
print(yu, yd)
""
" general direction w along wich to measure the spin component  "
wu=cos(theta/2)*zu+sin(theta/2)*exp(i*phi)*zd
wd=sin(theta/2)*zu-cos(theta/2)*exp(i*phi)*zd
print(wu "" wd)
" spin components operators   "
do(Sx=h/2((0,1),(1,0)), Sy=h/2((0,-i),(i,0)), Sz=h/2((1,0),(0,-1)))
print(Sx,  Sy,  Sz)
 
" S = sqrt ( Sx^2 + Sy^2 + Sz^2 )    "  
S = sqrt(Sx^2+Sy^2+Sz^2)
S
""
Sw=h/2*((cos(theta),sin(theta)*exp(-i*phi)),(sin(theta)*exp(i*phi),-cos(theta)))
" Sw = Sx*sin(theta)*cos(phi) + Sy*sin(theta)*sin(phi) + Sz*cos(theta)  "
""
" projection operators e.g.  Pxu = |xu><xu|  Pxd = |xd><xd|, Pyu  Pyd, Pzu  Pzd "
do(Pxu=outer(conj(xu),xu), Pxd=outer(conj(xd),xd))
do(Pyu=outer(conj(yu),yu), Pyd=outer(conj(yd),yd))
do(Pzu=outer(conj(zu),zu), Pzd=outer(conj(zd),zd))
print(Pxu,  Pxd,  Pyu,  Pyd,  Pzu,  Pzd, "")
" verify completness relation e.g.     |xu ><xu| + |xd><xd| = Î   "
do(check(Pzu+Pzd=unit(2)), check(Pxu+Pxd=unit(2)), check(Pyu+Pyd=unit(2)))
 


psi1=norm(3zu+4zd)
psi2=norm(zu+2i*zd)
psi3=norm(3zu-exp(i*pi/3)zd)
 
"*******************************************************************************"
"*                            multi stage Stern and Gerlach experiment :       *"
"*******************************************************************************"
""
"function pr(phi,psi) gives the transition probability psi --> phi: |<phi|psi>|^2"
pr(phi,psi)=float(mag(dot(hconj(phi),psi))^2)
state(projector,psi)=dot(projector,psi)/sqrt(dot(conj(psi),dot(projector,psi)))
""
" S----------->|Z|-----------> 50% up and 50% down"
""
" S---->|Z|--100% up-->|Z|---> 100% up and 0% down"
pr(zu,zu)
pr(zd,zu)
""
" S---->|Z|--100% up-->|X|---> 50% up and 50% down"
pr(xu,zu)
pr(xd,zu)
" S---->|Z|--100% up-->|X|---> 100% up --->|Z|---> 50% up and 50% down"
" xd is discarded, we select only xu"
pr(zu,xu)
pr(zd,xu)
"                        |---> 50% up   --->|Z|---> 25% up and 25% down"
" S---->|Z|--100% up-->|X|"
"                        |---> 50% down --->|Z|---> 25% up and 25% down"
pr(xu,zu)*pr(zu,xu)
pr(xu,zu)*pr(zd,xu)
pr(xd,zu)*pr(zu,xd)
pr(xd,zu)*pr(zd,xd)
""
"                        |---> 50% up   --->|"
" S---->|Z|--100% up-->|X|                  |Z|---> 100% up "                  
"                        |---> 50% down --->|"
psi2=state(Pxu+Pxd,zu)
pr(zu,psi2)
pr(zd,psi2)
"*******************************************************************************"
"*                              quantum computing                              *"
"*******************************************************************************"
" a quantum computer is a device wich acts on qubit strings."
" the permissible instructions are :"
" 1. initialize a qubit at |0>"
" 2. perform a unitary operation on any finite subset of the qubits"
" 3. measure qubits in the basis |0>, |1>"
" 4. terminate"
" 5. discard a qubit or reset it to |0>"
""
" a quantum computer may be implemented theorically with any two state particle"
" as photons or any particle/atom with a spin"
"*******************************************************************************"
"*                               quantum bit --> qubit                         *"
"*******************************************************************************"
""
" a quantum register of size three can store individual numbers such as 3 or 7"
"  |0> x |1> x |1> = |011> -->  3"
"  |1> x |1> x |1> = |111> -->  7"
" but, it can also store the two of them simultaneously. For if we take the "
" first qubit and instead of setting it to |0> or |1> we prepare a superposition"
" r2*(|0> + |1>) then we obtain :"
" r2*(|0> + |1>) x |1> x |1> = r2*(|011> + |111>)  --> 3 and 7 simultaneously "
" in fact we can prepare this register in a superposition of all eight numbers :"
" it is enough to put each qubit into the superposition r2*(|0> + |1>) :"
"    r2*(|0> + |1>) x r2*(|0> + |1>) x r2*(|0> + |1>) ="
"  r3*(|000> + |001> + |010> + |011> + |100> + |101> + |110> + |111>) "
" ( the normalisation factor  r3 = 1/sqrt(2^3) )"
 do(b0=(1,0),b1=(0,1),x=r2*(b0+b1))
 Tprod(Tprod(x,x),x)
""
" applied to n bits individually, H generates a superposition of all 2^n possible"
"states, wich can be viewed as the binary representation for numbers 0,...,2^n-1"
"   (H x H x ... x H)|00...O> = rn*((|0> + |1>) x (|0> + |1>) x...x (|0> + |1>))"
" ( the normalisation factor  rn = 1/sqrt(2^n) )"
""
" these preparations, and any other manipulations on qubits, have to be performed"
" by unitary operations. A quantum logic gate is a device which performs a fixed"
" unitary operation on selected qubits in a fixed period of time "
" the outputs of some of the gates are connected by wires to the inputs of others"
""
" here is a network which affects the Hadamard (see next topic) transform on three"
" qubits. If they are initially in state |000> then the output is the superposition"
" of all eight numbers from 0 to 7."
""
"   |0> -- |H| ------  r2*(|0> + |1>) ]"  
"   |0> -- |H| ------  r2*(|0> + |1>) ] --> r3*(|0>+|1>+|2>+|3>+|4>+|5>+|6>+|7>)"
"   |0> -- |H| ------  r2*(|0> + |1>) ]"          
""
"*******************************************************************************"
"*                               quantum gates                                 *"
"*******************************************************************************"
""
" single qubit gate :"
""
do(I = ((1,0),(0,1)), X = ((0,1),(1,0)), Y = ((0,-1),(1,0)), Z = ((1,0),(0,-1)))
" I = identity                -Rx(2pi) : "
" with  Rw(theta) :    rotation around w axis with angle theta "
"   |0>   -->   |0>           "
"   |1>   -->   |1>           "
I
" X = not operation           i*Rx(pi) : "
"   |0>   -->   |1>           "
"   |1>   -->   |0>           "
""
"    ----- |X| ------>"
X
" Y  =  Z X product           i*Ry(pi) : "
"   |0>   -->   |1>           "
"   |1>   -->  -|0>           "
Y
" Z = phase flip              i*Rz(pi) : "
"   |0>   -->   |0>           "
"   |1>   -->  -|1>           "
Z
""
H = r2*((1,1),(1,-1))
" H = Hadamard                 Ry(pi/2) : "
"   |0>   -->    r2(|0> + |1>)          "
"   |1>   -->    r2(|0> - |1> )         "
""
"    ----- |H| ------>"
H
""
Phi = r2*((1,0),(0,exp(i*phi)))
" Phi = phase shift gate :"
"   |0>   -->               |0>          "
"   |1>   -->    exp(i*phi)*|1>          "
""
"    ----- |phi| ------>"
Phi
""
" The Hadamard gate and the phase gate can be combined to construct"
" the following network, which generates the most general pure state"
" of a single qubit (up to a global phase) :"
""
"   -----|H|----|Phi=2*theta|-----|H|-----|Phi=pi/2+phi|-----> xi"
"   ==>  xi = cos(theta)*|0> + exp(i*phi)*sin(theta)*|1>"
xi=cos(theta)*b0 + exp(i*phi)*sin(theta)*b1
xi
""
" the Hadamard and phase gates are sufficient to construct any unitary"
" operation on a single qubit"
""
" multi qubit gate :"
""
cnot = ((1,0,0,0),(0,1,0,0),(0,0,0,1),(0,0,1,0))
" cnot = controlled not :     if first Qbit = 0 --> no change, else  -->  not"
" cnot |a,b> = |a, a XOR b>      a, b = 0,1"
"   |00>   -->   |00>           "
"   |01>   -->   |01>           "  
"   |10>   -->   |01>           "
"   |11>   -->   |10>           "
""
" |0><0| x I + |1><1| x X"
""
"    -------o-------->"
"           |         "
"    ----- |X| ------>"
cnot
""
"cnot gate is the unitary operator :"
(("Î",0),(0,"not"))
" more generally, cU is the controlled Û : "
"   |00>   -->   |00>           "
"   |01>   -->   |01>           "  
"   |10>   -->   |1>U|0> =(u00*|0> + u10*|1>)"
"   |11>   -->   |1>U|1> =(u01*|0> + u11*|1>)"
" with U = ((u00,u01),(u10,u11))"
""
" |0><0| x I + |1><1| x U"
""
"    -------o-------->"
"           |         "
"    ----- |U| ------>"
cU=(("Î",0),(0,"U"))
cU
""
" Toffoli gate :"
" the Toffoli gate, also ccnot gate, is a 3-bit gate, which is universal for"
" classical computation. The quantum Toffoli gate is the same gate, defined for"
" 3 qubits. If the first two bits are in the state |1>, it applies a"
" Xnot on the third bit, else it does nothing"
" ccnot |a,b,c> = |a, b, (a AND b) XOR c>      a, b = 0,1"
""
"   |000>   -->   |000>           "
"   |001>   -->   |001>           "  
"   |010>   -->   |010>           "
"   |011>   -->   |011>           "
"   |100>   -->   |100>           "
"   |101>   -->   |101>           "  
"   |110>   -->   |111>           "
"   |111>   -->   |110>           "
""
" |0><0| x I x I + |1><1| x cnot"
""
ccnot=((1,0,0,0,0,0,0,0),(0,1,0,0,0,0,0,0),(0,0,1,0,0,0,0,0),(0,0,0,1,0,0,0,0),(0,0,0,0,1,0,0,0),(0,0,0,0,0,1,0,0),(0,0,0,0,0,0,0,1),(0,0,0,0,0,0,1,0))
ccnot
"    -------o-------->"
"           |         "
"    -------o-------->"
"           |         "
"    ----- |X| ------>"
""
" swap gate :"
" the swap gate swaps two qubits. It is represented by the operator :"
((1,0,0,0),(0,0,1,0),(0,1,0,0),(0,0,0,1))
"   |0>   -->   |1>           "
"   |1>   -->   |0>           "
""
"    ------ x -------->"
"           |         "
"    ------ x ------->"
 
"*******************************************************************************"
"*                    Entanglement : creation of an EPR pair of qubits :       *"
"*******************************************************************************"
do(b0=(1,0),b1=(0,1),b00=(1,0,0,0),b01=(0,1,0,0),b10=(0,0,1,0),b11=(0,0,0,1))
""
" |i> -- |H| -------------o--------"
"                         |        "
"                |j> ----|X| ------"
"           psi1     psi2     psi3 "
""
" suppose we begin with a qubit |i> in a zero state |1> :"
i=b1
" psi1 is the output of Hadamard gate :  psi1 = dot(H,i) "
psi1 = dot(H,i)
psi1
" now, take another qubit |i> also in the |1> state :"
j=b1
" the new state space psi2 is the tensor product of |psi1> and |j> :"
psi2 = Tprod(psi1,j)
psi2
" applying cnot to psi2 we get  |psi3> = r2*(|01> - |10>)"
" this is the entangled Bell state |B11>"
psi3=dot(cnot,psi2)
psi3
" the four Bell states are produced by using one of the basis states :"
" |00> --> B00, |01> --> B01, |10> --> B10, |11> --> B11  as input"
" into the combined  Hadamard and cnot gates."
" if we reversed the order of the Hadamard and cnot gates, then Bell states"
" are transformed into computational basis states."
""
"  -----o---- |H| ---    |i>"
"       |                   "
"  ----|X| ----------    |j>"
""
" the key to entanglement is the property that the state space cannot be"
" decomposed into component spaces. That is, for our example, there does"
 
#The measurement operator M(O) of the observable Ô = {E0; E1; : : : Ek],
#where Ei is a mutually orthogonal decomposition of H, randomly applies
#a projection operator P(Em) to the measured state |psi> biased by the probability
#pm = <P(Em)i> and renormalises the result.
" not exists any |i> and |j> such that |i> x |j> = r2*(|01> - |10>)"