From a28d601b2bcfc393336375c53e5915fd16455395 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Fri, 12 Nov 2021 19:27:19 +0000 Subject: [PATCH] Fix test that are failing on aarch64-linux due to precision (#4674) More tests that were failing due to precision. See #4379 --- cirq-core/cirq/ops/pauli_string_test.py | 42 +++++++++++++++---------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/cirq-core/cirq/ops/pauli_string_test.py b/cirq-core/cirq/ops/pauli_string_test.py index 83e2f140656..79a16402fa6 100644 --- a/cirq-core/cirq/ops/pauli_string_test.py +++ b/cirq-core/cirq/ops/pauli_string_test.py @@ -1023,15 +1023,23 @@ def test_expectation_from_state_vector_qubit_map(): z = cirq.PauliString({q0: cirq.Z}) wf = np.array([0, 1, 0, 1, 0, 0, 0, 0], dtype=complex) / np.sqrt(2) for state in [wf, wf.reshape(2, 2, 2)]: - np.testing.assert_allclose(z.expectation_from_state_vector(state, {q0: 0, q1: 1, q2: 2}), 1) - np.testing.assert_allclose(z.expectation_from_state_vector(state, {q0: 0, q1: 2, q2: 1}), 1) - np.testing.assert_allclose(z.expectation_from_state_vector(state, {q0: 1, q1: 0, q2: 2}), 0) - np.testing.assert_allclose(z.expectation_from_state_vector(state, {q0: 1, q1: 2, q2: 0}), 0) np.testing.assert_allclose( - z.expectation_from_state_vector(state, {q0: 2, q1: 0, q2: 1}), -1 + z.expectation_from_state_vector(state, {q0: 0, q1: 1, q2: 2}), 1, atol=1e-8 ) np.testing.assert_allclose( - z.expectation_from_state_vector(state, {q0: 2, q1: 1, q2: 0}), -1 + z.expectation_from_state_vector(state, {q0: 0, q1: 2, q2: 1}), 1, atol=1e-8 + ) + np.testing.assert_allclose( + z.expectation_from_state_vector(state, {q0: 1, q1: 0, q2: 2}), 0, atol=1e-8 + ) + np.testing.assert_allclose( + z.expectation_from_state_vector(state, {q0: 1, q1: 2, q2: 0}), 0, atol=1e-9 + ) + np.testing.assert_allclose( + z.expectation_from_state_vector(state, {q0: 2, q1: 0, q2: 1}), -1, atol=1e-8 + ) + np.testing.assert_allclose( + z.expectation_from_state_vector(state, {q0: 2, q1: 1, q2: 0}), -1, atol=1e-8 ) @@ -1056,13 +1064,13 @@ def test_pauli_string_expectation_from_state_vector_pure_state(): x3 = cirq.PauliString({qubits[3]: cirq.X}) for state in [wf, wf.reshape((2, 2, 2, 2))]: - np.testing.assert_allclose(z0z1.expectation_from_state_vector(state, q_map), -1) - np.testing.assert_allclose(z0z2.expectation_from_state_vector(state, q_map), 0) - np.testing.assert_allclose(z0z3.expectation_from_state_vector(state, q_map), 0) - np.testing.assert_allclose(z0x1.expectation_from_state_vector(state, q_map), 0) - np.testing.assert_allclose(z1x2.expectation_from_state_vector(state, q_map), -1) - np.testing.assert_allclose(x0z1.expectation_from_state_vector(state, q_map), 0) - np.testing.assert_allclose(x3.expectation_from_state_vector(state, q_map), -1) + np.testing.assert_allclose(z0z1.expectation_from_state_vector(state, q_map), -1, atol=1e-8) + np.testing.assert_allclose(z0z2.expectation_from_state_vector(state, q_map), 0, atol=1e-8) + np.testing.assert_allclose(z0z3.expectation_from_state_vector(state, q_map), 0, atol=1e-8) + np.testing.assert_allclose(z0x1.expectation_from_state_vector(state, q_map), 0, atol=1e-8) + np.testing.assert_allclose(z1x2.expectation_from_state_vector(state, q_map), -1, atol=1e-8) + np.testing.assert_allclose(x0z1.expectation_from_state_vector(state, q_map), 0, atol=1e-8) + np.testing.assert_allclose(x3.expectation_from_state_vector(state, q_map), -1, atol=1e-8) def test_pauli_string_expectation_from_state_vector_pure_state_with_coef(): @@ -1082,9 +1090,11 @@ def test_pauli_string_expectation_from_state_vector_pure_state_with_coef(): z1x2 = -cirq.Z(qs[1]) * cirq.X(qs[2]) for state in [wf, wf.reshape((2, 2, 2, 2))]: - np.testing.assert_allclose(z0z1.expectation_from_state_vector(state, q_map), -0.123) - np.testing.assert_allclose(z0z2.expectation_from_state_vector(state, q_map), 0) - np.testing.assert_allclose(z1x2.expectation_from_state_vector(state, q_map), 1) + np.testing.assert_allclose( + z0z1.expectation_from_state_vector(state, q_map), -0.123, atol=1e-8 + ) + np.testing.assert_allclose(z0z2.expectation_from_state_vector(state, q_map), 0, atol=1e-8) + np.testing.assert_allclose(z1x2.expectation_from_state_vector(state, q_map), 1, atol=1e-8) def test_expectation_from_density_matrix_invalid_input():