diff --git a/abp/make_tables.py b/abp/make_tables.py index 877a37a..02c4087 100644 --- a/abp/make_tables.py +++ b/abp/make_tables.py @@ -16,6 +16,7 @@ from clifford import decompositions def find_clifford(needle, haystack): """ Find the index of a given u within a list of unitaries, up to a global phase """ + needle = normalize_global_phase(needle) for i, t in enumerate(haystack): for phase in range(8): if np.allclose(t, np.exp(1j * phase * np.pi / 4.) * needle): @@ -23,6 +24,16 @@ def find_clifford(needle, haystack): raise IndexError +def normalize_global_phase(m): + """ Normalize the global phase of a matrix """ + f = m.flatten() + v = f[np.flatnonzero(f)[0]] + print v + phase = np.arctan2(v.imag, v.real) % np.pi + rot = np.exp(-1j*phase) + return rot * m if rot * v > 0 else -rot*m + + def find_cz(bond, c1, c2, commuters, state_table): """ Find the output of a CZ operation """ # Figure out the target state @@ -47,7 +58,8 @@ def find_cz(bond, c1, c2, commuters, state_table): def compose_u(decomposition): """ Get the unitary representation of a particular decomposition """ matrices = ({"x": qi.sqx, "z": qi.msqz}[c] for c in decomposition) - return reduce(np.dot, matrices, np.matrix(np.eye(2, dtype=complex))) + output = reduce(np.dot, matrices, np.matrix(np.eye(2, dtype=complex))) + return normalize_global_phase(output) def get_unitaries(): @@ -79,7 +91,7 @@ def get_state_table(unitaries): for bond, i, j in tqdm(params, desc="Building state table"): state = qi.bond if bond else qi.nobond kp = np.kron(unitaries[i], unitaries[j]) - state_table[bond, i, j, :] = np.dot(kp, state).T + state_table[bond, i, j, :] = normalize_global_phase(np.dot(kp, state).T) return state_table diff --git a/tests/test_normalize_global_phase.py b/tests/test_normalize_global_phase.py new file mode 100644 index 0000000..b1e8d2e --- /dev/null +++ b/tests/test_normalize_global_phase.py @@ -0,0 +1,11 @@ +from abp import make_tables +from abp import qi +import numpy as np + +def test_normalize_global_phase(): + for i in range(10): + u = qi.pz + phase = np.random.uniform(0, 2*np.pi) + m = np.exp(1j*phase) * u + normalized = make_tables.normalize_global_phase(m) + assert np.allclose(normalized, u)