def test_kron_factor_special_unitaries(f1, f2): p = cirq.kron(f1, f2) g, g1, g2 = cirq.kron_factor_4x4_to_2x2s(p) assert np.allclose(cirq.kron(g1, g2), p) assert abs(g - 1) < 0.000001 assert cirq.is_special_unitary(g1) assert cirq.is_special_unitary(g2)
def test_kak_canonicalize_vector(x, y, z): i = np.eye(2) m = cirq.unitary( cirq.KakDecomposition( global_phase=1, single_qubit_operations_after=(i, i), interaction_coefficients=(x, y, z), single_qubit_operations_before=(i, i), )) kak = cirq.kak_canonicalize_vector(x, y, z, atol=1e-10) a1, a0 = kak.single_qubit_operations_after x2, y2, z2 = kak.interaction_coefficients b1, b0 = kak.single_qubit_operations_before m2 = cirq.unitary(kak) assert 0.0 <= x2 <= np.pi / 4 assert 0.0 <= y2 <= np.pi / 4 assert -np.pi / 4 < z2 <= np.pi / 4 assert abs(x2) >= abs(y2) >= abs(z2) assert x2 < np.pi / 4 - 1e-10 or z2 >= 0 assert cirq.is_special_unitary(a1) assert cirq.is_special_unitary(a0) assert cirq.is_special_unitary(b1) assert cirq.is_special_unitary(b0) assert np.allclose(m, m2)
def test_kron_factor_special_unitaries(f1, f2): p = cirq.kron(f1, f2) g, g1, g2 = cirq.kron_factor_4x4_to_2x2s(p) assert np.allclose(cirq.kron(g1, g2), p) assert abs(g - 1) < 0.000001 assert cirq.is_special_unitary(g1) assert cirq.is_special_unitary(g2) assert_kronecker_factorization_within_tolerance(p, g, g1, g2)
def recompose_so4(a: np.ndarray, b: np.ndarray) -> np.ndarray: assert a.shape == (2, 2) assert b.shape == (2, 2) assert cirq.is_special_unitary(a) assert cirq.is_special_unitary(b) magic = np.array([[1, 0, 0, 1j], [0, 1j, 1, 0], [0, 1j, -1, 0], [1, 0, 0, -1j]]) * np.sqrt(0.5) result = np.real(cirq.dot(np.conj(magic.T), cirq.kron(a, b), magic)) assert cirq.is_orthogonal(result) return result
def recompose_so4(a: np.ndarray, b: np.ndarray) -> np.ndarray: assert a.shape == (2, 2) assert b.shape == (2, 2) assert cirq.is_special_unitary(a) assert cirq.is_special_unitary(b) magic = np.array([[1, 0, 0, 1j], [0, 1j, 1, 0], [0, 1j, -1, 0], [1, 0, 0, -1j]]) * np.sqrt(0.5) result = np.real(cirq.dot(np.conj(magic.T), cirq.kron(a, b), magic)) assert cirq.is_orthogonal(result) return result
def su_to_gates(m): """Decomposes two level special unitaries to Ry and Rz gates Args: m: Matrix to be converted into gates Returns: result: Returns list of gates to be applied Raises: AssertionError: Matrix m is not a special unitary matrix """ assert cirq.is_special_unitary(m) u00 = m[0, 0] u01 = m[0, 1] theta = np.arccos(np.abs(u00)) lmbda = np.angle(u00) mu = np.angle(u01) result = [] if np.abs(lmbda - mu) > 1e-9: result.append(('Rz', lmbda - mu)) if np.abs(theta) > 1e-9: result.append(('Ry', 2 * theta)) if np.abs(lmbda + mu) > 1e-9: result.append(('Rz', lmbda + mu)) return result
def test_kak_canonicalize_vector(x, y, z): i = np.eye(2) m = recompose_kak(1, (i, i), (x, y, z), (i, i)) g, (a1, a0), (x2, y2, z2), (b1, b0) = cirq.kak_canonicalize_vector( x, y, z) m2 = recompose_kak(g, (a1, a0), (x2, y2, z2), (b1, b0)) assert 0.0 <= x2 <= np.pi / 4 assert 0.0 <= y2 <= np.pi / 4 assert -np.pi / 4 <= z2 <= np.pi / 4 assert abs(x2) >= abs(y2) >= abs(z2) assert cirq.is_special_unitary(a1) assert cirq.is_special_unitary(a0) assert cirq.is_special_unitary(b1) assert cirq.is_special_unitary(b0) assert np.allclose(m, m2)
def test_is_special_unitary_tolerance(): atol = 0.5 # Pays attention to specified tolerance. assert cirq.is_special_unitary(np.array([[1, 0], [-0.5, 1]]), atol=atol) assert not cirq.is_special_unitary(np.array([[1, 0], [-0.6, 1]]), atol=atol) assert cirq.is_special_unitary(np.array([[1, 0], [0, 1]]) * cmath.exp(1j * 0.1), atol=atol) assert not cirq.is_special_unitary(np.array([[1, 0], [0, 1]]) * cmath.exp(1j * 0.3), atol=atol) # Error isn't accumulated across entries, except for determinant factors. assert cirq.is_special_unitary(np.array([[1.2, 0, 0], [0, 1.2, 0], [0, 0, 1 / 1.2]]), atol=atol) assert not cirq.is_special_unitary(np.array([[1.2, 0, 0], [0, 1.2, 0], [0, 0, 1.2]]), atol=atol) assert not cirq.is_special_unitary( np.array([[1.2, 0, 0], [0, 1.3, 0], [0, 0, 1 / 1.2]]), atol=atol )
def test_to_special(): u = cirq.testing.random_unitary(4) su = cirq.to_special(u) assert not cirq.is_special_unitary(u) assert cirq.is_special_unitary(su)
def test_is_special_unitary(): assert cirq.is_special_unitary(np.empty((0, 0))) assert not cirq.is_special_unitary(np.empty((1, 0))) assert not cirq.is_special_unitary(np.empty((0, 1))) assert cirq.is_special_unitary(np.array([[1]])) assert not cirq.is_special_unitary(np.array([[-1]])) assert not cirq.is_special_unitary(np.array([[5]])) assert not cirq.is_special_unitary(np.array([[3j]])) assert not cirq.is_special_unitary(np.array([[1, 0], [0, -2]])) assert not cirq.is_special_unitary(np.array([[1, 0], [0, -1]])) assert cirq.is_special_unitary(np.array([[-1, 0], [0, -1]])) assert not cirq.is_special_unitary(np.array([[1j, 0], [0, 1]])) assert cirq.is_special_unitary(np.array([[1j, 0], [0, -1j]])) assert not cirq.is_special_unitary(np.array([[1, 0], [1, 1]])) assert not cirq.is_special_unitary(np.array([[1, 1], [0, 1]])) assert not cirq.is_special_unitary(np.array([[1, 1], [1, 1]])) assert not cirq.is_special_unitary(np.array([[1, -1], [1, 1]])) assert cirq.is_special_unitary(np.array([[1, -1], [1, 1]]) * np.sqrt(0.5)) assert cirq.is_special_unitary(np.array([[1, 1j], [1j, 1]]) * np.sqrt(0.5)) assert not cirq.is_special_unitary( np.array([[1, -1j], [1j, 1]]) * np.sqrt(0.5)) assert cirq.is_special_unitary( np.array([[1, 1j + 1e-11], [1j, 1 + 1j * 1e-9]]) * np.sqrt(0.5))