Skip to content

Commit

Permalink
Fix test that are failing on aarch64-linux due to precision (quantuml…
Browse files Browse the repository at this point in the history
…ib#4674)

More tests that were failing due to precision.  See quantumlib#4379
  • Loading branch information
dabacon authored Nov 12, 2021
1 parent 462de21 commit 06991f0
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions cirq/ops/pauli_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand All @@ -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():
Expand All @@ -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():
Expand Down

0 comments on commit 06991f0

Please sign in to comment.