From b9e640104c3d6b0a67cacc6fd7de88d4623a2b11 Mon Sep 17 00:00:00 2001 From: obliviateandsurrender Date: Wed, 11 Oct 2023 18:33:44 -0400 Subject: [PATCH] revert `PhaseShift` additions --- .../ops/qubit/parametric_ops_single_qubit.py | 2 +- tests/ops/qubit/test_parametric_ops.py | 13 ++++-------- tests/transforms/test_batch_transform.py | 21 +++++-------------- tests/transforms/test_compile.py | 8 ++----- tests/transforms/test_tape_expand.py | 15 ++++++------- 5 files changed, 18 insertions(+), 41 deletions(-) diff --git a/pennylane/ops/qubit/parametric_ops_single_qubit.py b/pennylane/ops/qubit/parametric_ops_single_qubit.py index 29767e482d1..c9b19e8d216 100644 --- a/pennylane/ops/qubit/parametric_ops_single_qubit.py +++ b/pennylane/ops/qubit/parametric_ops_single_qubit.py @@ -499,7 +499,7 @@ def compute_decomposition(phi, wires): [RZ(1.234, wires=[0])] """ - return [RZ(phi, wires=wires), qml.GlobalPhase(phi / 2)] + return [RZ(phi, wires=wires)] def adjoint(self): return PhaseShift(-self.data[0], wires=self.wires) diff --git a/tests/ops/qubit/test_parametric_ops.py b/tests/ops/qubit/test_parametric_ops.py index 1d23ce3d7b6..7e874328911 100644 --- a/tests/ops/qubit/test_parametric_ops.py +++ b/tests/ops/qubit/test_parametric_ops.py @@ -264,18 +264,15 @@ def test_phase_decomposition(self, phi, tol): op = qml.PhaseShift(phi, wires=0) res = op.decomposition() - assert len(res) == 2 + assert len(res) == 1 assert res[0].name == "RZ" + assert res[0].wires == Wires([0]) assert np.allclose(res[0].data[0], phi) decomposed_matrix = res[0].matrix() global_phase = np.exp(-1j * phi / 2)[..., np.newaxis, np.newaxis] - - assert res[1].name == "GlobalPhase" - assert np.allclose(qml.matrix(res[1]), np.exp(-1j * phi / 2)) - assert np.allclose(decomposed_matrix, global_phase * op.matrix(), atol=tol, rtol=0) def test_phase_decomposition_broadcasted(self, tol): @@ -284,18 +281,16 @@ def test_phase_decomposition_broadcasted(self, tol): op = qml.PhaseShift(phi, wires=0) res = op.decomposition() - assert len(res) == 2 + assert len(res) == 1 assert res[0].name == "RZ" + assert res[0].wires == Wires([0]) assert qml.math.allclose(res[0].data[0], np.array([0.3, 2.1, 0.2])) decomposed_matrix = res[0].matrix() global_phase = np.exp(-1j * phi / 2)[..., np.newaxis, np.newaxis] - assert res[1].name == "GlobalPhase" - assert np.allclose(qml.matrix(res[1]), np.exp(-1j * phi / 2)) - assert np.allclose(decomposed_matrix, global_phase * op.matrix(), atol=tol, rtol=0) def test_Rot_decomposition(self): diff --git a/tests/transforms/test_batch_transform.py b/tests/transforms/test_batch_transform.py index 06cd7b89c67..eb2d671da38 100644 --- a/tests/transforms/test_batch_transform.py +++ b/tests/transforms/test_batch_transform.py @@ -203,11 +203,9 @@ def my_transform(self, tape): spy_expand.assert_called() input_tape = spy_transform.call_args[0][1] - assert len(input_tape.operations) == 2 + assert len(input_tape.operations) == 1 assert input_tape.operations[0].name == "RZ" - assert input_tape.operations[1].name == "GlobalPhase" assert input_tape.operations[0].parameters == [0.5] - assert input_tape.operations[1].parameters == [0.25] @pytest.mark.parametrize("perform_expansion", [True, False]) def test_expand_fn_with_kwarg(self, mocker, perform_expansion): @@ -240,13 +238,9 @@ def my_transform(self, tape, **kwargs): spy_expand.assert_called() # The expand_fn of transform_fn always is called input_tape = spy_transform.call_args[0][1] - - assert len(input_tape.operations) == 2 if perform_expansion else 1 - assert input_tape.operations[0].name == "RZ" if perform_expansion else "PhaseShift" + assert len(input_tape.operations) == 1 + assert input_tape.operations[0].name == ("RZ" if perform_expansion else "PhaseShift") assert input_tape.operations[0].parameters == [0.5] - if perform_expansion: - assert input_tape.operations[1].name == "GlobalPhase" - assert input_tape.operations[1].parameters == [0.25] @pytest.mark.parametrize("perform_expansion", [True, False]) def test_expand_qnode_with_kwarg(self, mocker, perform_expansion): @@ -282,15 +276,10 @@ def qnode(x): spy_transform.assert_called() spy_expand.assert_called() # The expand_fn of transform_fn always is called input_tape = spy_transform.call_args[0][1] - - assert len(input_tape.operations) == 2 if perform_expansion else 1 - assert input_tape.operations[0].name == "RZ" if perform_expansion else "PhaseShift" + assert len(input_tape.operations) == 1 + assert input_tape.operations[0].name == ("RZ" if perform_expansion else "PhaseShift") assert input_tape.operations[0].parameters == [0.5] - if perform_expansion: - assert input_tape.operations[1].name == "GlobalPhase" - assert input_tape.operations[1].parameters == [0.25] - def test_parametrized_transform_tape(self): """Test that a parametrized transform can be applied to a tape""" diff --git a/tests/transforms/test_compile.py b/tests/transforms/test_compile.py index 5892da8c31f..70047a36c05 100644 --- a/tests/transforms/test_compile.py +++ b/tests/transforms/test_compile.py @@ -30,7 +30,6 @@ merge_rotations, single_qubit_fusion, ) -from pennylane.transforms.optimization.optimization_utils import _fuse_global_phases def build_qfunc(wires): @@ -244,7 +243,7 @@ def test_compile_decompose_into_basis_gates(self, wires): pipeline = [partial(commute_controlled, direction="left"), cancel_inverses, merge_rotations] - basis_set = ["CNOT", "RX", "RY", "RZ", "GlobalPhase"] + basis_set = ["CNOT", "RX", "RY", "RZ"] transformed_qfunc = compile(qfunc, pipeline=pipeline, basis_set=basis_set) transformed_qnode = qml.QNode(transformed_qfunc, dev) @@ -267,7 +266,6 @@ def test_compile_decompose_into_basis_gates(self, wires): "CNOT", "RY", "CNOT", - "GlobalPhase", ] wires_expected = [ @@ -284,11 +282,9 @@ def test_compile_decompose_into_basis_gates(self, wires): Wires([wires[1], wires[2]]), Wires(wires[2]), Wires([wires[1], wires[2]]), - Wires([]), ] - tansformed_ops = _fuse_global_phases(transformed_qnode.qtape.operations) - compare_operation_lists(tansformed_ops, names_expected, wires_expected) + compare_operation_lists(transformed_qnode.qtape.operations, names_expected, wires_expected) def test_compile_template(self): """Test that functions with templates are correctly expanded and compiled.""" diff --git a/tests/transforms/test_tape_expand.py b/tests/transforms/test_tape_expand.py index b706c396230..82f5ecdfb3a 100644 --- a/tests/transforms/test_tape_expand.py +++ b/tests/transforms/test_tape_expand.py @@ -226,10 +226,9 @@ def generator(self): tape = qml.tape.QuantumScript.from_queue(q) new_tape = qml.transforms.expand_nonunitary_gen(tape) assert tape.operations[:2] == new_tape.operations[:2] - exp_op, gph_op = new_tape.operations[2:4] + exp_op = new_tape.operations[2] assert exp_op.name == "RZ" and exp_op.data == (2.1,) and exp_op.wires == qml.wires.Wires(1) - assert gph_op.name == "GlobalPhase" and gph_op.data == (2.1 * 0.5,) - assert tape.operations[3:] == new_tape.operations[4:] + assert tape.operations[3:] == new_tape.operations[3:] def test_expand_nonunitary_generator(self): """Test that a tape with single-parameter operations with @@ -245,10 +244,9 @@ def test_expand_nonunitary_generator(self): new_tape = qml.transforms.expand_nonunitary_gen(tape) assert tape.operations[:2] == new_tape.operations[:2] - exp_op, gph_op = new_tape.operations[2:4] + exp_op = new_tape.operations[2] assert exp_op.name == "RZ" and exp_op.data == (2.1,) and exp_op.wires == qml.wires.Wires(1) - assert gph_op.name == "GlobalPhase" and gph_op.data == (2.1 * 0.5,) - assert tape.operations[3:] == new_tape.operations[4:] + assert tape.operations[3:] == new_tape.operations[3:] def test_decompose_all_nonunitary_generator(self): """Test that decompositions actually only contain unitarily @@ -332,9 +330,8 @@ class NonDiffPhaseShift(qml.PhaseShift): assert new_tape.operations[0].name == "RZ" assert new_tape.operations[0].grad_method == "A" - assert new_tape.operations[1].name == "GlobalPhase" - assert new_tape.operations[2].name == "RY" - assert new_tape.operations[3].name == "CNOT" + assert new_tape.operations[1].name == "RY" + assert new_tape.operations[2].name == "CNOT" def test_nontrainable_nondiff(self, mocker): """Test that a circuit with non-differentiable