Unitary transformation using Python

  • #1
35
3

Main Question or Discussion Point

I would like to ask about unitary transformation.

  1. UA(IV)
  2. UB*UA(IV)
  3. UAT(UB*UA(IV))=UB(IV)
  4. UB(IV)*(X)
  5. IVT(UB(IV)*(X))=UB(X)
  6. UBT*UB(X)=X
From the information above, UAT,IVT and UBT are the transpose of the complex conjugate. The aim of this code is to get the value of X in the step 4. This is the code:

Python:
import numpy as np
import math
import random
import cmath
from math import pi,e,log

y=60
z=60
UA=np.matrix([[1, 0, 0, 0], [0, math.cos(4*y), math.sin(4*y), 0], [0,
math.sin(4*y), -(math.cos(4*y)), 0], [0, 0, 0, -1]])
UB=np.matrix([[1, 0, 0, 0], [0, math.cos(4*z), math.sin(4*z), 0], [0,
math.sin(4*z), -(math.cos(4*z)), 0], [0, 0, 0, -1]])

IV=np.matrix('1 ;1 ;0 ;0')
X=np.matrix('1 ;0 ;1 ;0')
print (X)
one=UA*IV

print ("UAIV",one)
two=UB*one
print ("UBUAIV",two)
UAT=np.transpose(UA.real)
three=(UAT*two)
print("UAT(UBUA(IV))=UB(IV)",three)
a = np.squeeze(np.asarray(three))
b = np.squeeze(np.asarray(IV))
four=a*b
print ("UB(IV)(X)",four)
IVT=np.transpose(IV.real)
c=np.squeeze(np.asarray(four))
d=np.squeeze(np.asarray(IVT))
five=c*d
print ("IVT(UB(IV)(X))=UB(X)",five)
UBT=np.transpose(UB.real)
e=np.squeeze(np.asarray(UBT))
f=np.squeeze(np.asarray(five))
six=e*f
print ("UBTUB(X)=X",six)
However, I didn't get the result of X. Supposedly the result is
Code:
[[1]
[0]
[1]
[0]]
My wrong result:
daRcn.jpg


I hope anyone may help me on this matter. Thank you.
 

Attachments

Last edited:

Answers and Replies

  • #2
StoneTemplePython
Science Advisor
Gold Member
2019 Award
1,145
546
There's a button to insert properly formatted code into the message board -- see the "+" sign

if the matrix ##\mathbf Z## has nonzero imaginary components, then its adjoint (read: conjugate transpose) is given by

Python:
np.conj(Z).T
 
  • #3
35
3
There's a button to insert properly formatted code into the message board -- see the "+" sign

if the matrix ##\mathbf Z## has nonzero imaginary components, then its adjoint (read: conjugate transpose) is given by

Python:
np.conj(Z).T
What is the problem of my code? I didn't have any idea why the output is not equal to X.
 
  • #4
StoneTemplePython
Science Advisor
Gold Member
2019 Award
1,145
546
What is the problem of my code? I didn't have any idea why the output is not equal to X.
Look, you have now edited your original post to say generic "Code" not "Python".

Your code is not formatted and will not run -- in Python the whitespace is part of the code. What you've posted is unreadable by me or a machine. The fact that I did a ctrl F and didn't find a "conj" in there is a red flag.

If you want more help than this, you need to post readable code.


This is the code:

Code:
y=60
z=60
UA=np.matrix([[1, 0, 0, 0], [0, math.cos(4*y), math.sin(4*y), 0], [0,
math.sin(4*y), -(math.cos(4*y)), 0], [0, 0, 0, -1]])
UB=np.matrix([[1, 0, 0, 0], [0, math.cos(4*z), math.sin(4*z), 0], [0,
math.sin(4*z), -(math.cos(4*z)), 0], [0, 0, 0, -1]])

IV=np.matrix('1 ;1 ;0 ;0')
X=np.matrix('1 ;0 ;1 ;0')
print (X)
one=UA*IV

print ("UAIV",one)
two=UB*one
print ("UBUAIV",two)
UAT=np.transpose(UA.real)
three=(UAT*two)
print("UAT(UBUA(IV))=UB(IV)",three)
a = np.squeeze(np.asarray(three))
b = np.squeeze(np.asarray(IV))
four=a*b
print ("UB(IV)(X)",four)
IVT=np.transpose(IV.real)
c=np.squeeze(np.asarray(four))
d=np.squeeze(np.asarray(IVT))
five=c*d
print ("IVT(UB(IV)(X))=UB(X)",five)
UBT=np.transpose(UB.real)
e=np.squeeze(np.asarray(UBT))
f=np.squeeze(np.asarray(five))
six=e*f
print ("UBTUB(X)=X",six)
However, I didn't get the result of X. Supposedly the result is
Code:
[[1]
[0]
[1]
[0]]
My wrong result:
View attachment 228132
 
  • #5
35
3
Look, you have now edited your original post to say generic "Code" not "Python".

Your code is not formatted and will not run -- in Python the whitespace is part of the code. What you've posted is unreadable by me or a machine. The fact that I did a ctrl F and didn't find a "conj" in there is a red flag.

If you want more help than this, you need to post readable code.
Alright, I have make it readable as Python code. Thank you.
 
  • #6
StoneTemplePython
Science Advisor
Gold Member
2019 Award
1,145
546
Alright, I have make it readable as Python code. Thank you.
hmmmm, I was looking for more whitespace, but ummm I guess it's not in the books so to speak.

I don't really know where to start. Have you considered doing a small course that uses python + numpy? E.g.

https://www.edx.org/course/introduction-computational-thinking-data-mitx-6-00-2x-6

- - - -
If using numpy, you really should be getting random numbers and things like log from numpy -- random and math modules generally aren't to be used. I don't think I've ever had the need to use numpy squeeze though I've used numpy a ton.

some big stuff
np.matrix is actually a long deprecated type / option... np.array is much preferred officially and unofficially. Note that * acts arrays differently (i.e. it does elementwise multiplication) -- use @ for matrix multiplication if you are in python 3.x and using np.array ) When you entered
Python:
e=np.squeeze(np.asarray(UBT))
you switched types from numpy matrix to numpy array -- yet both are 'matrices' in math speak -- this is a recipe for huge problems. I highly recommend not using the "np.matrix" type at all.


This is the code:

Python:
import numpy as np
import math
import random
import cmath
from math import pi,e,log

y=60
z=60
UA=np.matrix([[1, 0, 0, 0], [0, math.cos(4*y), math.sin(4*y), 0], [0,
math.sin(4*y), -(math.cos(4*y)), 0], [0, 0, 0, -1]])
UB=np.matrix([[1, 0, 0, 0], [0, math.cos(4*z), math.sin(4*z), 0], [0,
math.sin(4*z), -(math.cos(4*z)), 0], [0, 0, 0, -1]])

IV=np.matrix('1 ;1 ;0 ;0')
X=np.matrix('1 ;0 ;1 ;0')
print (X)
one=UA*IV

print ("UAIV",one)
two=UB*one
print ("UBUAIV",two)
UAT=np.transpose(UA.real)
three=(UAT*two)
print("UAT(UBUA(IV))=UB(IV)",three)
a = np.squeeze(np.asarray(three))
b = np.squeeze(np.asarray(IV))
four=a*b
print ("UB(IV)(X)",four)
IVT=np.transpose(IV.real)
c=np.squeeze(np.asarray(four))
d=np.squeeze(np.asarray(IVT))
five=c*d
print ("IVT(UB(IV)(X))=UB(X)",five)
UBT=np.transpose(UB.real)
e=np.squeeze(np.asarray(UBT))
f=np.squeeze(np.asarray(five))
six=e*f
print ("UBTUB(X)=X",six)
However, I didn't get the result of X. Supposedly the result is
Code:
[[1]
[0]
[1]
[0]]
My wrong result:
View attachment 228132

I hope anyone may help me on this matter. Thank you.
otherwise I'm not so sure what it is you're trying to do. Surely a much simpler example with less lines can illustrate how np.conj works, which is still not in the code.
 
  • #7
35
3
hmmmm, I was looking for more whitespace, but ummm I guess it's not in the books so to speak.

I don't really know where to start. Have you considered doing a small course that uses python + numpy? E.g.

https://www.edx.org/course/introduction-computational-thinking-data-mitx-6-00-2x-6

- - - -
If using numpy, you really should be getting random numbers and things like log from numpy -- random and math modules generally aren't to be used. I don't think I've ever had the need to use numpy squeeze though I've used numpy a ton.

some big stuff
np.matrix is actually a long deprecated type / option... np.array is much preferred officially and unofficially. Note that * acts arrays differently (i.e. it does elementwise multiplication) -- use @ for matrix multiplication if you are in python 3.x and using np.array ) When you entered
Python:
e=np.squeeze(np.asarray(UBT))
you switched types from numpy matrix to numpy array -- yet both are 'matrices' in math speak -- this is a recipe for huge problems. I highly recommend not using the "np.matrix" type at all.




otherwise I'm not so sure what it is you're trying to do. Surely a much simpler example with less lines can illustrate how np.conj works, which is still not in the code.
Consider the simple example:

Given the set of matrix,supposedly the transpose of conjugate IV will obtain the UB:
Python:
IV_transpose*UB(IV)=UB
However, I get the different output. Please see the code;

Python:
angle=60
UB = np.array([
    [1,0,0,0],
    [0,math.cos(4*math.radians(angle)),math.sin(4*math.radians(angle)),0],
    [0, math.sin(4 * math.radians(angle)), -math.cos(4
    *math.radians(angle)),0],[0,0,0,-1]])

IV = [[1.0],[1.0],[0.0],[0.0]]

IVT=np.conj(IV).T
UBIV=np.matmul(UB,IV)
output=np.matmul(IVT,UBIV)
print (output)
print (UB)
This is my output:
Python:
IV_transpose*UB(IV)=[[ 0.5]]
UB=
[[ 1.         0.         0.         0.       ]
[ 0.        -0.5       -0.8660254  0.       ]
[ 0.        -0.8660254  0.5        0.       ]
[ 0.         0.         0.        -1.       ]]
My question is why
Python:
IV_transpose*UB(IV)!=UB
. How to make it equal? Thank you.
 
  • #8
StoneTemplePython
Science Advisor
Gold Member
2019 Award
1,145
546
Consider the simple example:

Given the set of matrix,supposedly the transpose of conjugate IV will obtain the UB:
Python:
IV_transpose*UB(IV)=UB
However, I get the different output. Please see the code;

Python:
angle=60
UB = np.array([
    [1,0,0,0],
    [0,math.cos(4*math.radians(angle)),math.sin(4*math.radians(angle)),0],
    [0, math.sin(4 * math.radians(angle)), -math.cos(4
    *math.radians(angle)),0],[0,0,0,-1]])

IV = [[1.0],[1.0],[0.0],[0.0]]

IVT=np.conj(IV).T
UBIV=np.matmul(UB,IV)
output=np.matmul(IVT,UBIV)
print (output)
print (UB)
This is my output:
Python:
IV_transpose*UB(IV)=[[ 0.5]]
UB=
[[ 1.         0.         0.         0.       ]
[ 0.        -0.5       -0.8660254  0.       ]
[ 0.        -0.8660254  0.5        0.       ]
[ 0.         0.         0.        -1.       ]]
My question is why
Python:
IV_transpose*UB(IV)!=UB
. How to make it equal? Thank you.

for starters your "IV" is a list not a numpy array. Additionally you have it as a real valued vector (i.e. 1-D, not at 2-D matrix) with 4 components in it. It isn't clear to me what this has to do with being unitary or why you would take the conjugate transpose of this.

Assuming we're dealing with matrices and vectors that have at least 2 components (i.e. not scalars) you can never use a rank one matrix to ever effect a unitary similarity transform. But that is what you seem to be doing with "IV_transpose*UB(IV)". Furthermore even if this was a unitary similarity transform you expect a similar matrix, almost never the exact same matrix back so I don't know why you've flagged that they are not equal.

I really am not in a position to give a tutorial on numpy 101 -- but from these postings it is clear that you need one. There's also a huge problem with what you've posted in terms of basic linear algebra concepts-- at least in this case I can recommend that you look through Linear Algebra Done Wrong, freely available from the author here:

https://www.math.brown.edu/~treil/papers/LADW/LADW_2017-09-04.pdf

good luck
 

Related Threads on Unitary transformation using Python

Replies
1
Views
2K
Replies
4
Views
3K
  • Last Post
Replies
4
Views
237
Replies
1
Views
1K
Replies
1
Views
335
Replies
0
Views
1K
  • Last Post
Replies
8
Views
3K
Replies
2
Views
4K
Replies
9
Views
12K
Replies
21
Views
1K
Top