# Unitary transformation using Python

• Python

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:
[


]

My wrong result: I hope anyone may help me on this matter. Thank you.

#### Attachments

Last edited:

StoneTemplePython
Gold Member
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

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.

StoneTemplePython
Gold Member
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:
[


]

My wrong result:
View attachment 228132

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.

StoneTemplePython
Gold Member
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:
[


]

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.

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],

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.

StoneTemplePython
Gold Member
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],

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: