def _random_single_partial_cz_effect(): return cirq.dot( cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)))
def _random_double_full_cz_effect(): return cirq.dot( cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), cirq.CZ.matrix(), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), cirq.CZ.matrix(), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)))
def test_single_qubit_matrix_to_native_gates_fuzz_half_turns_always_one_gate( pre_turns, post_turns): intended_effect = cirq.dot( cirq.RotZGate(half_turns=2 * pre_turns).matrix(), cirq.X.matrix(), cirq.RotZGate(half_turns=2 * post_turns).matrix()) gates = decompositions.single_qubit_matrix_to_native_gates( intended_effect, tolerance=0.0001) assert len(gates) == 1 assert_gates_implement_unitary(gates, intended_effect)
def test_single_qubit_matrix_to_native_gates_fuzz_half_turns_always_one_gate( pre_turns, post_turns): intended_effect = cirq.dot(cirq.unitary(cirq.Z**(2 * pre_turns)), cirq.unitary(cirq.X), cirq.unitary(cirq.Z**(2 * post_turns))) gates = cirq.single_qubit_matrix_to_phased_x_z(intended_effect, atol=0.0001) assert len(gates) == 1 assert_gates_implement_unitary(gates, intended_effect, atol=1e-8)
def _random_single_MS_effect(): t = random.random() s = np.sin(t) c = np.cos(t) return cirq.dot( cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), np.array([[c, 0, 0, -1j * s], [0, c, -1j * s, 0], [0, -1j * s, c, 0], [-1j * s, 0, 0, c]]), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)))
def _random_double_full_cz_effect(): return cirq.dot( cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), cirq.unitary(cirq.CZ), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), cirq.unitary(cirq.CZ), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), )
def _random_double_partial_cz_effect(): return cirq.dot( cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), )
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 test_single_qubit_matrix_to_gates_fuzz_half_turns_merge_z_gates( pre_turns, post_turns): intended_effect = cirq.dot( cirq.RotZGate(half_turns=2 * pre_turns).matrix(), cirq.X.matrix(), cirq.RotZGate(half_turns=2 * post_turns).matrix()) gates = cirq.single_qubit_matrix_to_gates(intended_effect, tolerance=0.0001) assert len(gates) <= 2 assert_gates_implement_unitary(gates, intended_effect)
def test_single_qubit_matrix_to_gates_fuzz_half_turns_merge_z_gates( pre_turns, post_turns): intended_effect = cirq.dot( cirq.unitary(cirq.Z**(2 * pre_turns)), cirq.unitary(cirq.X), cirq.unitary(cirq.Z**(2 * post_turns))) gates = cirq.single_qubit_matrix_to_gates( intended_effect, tolerance=1e-7) assert len(gates) <= 2 assert_gates_implement_unitary(gates, intended_effect, atol=1e-6)
def test_single_qubit_matrix_to_gates_fuzz_half_turns_merge_z_gates( pre_turns, post_turns): intended_effect = cirq.dot( cirq.RotZGate(half_turns=2 * pre_turns).matrix(), cirq.X.matrix(), cirq.RotZGate(half_turns=2 * post_turns).matrix()) gates = cirq.single_qubit_matrix_to_gates( intended_effect, tolerance=0.0001) assert len(gates) <= 2 assert_gates_implement_unitary(gates, intended_effect)
def test_dot(): assert cirq.dot(2) == 2 assert cirq.dot(2.5, 2.5) == 6.25 a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) assert cirq.dot(a) is not a np.testing.assert_allclose(cirq.dot(a), a, atol=1e-8) np.testing.assert_allclose(cirq.dot(a, b), np.dot(a, b), atol=1e-8) np.testing.assert_allclose(cirq.dot(a, b, a), np.dot(np.dot(a, b), a), atol=1e-8) # Invalid use with pytest.raises(ValueError): cirq.dot()
def test_single_qubit_matrix_to_phxz_fuzz_half_turns_always_one_gate( pre_turns, post_turns): atol = 1e-6 aggr_atol = atol * 10.0 intended_effect = cirq.dot(cirq.unitary(cirq.Z**(2 * pre_turns)), cirq.unitary(cirq.X), cirq.unitary(cirq.Z**(2 * post_turns))) gate = cirq.single_qubit_matrix_to_phxz(intended_effect, atol=atol) assert gate.z_exponent == 0 assert_gates_implement_unitary([gate], intended_effect, atol=aggr_atol)
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_dot(): assert cirq.dot(2) == 2 assert cirq.dot(2.5, 2.5) == 6.25 a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) assert cirq.dot(a) is not a np.testing.assert_allclose(cirq.dot(a), a, atol=1e-8) np.testing.assert_allclose(cirq.dot(a, b), np.dot(a, b), atol=1e-8) np.testing.assert_allclose(cirq.dot(a, b, a), np.dot(np.dot(a, b), a), atol=1e-8)
def _random_double_MS_effect(): t1 = random.random() s1 = np.sin(t1) c1 = np.cos(t1) t2 = random.random() s2 = np.sin(t2) c2 = np.cos(t2) return cirq.dot( cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), np.array([[c1, 0, 0, -1j * s1], [0, c1, -1j * s1, 0], [0, -1j * s1, c1, 0], [-1j * s1, 0, 0, c1]]), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)), np.array([[c2, 0, 0, -1j * s2], [0, c2, -1j * s2, 0], [0, -1j * s2, c2, 0], [-1j * s2, 0, 0, c2]]), cirq.kron(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)))
def assert_gates_implement_unitary(gates: Sequence[cirq.SingleQubitGate], intended_effect: np.ndarray, atol: float): actual_effect = cirq.dot(*[cirq.unitary(g) for g in reversed(gates)]) cirq.testing.assert_allclose_up_to_global_phase(actual_effect, intended_effect, atol=atol)
def assert_gates_implement_unitary(gates: List[cirq.SingleQubitGate], intended_effect: np.ndarray): actual_effect = cirq.dot(*[cirq.unitary(g) for g in reversed(gates)]) assert cirq.allclose_up_to_global_phase(actual_effect, intended_effect)