def test_fidelity_known_values(): vec1 = np.array([1, 1j, -1, -1j]) * 0.5 vec2 = np.array([1, -1, 1, -1], dtype=np.complex128) * 0.5 vec3 = np.array([1, 0, 0, 0], dtype=np.complex128) tensor1 = np.reshape(vec1, (2, 2)) mat1 = cirq.density_matrix(np.outer(vec1, vec1.conj())) mat2 = cirq.density_matrix(np.outer(vec2, vec2.conj())) mat3 = 0.3 * mat1.density_matrix() + 0.7 * mat2.density_matrix() np.testing.assert_allclose(cirq.fidelity(vec1, vec1, qid_shape=(4, )), 1) np.testing.assert_allclose(cirq.fidelity(vec2, vec2, qid_shape=(4, )), 1) np.testing.assert_allclose(cirq.fidelity(vec1, vec3, qid_shape=(4, )), 0.25) np.testing.assert_allclose(cirq.fidelity(vec1, tensor1, qid_shape=(4, )), 1) np.testing.assert_allclose(cirq.fidelity(tensor1, vec1, qid_shape=(2, 2)), 1) np.testing.assert_allclose(cirq.fidelity(mat1, mat1, qid_shape=(2, 2)), 1) np.testing.assert_allclose(cirq.fidelity(mat2, mat2, qid_shape=(2, 2)), 1) np.testing.assert_allclose(cirq.fidelity(vec1, mat1, qid_shape=(2, 2)), 1) np.testing.assert_allclose(cirq.fidelity(mat2, vec2, qid_shape=(2, 2)), 1) np.testing.assert_allclose(cirq.fidelity(vec1, vec2, qid_shape=(4, )), 0) np.testing.assert_allclose(cirq.fidelity(vec1, mat2, qid_shape=(2, 2)), 0) np.testing.assert_allclose(cirq.fidelity(mat1, vec2, qid_shape=(2, 2)), 0) np.testing.assert_allclose(cirq.fidelity(vec1, mat3, qid_shape=(2, 2)), 0.3) np.testing.assert_allclose(cirq.fidelity(tensor1, mat3, qid_shape=(2, 2)), 0.3) np.testing.assert_allclose(cirq.fidelity(mat3, tensor1, qid_shape=(2, 2)), 0.3) np.testing.assert_allclose(cirq.fidelity(mat3, vec2, qid_shape=(2, 2)), 0.7)
def test_fidelity_invariant_under_unitary_transformation(): np.testing.assert_allclose( cirq.fidelity(cirq.density_matrix(MAT1), MAT2, qid_shape=(15, )), cirq.fidelity(cirq.density_matrix(U @ MAT1 @ U.T.conj()), U @ MAT2 @ U.T.conj(), qid_shape=(15, )), )
def test_fidelity_symmetric(): np.testing.assert_allclose(cirq.fidelity(VEC1, VEC2), cirq.fidelity(VEC2, VEC1)) np.testing.assert_allclose(cirq.fidelity(VEC1, MAT1), cirq.fidelity(MAT1, VEC1)) np.testing.assert_allclose( cirq.fidelity(cirq.density_matrix(MAT1), MAT2), cirq.fidelity(cirq.density_matrix(MAT2), MAT1), )
def test_density_matrix(): density_matrix_1 = np.eye(4, dtype=np.complex64) / 4 state_vector_1 = cirq.one_hot(shape=(4,), dtype=np.complex64) state = cirq.density_matrix(density_matrix_1) assert state.data is density_matrix_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex64 with pytest.raises(ValueError, match='square'): _ = cirq.density_matrix(state_vector_1)
def test_fidelity_commuting_matrices(): d1 = np.random.uniform(size=N) d1 /= np.sum(d1) d2 = np.random.uniform(size=N) d2 /= np.sum(d2) mat1 = cirq.density_matrix(U @ np.diag(d1) @ U.T.conj()) mat2 = U @ np.diag(d2) @ U.T.conj() np.testing.assert_allclose(cirq.fidelity(mat1, mat2, qid_shape=(15, )), np.sum(np.sqrt(d1 * d2))**2)
def test_fidelity_between_zero_and_one(): assert 0 <= cirq.fidelity(VEC1, VEC2, qid_shape=(15, )) <= 1 assert 0 <= cirq.fidelity(VEC1, MAT1, qid_shape=(15, )) <= 1 assert 0 <= cirq.fidelity( cirq.density_matrix(MAT1), MAT2, qid_shape=(15, )) <= 1
def test_fidelity_between_zero_and_one(): assert 0 <= cirq.fidelity(VEC1, VEC2) <= 1 assert 0 <= cirq.fidelity(VEC1, MAT1) <= 1 assert 0 <= cirq.fidelity(cirq.density_matrix(MAT1), MAT2) <= 1