def _random_double_full_cz_effect(): return linalg.dot( linalg.kron(testing.random_unitary(2), testing.random_unitary(2)), ops.CZ.matrix(), linalg.kron(testing.random_unitary(2), testing.random_unitary(2)), ops.CZ.matrix(), linalg.kron(testing.random_unitary(2), testing.random_unitary(2)))
def _random_double_partial_cz_effect(): return linalg.dot( linalg.kron(testing.random_unitary(2), testing.random_unitary(2)), np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]), linalg.kron(testing.random_unitary(2), testing.random_unitary(2)), np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]), linalg.kron(testing.random_unitary(2), testing.random_unitary(2)))
def test_random_unitary(): u1 = random_unitary(2) u2 = random_unitary(2) assert is_unitary(u1) assert is_unitary(u2) assert not np.allclose(u1, u2)
[[1, 0], [0, -1j]]), tolerance=0.01) assert actual == [ops.Z**-0.5] def test_known_h(): actual = decompositions.single_qubit_matrix_to_native_gates( np.array([[1, 1], [1, -1]]) * np.sqrt(0.5), tolerance=0.001) assert actual == [ops.Y**-0.5, ops.Z] @pytest.mark.parametrize('intended_effect', [ np.array([[0, 1j], [1, 0]]), ] + [testing.random_unitary(2) for _ in range(10)]) def test_single_qubit_matrix_to_native_gates_cases(intended_effect): gates = decompositions.single_qubit_matrix_to_native_gates( intended_effect, tolerance=0.0001) assert len(gates) <= 2 assert_gates_implement_unitary(gates, intended_effect) @pytest.mark.parametrize('pre_turns,post_turns', [(random.random(), random.random()) for _ in range(10)]) def test_single_qubit_matrix_to_native_gates_fuzz_half_turns_always_one_gate( pre_turns, post_turns): intended_effect = linalg.dot( ops.RotZGate(half_turns=2 * pre_turns).matrix(), ops.X.matrix(), ops.RotZGate(half_turns=2 * post_turns).matrix())
]) def test_bidiagonalize_real_fails(a, b): a = np.array(a) b = np.array(b) with pytest.raises(ValueError): diagonalize.bidiagonalize_real_matrix_pair_with_symmetric_products( a, b) @pytest.mark.parametrize('mat', [ np.diag([1]), np.diag([1j]), np.diag([-1]), SWAP, CNOT, Y, H, combinators.kron(H, H), combinators.kron(Y, Y), QFT ] + [testing.random_unitary(2) for _ in range(10)] + [testing.random_unitary(4) for _ in range(10)] + [testing.random_unitary(k) for k in range(1, 10)]) def test_bidiagonalize_unitary_with_special_orthogonals(mat): p, d, q = diagonalize.bidiagonalize_unitary_with_special_orthogonals(mat) assert predicates.is_special_orthogonal(p) assert predicates.is_special_orthogonal(q) assert np.allclose(p.dot(mat).dot(q), np.diag(d)) assert_bidiagonalized_by(mat, p, q) @pytest.mark.parametrize('mat', [ np.diag([0]), np.diag([0.5]), np.diag([1, 0]), np.diag([0.5, 2]),
assert np.allclose(desired, exp_mapped) @pytest.mark.parametrize('f1,f2', [ (H, X), (H * 1j, X), (H, SQRT_X), (H, SQRT_SQRT_X), (H, H), (SQRT_SQRT_X, H), (X, np.eye(2)), (1j * X, np.eye(2)), (X, 1j * np.eye(2)), (-X, 1j * np.eye(2)), (X, X), ] + [(testing.random_unitary(2), testing.random_unitary(2)) for _ in range(10)]) def test_kron_factor(f1, f2): p = linalg.kron(f1, f2) g, g1, g2 = linalg.kron_factor_4x4_to_2x2s(p) assert abs(np.linalg.det(g1) - 1) < 0.00001 assert abs(np.linalg.det(g2) - 1) < 0.00001 assert np.allclose(g * linalg.kron(g1, g2), p) @pytest.mark.parametrize( 'f1,f2', [(testing.random_special_unitary(2), testing.random_special_unitary(2)) for _ in range(10)]) def test_kron_factor_special_unitaries(f1, f2): p = linalg.kron(f1, f2)