Some states pass through a gate almost untouched: the gate only multiplies them by a number. A state |u\rangle with
is an eigenvector of the matrix U, and \lambda is its eigenvalue. A unitary gate never changes the length of a vector, so |\lambda| = 1 and \lambda is a pure turn: \lambda = e^{2\pi i\varphi} for exactly one \varphi in [0, 1), the eigenphase. It counts the turn in whole turns: \varphi = 0.25 is a quarter turn (\lambda = i), \varphi = 0.5 is \lambda = -1, and \varphi = 0.75 is \lambda = -i.
Write eigenphase(U, v) that returns the eigenphase \varphi of the normalised vector v under the unitary matrix U (a list of rows, U[row][col]; the gate acts as new = U @ v), or None if v is not an eigenvector of U.
vcounts as an eigenvector when some number \lambda makes every entry of U v equal to \lambda times the same entry of v, to within1e-9. The tests stay far from that line: a vector that is not an eigenvector misses by more than0.01in some entry.- The answer lies in [0, 1). Rounding can turn an eigenvalue of exactly 1 into a phase like
0.9999999999999999: return0.0whenever your \varphi is within1e-9of 1. No test has an eigenphase in (0.999999, 1).
Examples
Input: U = [[0, 1], [1, 0]], v = [0.7071067811865476, -0.7071067811865476]
Output: 0.5
Explanation: X swaps the two entries, which gives -v: lambda = -1 = e^(2 pi i * 0.5).
Input: U = [[1, 0], [0, (0.7071067811865476+0.7071067811865476j)]], v = [0, 1]
Output: 0.125
Explanation: the T gate multiplies |1> by e^(i pi/4), an eighth of a turn.
Input: U = [[0.7071067811865476, 0.7071067811865476], [0.7071067811865476, -0.7071067811865476]], v = [1, 0]
Output: None
Explanation: H sends |0> to (|0> + |1>)/sqrt 2, which is not a multiple of |0>.
Answers are compared with a tolerance of 1e-6.
Goals
- Test whether a vector is an eigenvector of a unitary matrix
- Write an eigenvalue of modulus 1 as $e^{2\pi i\varphi}$ with $\varphi$ in $[0, 1)$