From ff3e6eabf3f3929d4bd023f35e4adb39497de6b7 Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Mon, 4 Mar 2024 10:12:19 -0500 Subject: [PATCH 1/8] Updated condition for swapping matmul order --- pennylane/pauli/pauli_arithmetic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane/pauli/pauli_arithmetic.py b/pennylane/pauli/pauli_arithmetic.py index ebb8ad40e6a..4b0c20b3777 100644 --- a/pennylane/pauli/pauli_arithmetic.py +++ b/pennylane/pauli/pauli_arithmetic.py @@ -227,7 +227,7 @@ def __hash__(self): def _matmul(self, other): """Private matrix multiplication that returns (pauli_word, coeff) tuple for more lightweight processing""" base, iterator, swapped = ( - (self, other, False) if len(self) > len(other) else (other, self, True) + (self, other, False) if len(self) >= len(other) else (other, self, True) ) result = copy(dict(base)) coeff = 1 From 08b681e24a50d322f7cdc9d0e43b5e2863113896 Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Mon, 4 Mar 2024 12:37:08 -0500 Subject: [PATCH 2/8] Added tests --- tests/ops/op_math/test_prod.py | 15 ++++++++++++--- tests/ops/op_math/test_sum.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/tests/ops/op_math/test_prod.py b/tests/ops/op_math/test_prod.py index f7609c09ec0..0910a77ec99 100644 --- a/tests/ops/op_math/test_prod.py +++ b/tests/ops/op_math/test_prod.py @@ -255,6 +255,15 @@ def test_terms_pauli_rep(self, op, coeffs_true, ops_true): assert coeffs == coeffs_true assert ops1 == ops_true + def test_terms_pauli_rep_wire_order(self): + """Test that the wire order of the terms is the same as the wire order of the original + operands when the Prod has a valid pauli_rep""" + H = qml.prod(X(0), X(1), X(2)) + _, H_ops = H.terms() + + assert len(H_ops) == 1 + assert H_ops[0].wires == H.wires + def test_batch_size(self): """Test that batch size returns the batch size of a base operation if it is batched.""" x = qml.numpy.array([1.0, 2.0, 3.0]) @@ -1003,8 +1012,8 @@ def test_pauli_rep_order(self): """ op = qml.prod(qml.PauliX(0), qml.PauliY(1), qml.PauliZ(2)) pw = list(op.pauli_rep.keys())[0] - assert list(pw.keys()) == [1, 0, 2] - assert list(pw.values()) == ["Y", "X", "Z"] + assert list(pw.keys()) == [0, 1, 2] + assert list(pw.values()) == ["X", "Y", "Z"] @pytest.mark.parametrize("op, rep", op_pauli_reps) def test_pauli_rep(self, op, rep): @@ -1087,7 +1096,7 @@ def test_simplify_method_product_of_sums(self): """Test the simplify method with a product of sums.""" prod_op = Prod(qml.PauliX(0) + qml.RX(1, 0), qml.PauliX(1) + qml.RX(1, 1), qml.Identity(3)) final_op = qml.sum( - Prod(qml.PauliX(1), qml.PauliX(0)), + Prod(qml.PauliX(0), qml.PauliX(1)), qml.PauliX(0) @ qml.RX(1, 1), qml.PauliX(1) @ qml.RX(1, 0), qml.RX(1, 0) @ qml.RX(1, 1), diff --git a/tests/ops/op_math/test_sum.py b/tests/ops/op_math/test_sum.py index cc3c1fd99c4..36888fde6bb 100644 --- a/tests/ops/op_math/test_sum.py +++ b/tests/ops/op_math/test_sum.py @@ -213,6 +213,24 @@ def test_terms_pauli_rep(self, op, coeffs_true, ops_true): assert coeffs == coeffs_true assert ops1 == ops_true + def test_terms_pauli_rep_wire_order(self): + """Test that the wire order of the terms is the same as the wire order of the original + operands when the Sum has a valid pauli_rep""" + w0, w1, w2, w3 = [0, 1, 2, 3] + coeffs = [0.5, -0.5] + + obs = [ + qml.X(w0) @ qml.Y(w1) @ qml.X(w2) @ qml.Z(w3), + qml.X(w0) @ qml.X(w1) @ qml.Y(w2) @ qml.Z(w3), + ] + + H = qml.dot(coeffs, obs) + _, H_ops = H.terms() + + assert all(o1.wires == o2.wires for o1, o2 in zip(obs, H_ops)) + assert H_ops[0] == qml.prod(qml.X(w0), qml.Y(w1), qml.X(w2), qml.Z(w3)) + assert H_ops[1] == qml.prod(qml.X(w0), qml.X(w1), qml.Y(w2), qml.Z(w3)) + coeffs_ = [1.0, 1.0, 1.0, 3.0, 4.0, 4.0, 5.0] h6 = qml.sum( qml.s_prod(2.0, qml.prod(qml.Hadamard(0), qml.PauliZ(10))), From c063fbb46b3c8c7d4630c8d928f0da5c1b72da0b Mon Sep 17 00:00:00 2001 From: qottmann Date: Fri, 5 Apr 2024 15:36:25 +0200 Subject: [PATCH 3/8] [ci skip] From 58efe4ce834a1410560537dbe4ab5bafeef8c7cc Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Tue, 9 Apr 2024 12:23:58 -0400 Subject: [PATCH 4/8] [skip ci] Skip CI From 4786179fbe36228a144cecffd12bafe7af792b87 Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Tue, 9 Apr 2024 14:54:51 -0400 Subject: [PATCH 5/8] [skip ci] Skip CI From 64af1ca7d2c9424426888cb774376204e1fbf04e Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Thu, 18 Apr 2024 10:29:56 -0400 Subject: [PATCH 6/8] Fixed failing tests --- .../pauli/test_measurement_transformations.py | 2 +- tests/pauli/test_pauli_utils.py | 5 +---- .../test_approx_time_evolution.py | 4 ++-- tests/test_qaoa.py | 18 +++++++++--------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/tests/pauli/test_measurement_transformations.py b/tests/pauli/test_measurement_transformations.py index cae360ebf0f..7526a5fc488 100644 --- a/tests/pauli/test_measurement_transformations.py +++ b/tests/pauli/test_measurement_transformations.py @@ -120,7 +120,7 @@ def test_diagonalize_pauli_word_catch_non_pauli_word(self, non_pauli_word): ( [PauliX(0) @ PauliY(1), PauliX(0) @ PauliZ(2)], ( - [RX(np.pi / 2, wires=[1]), RY(-np.pi / 2, wires=[0])], + [RY(-np.pi / 2, wires=[0]), RX(np.pi / 2, wires=[1])], [PauliZ(wires=[0]) @ PauliZ(wires=[1]), PauliZ(wires=[0]) @ PauliZ(wires=[2])], ), ), diff --git a/tests/pauli/test_pauli_utils.py b/tests/pauli/test_pauli_utils.py index 2f21637e06e..db66e0bff78 100644 --- a/tests/pauli/test_pauli_utils.py +++ b/tests/pauli/test_pauli_utils.py @@ -926,10 +926,7 @@ def test_diagonalize_pauli_word_catch_non_pauli_word(self, non_pauli_word): ( [PauliX(0) @ PauliY(1), PauliX(0) @ PauliZ(2)], ( - [ - RX(np.pi / 2, wires=[1]), - RY(-np.pi / 2, wires=[0]), - ], + [RY(-np.pi / 2, wires=[0]), RX(np.pi / 2, wires=[1])], [PauliZ(wires=[0]) @ PauliZ(wires=[1]), PauliZ(wires=[0]) @ PauliZ(wires=[2])], ), ), diff --git a/tests/templates/test_subroutines/test_approx_time_evolution.py b/tests/templates/test_subroutines/test_approx_time_evolution.py index 044f261ee1b..5924d3e7504 100644 --- a/tests/templates/test_subroutines/test_approx_time_evolution.py +++ b/tests/templates/test_subroutines/test_approx_time_evolution.py @@ -66,7 +66,7 @@ class TestDecomposition: ), ( 2, - qml.Hamiltonian([2, 0.5], [qml.PauliX("a"), qml.PauliZ("b") @ qml.PauliX("a")]), + qml.Hamiltonian([2, 0.5], [qml.PauliX("a"), qml.PauliX("a") @ qml.PauliZ("b")]), 2, [ qml.PauliRot(4.0, "X", wires=["a"]), @@ -87,7 +87,7 @@ class TestDecomposition: [2, 0.5, 0.5], [ qml.PauliX("a"), - qml.PauliZ(-15) @ qml.PauliX("a"), + qml.PauliX("a") @ qml.PauliZ(-15), qml.Identity(0) @ qml.PauliY(-15), ], ), diff --git a/tests/test_qaoa.py b/tests/test_qaoa.py index 591b5a5ce3f..32428406f4c 100644 --- a/tests/test_qaoa.py +++ b/tests/test_qaoa.py @@ -1166,12 +1166,12 @@ def make_mixer_layer_test_cases(): [ qaoa.xy_mixer(Graph([(0, 1), (1, 2), (2, 0)])), [ - qml.PauliRot(1.0, "XX", wires=[1, 0]), - qml.PauliRot(1.0, "YY", wires=[1, 0]), - qml.PauliRot(1.0, "XX", wires=[2, 0]), - qml.PauliRot(1.0, "YY", wires=[2, 0]), - qml.PauliRot(1.0, "XX", wires=[2, 1]), - qml.PauliRot(1.0, "YY", wires=[2, 1]), + qml.PauliRot(1.0, "XX", wires=[0, 1]), + qml.PauliRot(1.0, "YY", wires=[0, 1]), + qml.PauliRot(1.0, "XX", wires=[0, 2]), + qml.PauliRot(1.0, "YY", wires=[0, 2]), + qml.PauliRot(1.0, "XX", wires=[1, 2]), + qml.PauliRot(1.0, "YY", wires=[1, 2]), ], ], ] @@ -1186,9 +1186,9 @@ def make_cost_layer_test_cases(): [ qaoa.maxcut(Graph([(0, 1), (1, 2), (2, 0)]))[0], [ - qml.PauliRot(1.0, "ZZ", wires=[1, 0]), - qml.PauliRot(1.0, "ZZ", wires=[2, 0]), - qml.PauliRot(1.0, "ZZ", wires=[2, 1]), + qml.PauliRot(1.0, "ZZ", wires=[0, 1]), + qml.PauliRot(1.0, "ZZ", wires=[0, 2]), + qml.PauliRot(1.0, "ZZ", wires=[1, 2]), ], ], ] From 6f16caaf61e48312b6c2c8793bba362c7b2ae40d Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Thu, 18 Apr 2024 11:05:27 -0400 Subject: [PATCH 7/8] Trigger CI From a6189205817b22ed3b6fea199afddfb02b00d2f7 Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Thu, 18 Apr 2024 14:42:56 -0400 Subject: [PATCH 8/8] Updated qasm test --- tests/circuit_graph/test_qasm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/circuit_graph/test_qasm.py b/tests/circuit_graph/test_qasm.py index c3f2beaf5f7..9521b1c8bb9 100644 --- a/tests/circuit_graph/test_qasm.py +++ b/tests/circuit_graph/test_qasm.py @@ -117,9 +117,9 @@ def test_to_ApproxTimeEvolution(self): include "qelib1.inc"; qreg q[2]; creg c[2]; - cx q[0],q[1]; - rz(2.0) q[1]; - cx q[0],q[1]; + cx q[1],q[0]; + rz(2.0) q[0]; + cx q[1],q[0]; measure q[0] -> c[0]; measure q[1] -> c[1]; """