CQCL / tket

Source code for the TKET quantum compiler, Python bindings and utilities
https://tket.quantinuum.com/
Apache License 2.0
243 stars 48 forks source link

Work around numerical instability in `get_information_content()` #488

Open cqc-alec opened 1 year ago

cqc-alec commented 1 year ago

When Eigen::SelfAdjointEigenSolver (called during KAK decomposition) finds repeated eigenvalues in a matrix, the eigenvectors and their order are not uniquely determined. Rounding errors lead to different results being returned on different platforms. We should find a way to normalize the decomposition in the presence of rounding errors. It seems that some decompositions are better than others when it comes to finding further Clifford simplifications. This needs further investigation.

cqc-alec commented 1 year ago

Partially solved by #491 . However, some issues remain. See this comparison of logs showing the results of the eigensolver and the resulting circuits on Linux and MacOS (from #498 ).

Linux:

============ Eigendecomposition =============
eigvals = 
(0,-1)
(0,-1)
 (0,1)
 (0,1)
eigvecs =
 (0.707107,0)         (0,0)         (0,0)  (0.707107,0)
        (0,0) (-0.707107,0) (-0.707107,0)         (0,0)
(-0.707107,0)         (0,0)         (0,0)  (0.707107,0)
        (0,0)  (0.707107,0) (-0.707107,0)         (0,0)
=============================================
============ Eigendecomposition =============
eigvals = 
(0,-1)
(0,-1)
 (0,1)
 (0,1)
eigvecs =
 (0.707107,0)         (0,0)         (0,0)  (0.707107,0)
        (0,0) (-0.707107,0) (-0.707107,0)         (0,0)
(-0.707107,0)         (0,0)         (0,0)  (0.707107,0)
        (0,0)  (0.707107,0) (-0.707107,0)         (0,0)
=============================================
TK1(0, 1, 0) q[0];
TK1(0, 3.5, 1) q[1];
TK2(0.5, 0, 0) q[0], q[1];
TK1(0, 3.5, 0.2) q[1];
TK2(0.5, 0, 0) q[0], q[1];
TK1(0.5, 0.5, 0.5) q[0];
TK1(0, 0, 1) q[1];
Phase (in half-turns): 0.0

============== (SythesiseTK)
============ Eigendecomposition =============
eigvals = 
(-0.809017,-0.587785)
(-0.809017,-0.587785)
 (-0.809017,0.587785)
 (-0.809017,0.587785)
eigvecs =
       (-0,0) (-0.707107,0)  (0.707107,0)         (0,0)
 (0.707107,0)        (-0,0)         (0,0) (-0.707107,0)
 (0.707107,0)        (-0,0)         (0,0)  (0.707107,0)
       (-0,0) (-0.707107,0) (-0.707107,0)         (0,0)
=============================================
TK1(0, 1, 0) q[0];
TK1(0, 3.5, 1) q[1];
TK1(0, 0, 1) q[0];
TK1(0, 0, 0.5) q[1];
TK2(0.2, 0, 0) q[0], q[1];
TK1(0.5, 3.5, 0.5) q[0];
TK1(0, 1.5, 0.5) q[1];
Phase (in half-turns): 1.5

============== (KAK Decomposition)

MacOS:

============ Eigendecomposition =============
eigvals = 
(0,-1)
(0,-1)
 (0,1)
 (0,1)
eigvecs =
 (0.707107,0)         (0,0)         (0,0)  (0.707107,0)
        (0,0) (-0.707107,0) (-0.707107,0)         (0,0)
(-0.707107,0)         (0,0)         (0,0)  (0.707107,0)
        (0,0)  (0.707107,0) (-0.707107,0)         (0,0)
=============================================
============ Eigendecomposition =============
eigvals = 
(0,-1)
(0,-1)
 (0,1)
 (0,1)
eigvecs =
 (0.707107,0)         (0,0)         (0,0)  (0.707107,0)
        (0,0) (-0.707107,0) (-0.707107,0)         (0,0)
(-0.707107,0)         (0,0)         (0,0)  (0.707107,0)
        (0,0)  (0.707107,0) (-0.707107,0)         (0,0)
=============================================
TK1(0, 1, 0) q[0];
TK1(0, 3.5, 1) q[1];
TK2(0.5, 0, 0) q[0], q[1];
TK1(0, 3.5, 0.2) q[1];
TK2(0.5, 0, 0) q[0], q[1];
TK1(0.5, 0.5, 0.5) q[0];
TK1(0, 0, 1) q[1];
Phase (in half-turns): 0.0

============== (SythesiseTK)
============ Eigendecomposition =============
eigvals = 
(-0.809017,-0.587785)
(-0.809017,-0.587785)
 (-0.809017,0.587785)
 (-0.809017,0.587785)
eigvecs =
       (-0,0) (-0.707107,0)  (0.707107,0)         (0,0)
(-0.707107,0)        (-0,0)         (0,0)  (0.707107,0)
(-0.707107,0)        (-0,0)         (0,0) (-0.707107,0)
       (-0,0) (-0.707107,0) (-0.707107,0)         (0,0)
=============================================
TK1(0, 1, 0) q[0];
TK1(0, 3.5, 1) q[1];
TK1(0, 0, 1.5) q[1];
TK2(0.2, 0, 0) q[0], q[1];
TK1(0.5, 3.5, 3.5) q[0];
TK1(0, 3.5, 3.5) q[1];
Phase (in half-turns): 1.5

============== (KAK Decomposition)
github-actions[bot] commented 3 weeks ago

This issue has been automatically marked as stale.