Is there a way in numpy to test whether a matrix is Unitary

2024/10/15 8:27:38

I was wondering if there is any function in numpy to determine whether a matrix is Unitary?

This is the function I wrote but it is not working. I would be thankful if you guys can find an error in my function and/or tell me another way to find out if a given matrix is unitary.

def is_unitary(matrix: np.ndarray) -> bool:unitary = Truen = matrix.sizeerror = np.linalg.norm(np.eye(n) - matrix.dot( matrix.transpose().conjugate()))if not(error < np.finfo(matrix.dtype).eps * 10.0 *n):unitary = Falsereturn unitary
Answer

Let's take an obviously unitary array:

>>> a = 0.7
>>> b = (1-a**2)**0.5
>>> m = np.array([[a,b],[-b,a]])
>>> m.dot(m.conj().T)
array([[ 1.,  0.],[ 0.,  1.]])

and try your function on it:

>>> is_unitary(m)
Traceback (most recent call last):File "<ipython-input-28-8dc9ddb462bc>", line 1, in <module>is_unitary(m)File "<ipython-input-20-3758c2016b67>", line 5, in is_unitaryerror = np.linalg.norm(np.eye(n) - matrix.dot( matrix.transpose().conjugate()))
ValueError: operands could not be broadcast together with shapes (4,4) (2,2) 

which happens because

>>> m.size
4
>>> np.eye(m.size)
array([[ 1.,  0.,  0.,  0.],[ 0.,  1.,  0.,  0.],[ 0.,  0.,  1.,  0.],[ 0.,  0.,  0.,  1.]])

If we replace n = matrix.size with len(m) or m.shape[0] or something, we get

>>> is_unitary(m)
True

I might just use

>>> np.allclose(np.eye(len(m)), m.dot(m.T.conj()))
True

where allclose has rtol and atol parameters.

https://en.xdnf.cn/q/69308.html

Related Q&A

Two unique marker symbols for one legend

I would like to add a "red filled square" symbol beside the "red filled circle" symbol under legend. How do I achieve this? I prefer to stick with pyplot rather than pylab. Below i…

What is Rubys equivalent to Pythons multiprocessing module?

To get real concurrency in Ruby or Python, I need to create new processes. Python makes this pretty straightforward using the multiprocessing module, which abstracts away all the fork / wait goodness a…

Using grep in python

There is a file (query.txt) which has some keywords/phrases which are to be matched with other files using grep. The last three lines of the following code are working perfectly but when the same comma…

shuffling a list with restrictions in Python

I have a problem with randomizing a list with restrictions in Python (3). I have seen a few other questions relating to this, but none of them really seem to solve my problem. Im a beginner, so any hel…

Django: Is it reasonable to use objects as dictionary keys?

Is it reasonable to use objects as keys to a dictionary in django? I have done so and it works. But I am wondering if this is best practice, or if it is going to make difficulties I dont foresee righ…

How do I now (since June 2022) send an email via Gmail using a Python script?

I had a Python script which did this. I had to enable something in the Gmail account. For maybe 3 years the script then ran like this: import smtplib, ssl ... subject = some subject message body = &quo…

Fast relational database for simple use with Python [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Event handling with Jython Swing

Im making a GUI by using Swing from Jython. Event handling seems to be particularly elegant from Jython, just setJButton("Push me", actionPerformed = nameOfFunctionToCall)However, trying same…

How Does Deque Work in Python

I am having trouble understanding how the deque works in the snippet of code below, while trying to recreate a queue and a stack in Python.Stack Example - Understoodstack = ["a", "b"…

When to use generator functions and when to use loops in Python

I am coming from a Matlab background and I am finding it difficult to get around the concept of generators in Python. Can someone please answer me the following:The difference between a generator funct…