Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pw-reorder' into feature/dynamic…
Browse files Browse the repository at this point in the history
…_samples
  • Loading branch information
vincentmr committed Apr 23, 2024
2 parents a5f2bf5 + 296ea6d commit 13a309f
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ def test_tensor_non_tensor_return(self, use_csingle, wires_map):

named_obs = NamedObsC64 if use_csingle else NamedObsC128
tensor_prod_obs = TensorProdObsC64 if use_csingle else TensorProdObsC128
first_s = tensor_prod_obs([named_obs("PauliX", [1]), named_obs("PauliZ", [0])])

s_expected = [
first_s,
tensor_prod_obs([named_obs("PauliZ", [0]), named_obs("PauliX", [1])]),
named_obs("Hadamard", [1]),
]

Expand All @@ -158,7 +157,6 @@ def test_tensor_non_tensor_return(self, use_csingle, wires_map):
)
assert s == s_expected

@pytest.mark.xfail(reason="Prod with overlapping wires not supported")
@pytest.mark.parametrize("use_csingle", [True, False])
@pytest.mark.parametrize("wires_map", [wires_dict, None])
def test_prod_return_with_overlapping_wires(self, use_csingle, wires_map):
Expand Down Expand Up @@ -283,7 +281,7 @@ def test_hamiltonian_return(self, use_csingle, wires_map):
named_obs("PauliY", [2]),
]
),
tensor_prod_obs([named_obs("PauliY", [2]), named_obs("PauliX", [0])]),
tensor_prod_obs([named_obs("PauliX", [0]), named_obs("PauliY", [2])]),
hermitian_obs(np.ones(64, dtype=c_dtype), [0, 1, 2]),
],
)
Expand Down Expand Up @@ -317,22 +315,23 @@ def test_hamiltonian_tensor_return(self, use_csingle, wires_map):
tape, wires_map
)

first_term = tensor_prod_obs(
[
hermitian_obs(np.eye(4, dtype=c_dtype).ravel(), [0, 1]),
named_obs("PauliY", [2]),
named_obs("PauliZ", [3]),
]
)
# Expression (ham @ obs) is converted internally by Pennylane
# where obs is appended to each term of the ham

s_expected = hamiltonian_obs(
np.array([0.3, 0.5, 0.4], dtype=r_dtype),
[
first_term,
tensor_prod_obs(
[
hermitian_obs(np.eye(4, dtype=c_dtype).ravel(), [0, 1]),
named_obs("PauliY", [2]),
named_obs("PauliZ", [3]),
]
),
tensor_prod_obs(
[
named_obs("PauliX", [0]),
named_obs("PauliY", [2]),
named_obs("PauliZ", [3]),
]
),
Expand Down Expand Up @@ -388,7 +387,7 @@ def test_hamiltonian_mix_return(self, use_csingle, wires_map):
named_obs("PauliY", [2]),
]
),
tensor_prod_obs([named_obs("PauliY", [2]), named_obs("PauliX", [0])]),
tensor_prod_obs([named_obs("PauliX", [0]), named_obs("PauliY", [2])]),
hermitian_obs(np.ones(64, dtype=c_dtype), [0, 1, 2]),
],
)
Expand All @@ -401,7 +400,7 @@ def test_hamiltonian_mix_return(self, use_csingle, wires_map):
hermitian_obs(np.eye(4, dtype=c_dtype).ravel(), [1, 2]),
]
),
tensor_prod_obs([named_obs("PauliX", [2]), named_obs("PauliY", [0])]),
tensor_prod_obs([named_obs("PauliY", [0]), named_obs("PauliX", [2])]),
],
)

Expand Down Expand Up @@ -441,7 +440,7 @@ def test_pauli_rep_single_term(self, use_csingle, wires_map):
s, _ = QuantumScriptSerializer(device_name, use_csingle).serialize_observables(
tape, wires_map
)
s_expected = tensor_prod_obs([named_obs("PauliZ", [1]), named_obs("PauliX", [0])])
s_expected = tensor_prod_obs([named_obs("PauliX", [0]), named_obs("PauliZ", [1])])
assert s[0] == s_expected

@pytest.mark.parametrize("use_csingle", [True, False])
Expand Down Expand Up @@ -482,11 +481,7 @@ def test_prod(self, use_csingle, wires_map):
assert isinstance(res[0], tensor_obs)

s_expected = tensor_obs(
[
named_obs("PauliZ", [0]),
named_obs("PauliX", [1]),
named_obs("Hadamard", [2]),
]
[named_obs("PauliZ", [0]), named_obs("PauliX", [1]), named_obs("Hadamard", [2])]
)
assert res[0] == s_expected

Expand Down Expand Up @@ -522,7 +517,7 @@ def test_sum(self, use_csingle, wires_map):
coeffs,
[
tensor_obs(
[named_obs("PauliZ", [1]), named_obs("PauliX", [0]), named_obs("PauliX", [2])]
[named_obs("PauliX", [0]), named_obs("PauliZ", [1]), named_obs("PauliX", [2])]
),
tensor_obs(
[named_obs("PauliZ", [0]), named_obs("PauliY", [1]), named_obs("Hadamard", [2])]
Expand Down

0 comments on commit 13a309f

Please sign in to comment.