# Unitary transformation using Python

In summary: The * operator: when applied to numpy arrays is element-wise, so you can't use it for matrix multiplication. You can use the np.dot function or the @ operator (if using python 3.x and using numpy arrays) to do matrix multiplication. In summary, the code provided is attempting to calculate the value of X using unitary transformations and transpose of the complex conjugate. However, there are several issues with the code such as using deprecated types and not using proper numpy functions for matrix multiplication. It is recommended to take a course on using numpy and to simplify the code to better understand how to use numpy functions such as np.conj.

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:

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

#### Attachments

• daRcn.jpg
18.8 KB · Views: 1,303
Last edited:
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

StoneTemplePython said:
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.

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

StoneTemplePython said:
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.

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.

StoneTemplePython said:
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.

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:

good luck

## 1. What is a unitary transformation?

A unitary transformation is a type of linear transformation that preserves the length of vectors and the angles between them. In other words, it is a transformation that does not distort the shape or size of a vector.

## 2. Why is unitary transformation important in scientific computing?

Unitary transformation is important in scientific computing because it allows for efficient and accurate manipulation of complex data sets. It is particularly useful in quantum mechanics and signal processing applications.

## 3. How can unitary transformation be implemented in Python?

In Python, unitary transformation can be implemented using the NumPy library. The NumPy function numpy.linalg.eig can be used to calculate the eigenvalues and eigenvectors of a matrix, which are necessary for performing a unitary transformation.

## 4. Can unitary transformation be applied to non-square matrices?

No, unitary transformation can only be applied to square matrices. This is because the transformation requires the matrix to be invertible, and non-square matrices do not have an inverse.

## 5. Are there any real-world applications of unitary transformation?

Yes, unitary transformation has numerous real-world applications, including image and audio compression, quantum computing, and data encryption. It is also used in various fields of science, such as physics, chemistry, and engineering.

• Programming and Computer Science
Replies
5
Views
2K
• Programming and Computer Science
Replies
8
Views
791
• Programming and Computer Science
Replies
1
Views
2K
• Programming and Computer Science
Replies
21
Views
4K
• Programming and Computer Science
Replies
1
Views
1K
• Programming and Computer Science
Replies
3
Views
3K
• Programming and Computer Science
Replies
1
Views
1K
• Programming and Computer Science
Replies
7
Views
3K
• Programming and Computer Science
Replies
17
Views
2K
• Programming and Computer Science
Replies
2
Views
16K