ProjectQ-Framework / ProjectQ

ProjectQ: An open source software framework for quantum computing
https://projectq.ch
Apache License 2.0
875 stars 271 forks source link

Fails to decompose single qubit unitary #224

Open emanuel-malvetti opened 6 years ago

emanuel-malvetti commented 6 years ago

The following matrix cannot be decomposed even though it is unitary.

U = np.matrix([[(-0.7108860402090058-0.7033072016973199j),
                    (-9.403468524726843e-05+9.504800300819127e-05j)],
                   [(-9.507314515605492e-05+9.400926537078691e-05j),
                    (-0.7031170805491339-0.7110740841596025j)]])

In fact running np.linalg.norm(U.getH()*U - np.eye(2)) returns 5.462198399343198e-16.

Reducing TOLERANCE in arb1qubit2rzandry.py from 1e-12 to 1e-10 removes the exception, so I assume this is caused by cancellation effects, similar to what happens when solving the quadratic equation: https://en.wikipedia.org/wiki/Loss_of_significance#Instability_of_the_quadratic_equation

Exception: ("Couldn't find parameters for matrix ", [[(-0.7108860402090058-0.7033072016973199j), (-9.403468524726843e-05+9.504800300819127e-05j)], [(-9.507314515605492e-05+9.400926537078691e-05j), (-0.7031170805491339-0.7110740841596025j)]], "This shouldn't happen. Maybe the matrix is not unitary?")
 raised in:
'  File "/Users/isaac/Library/Python/3.5/lib/python/site-packages/projectq/setups/decompositions/arb1qubit2rzandry.py", line 192, in _find_parameters'
'    "not unitary?")'
thomashaener commented 6 years ago

Thanks for pointing this out! I will try to make the current implementation more robust as soon as I find time.