def recompose_kak(g, a, v, b) -> np.ndarray: a1, a0 = a x, y, z = v b1, b0 = b xx = cirq.kron(X, X) yy = cirq.kron(Y, Y) zz = cirq.kron(Z, Z) a = cirq.kron(a1, a0) m = cirq.map_eigenvalues(xx * x + yy * y + zz * z, lambda e: np.exp(1j * e)) b = cirq.kron(b1, b0) return cirq.dot(a, m, b) * g
def test_map_eigenvalues_raise(matrix, exponent, desired): exp_mapped = cirq.map_eigenvalues(matrix, lambda e: complex(e)**exponent) assert np.allclose(desired, exp_mapped)
def test_map_eigenvalues_identity(matrix): identity_mapped = cirq.map_eigenvalues(matrix, lambda e: e) assert np.allclose(matrix, identity_mapped)
]) * np.sqrt(0.5)), (1, np.array([ [1, 0, 0, -1j], [0, 1, -1j, 0], [0, -1j, 1, 0], [-1j, 0, 0, 1], ]) * np.sqrt(0.5)), (1, np.array([ [1, 0, 0, 1j], [0, 1, -1j, 0], [0, -1j, 1, 0], [1j, 0, 0, 1], ]) * np.sqrt(0.5)), (1.5, cirq.map_eigenvalues(cirq.unitary(cirq.SWAP), lambda e: e**0.5)), (2, cirq.unitary(cirq.SWAP).dot(cirq.unitary(cirq.CZ))), (3, cirq.unitary(cirq.SWAP)), (3, np.array([ [0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0j], ])), ] + [(1, _random_single_MS_effect()) for _ in range(10)] + [(3, cirq.testing.random_unitary(4)) for _ in range(10)] + [(2, _random_double_MS_effect()) for _ in range(10)]) def test_two_to_ops(max_ms_depth: int, effect: np.array): q0 = cirq.NamedQubit('q0') q1 = cirq.NamedQubit('q1')
]) * np.sqrt(0.5)), (1, 1, np.array([ [1, 0, 0, -1j], [0, 1, -1j, 0], [0, -1j, 1, 0], [-1j, 0, 0, 1], ]) * np.sqrt(0.5)), (1, 1, np.array([ [1, 0, 0, 1j], [0, 1, -1j, 0], [0, -1j, 1, 0], [1j, 0, 0, 1], ]) * np.sqrt(0.5)), (1.5, 3, cirq.map_eigenvalues(cirq.SWAP.matrix(), lambda e: e**0.5)), (2, 2, cirq.SWAP.matrix().dot(cirq.CZ.matrix())), (3, 3, cirq.SWAP.matrix()), (3, 3, np.array([ [0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0j], ])), ] + [(1, 2, _random_single_partial_cz_effect()) for _ in range(10)] + [(2, 2, _random_double_full_cz_effect()) for _ in range(10)] + [(2, 3, _random_double_partial_cz_effect()) for _ in range(10)] + [(3, 3, cirq.testing.random_unitary(4)) for _ in range(10)])
def test_map_eigenvalues_raise(matrix, exponent, desired): exp_mapped = cirq.map_eigenvalues(matrix, lambda e: complex(e)**exponent) assert np.allclose(desired, exp_mapped)
def test_map_eigenvalues_identity(matrix): identity_mapped = cirq.map_eigenvalues(matrix, lambda e: e) assert np.allclose(matrix, identity_mapped)
[1j, 0, 0, 1], ]) * np.sqrt(0.5)), (1, 1, np.array([ [1, 0, 0, -1j], [0, 1, -1j, 0], [0, -1j, 1, 0], [-1j, 0, 0, 1], ]) * np.sqrt(0.5)), (1, 1, np.array([ [1, 0, 0, 1j], [0, 1, -1j, 0], [0, -1j, 1, 0], [1j, 0, 0, 1], ]) * np.sqrt(0.5)), (1.5, 3, cirq.map_eigenvalues(cirq.SWAP.matrix(), lambda e: e**0.5)), (2, 2, cirq.SWAP.matrix().dot(cirq.CZ.matrix())), (3, 3, cirq.SWAP.matrix()), (3, 3, np.array([ [0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0j], ])), ] + [ (1, 2, _random_single_partial_cz_effect()) for _ in range(10) ] + [ (2, 2, _random_double_full_cz_effect()) for _ in range(10) ] + [