Repository: Qiskit/qiskit

Issue:
Generation of random unitary matrix does not draw matrices from Haar measure
https://github.com/QISKit/qiskit-sdk-py/blob/5c1c68a5aa3dcccdf5c10f9eb307383ebb40826b/qiskit/tools/qi/qi.py#L346

The method of generation random unitary matrices is faulty. It will not generate random matrices drawn from the Haar measure.

Please refer to  "How to generate random matrices from the classical compact groups", Francesco Mezzadri, arXiv:math-ph/0609050 p.11.
Generation of random unitary matrix does not draw matrices from Haar measure
https://github.com/QISKit/qiskit-sdk-py/blob/5c1c68a5aa3dcccdf5c10f9eb307383ebb40826b/qiskit/tools/qi/qi.py#L346

The method of generation random unitary matrices is faulty. It will not generate random matrices drawn from the Haar measure.

Please refer to  "How to generate random matrices from the classical compact groups", Francesco Mezzadri, arXiv:math-ph/0609050 p.11.


Discussion:
I would not say it is faulty.  It just doesn't draw from the Haar measure.

On Tue, May 29, 2018 at 10:53 AM Piotr Gawron <notifications@github.com>
wrote:

>
> https://github.com/QISKit/qiskit-sdk-py/blob/5c1c68a5aa3dcccdf5c10f9eb307383ebb40826b/qiskit/tools/qi/qi.py#L346
>
> The method of generation random unitary matrices is faulty. It will not
> generate random matrices drawn from the Haar measure.
>
> Please refer to "How to generate random matrices from the classical
> compact groups", Francesco Mezzadri, arXiv:math-ph/0609050 p.11.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/QISKit/qiskit-sdk-py/issues/519>, or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABMPqXuOYjjJWcOUXwfsonp2rfbyxT_wks5t3WD5gaJpZM4URpzj>
> .
>

I agree with @pgawron we should update this to be from the Haar measure. After we do this i agree @nonhermitian we should also make the documentation clear and say a random martix from the Haar measure. 
If this is addressable by improving the documentation, let's do it as soon as possible. Is this the case?
No it needs to write a new algorithm. Its simple but I need to find some time 
How about using scipy.stats.unitary_group?

https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.unitary_group.html
@ikkoham i agree. Can you just submit a Pr and then we can close this issue.  I would put a test with it as well. 
Thanks. OK, I will submit a PR.
I would not say it is faulty.  It just doesn't draw from the Haar measure.

On Tue, May 29, 2018 at 10:53 AM Piotr Gawron <notifications@github.com>
wrote:

>
> https://github.com/QISKit/qiskit-sdk-py/blob/5c1c68a5aa3dcccdf5c10f9eb307383ebb40826b/qiskit/tools/qi/qi.py#L346
>
> The method of generation random unitary matrices is faulty. It will not
> generate random matrices drawn from the Haar measure.
>
> Please refer to "How to generate random matrices from the classical
> compact groups", Francesco Mezzadri, arXiv:math-ph/0609050 p.11.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/QISKit/qiskit-sdk-py/issues/519>, or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABMPqXuOYjjJWcOUXwfsonp2rfbyxT_wks5t3WD5gaJpZM4URpzj>
> .
>

I agree with @pgawron we should update this to be from the Haar measure. After we do this i agree @nonhermitian we should also make the documentation clear and say a random martix from the Haar measure. 
If this is addressable by improving the documentation, let's do it as soon as possible. Is this the case?
No it needs to write a new algorithm. Its simple but I need to find some time 
How about using scipy.stats.unitary_group?

https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.unitary_group.html
@ikkoham i agree. Can you just submit a Pr and then we can close this issue.  I would put a test with it as well. 
Thanks. OK, I will submit a PR.

Resolving patch:
diff --git a/qiskit/tools/qi/qi.py b/qiskit/tools/qi/qi.py
--- a/qiskit/tools/qi/qi.py
+++ b/qiskit/tools/qi/qi.py
@@ -18,6 +18,7 @@
 
 import numpy as np
 import scipy.linalg as la
+from scipy.stats import unitary_group
 
 from qiskit import QISKitError
 from qiskit.tools.qi.pauli import pauli_group
@@ -335,8 +336,7 @@ def random_unitary_matrix(length):
     Returns:
         ndarray: U (length, length) unitary ndarray.
     """
-    q_matrix = la.qr(__ginibre_matrix(length))[0]  # Get only the first element
-    return q_matrix
+    return unitary_group.rvs(length)
 
 
 def random_density_matrix(length, rank=None, method='Hilbert-Schmidt'):


Test patch: