Skip to content

Commit

Permalink
revert PhaseShift additions
Browse files Browse the repository at this point in the history
  • Loading branch information
obliviateandsurrender committed Oct 11, 2023
1 parent 5334132 commit b9e6401
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pennylane/ops/qubit/parametric_ops_single_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 4 additions & 9 deletions tests/ops/qubit/test_parametric_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
21 changes: 5 additions & 16 deletions tests/transforms/test_batch_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"""
Expand Down
8 changes: 2 additions & 6 deletions tests/transforms/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
merge_rotations,
single_qubit_fusion,
)
from pennylane.transforms.optimization.optimization_utils import _fuse_global_phases


def build_qfunc(wires):
Expand Down Expand Up @@ -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)
Expand All @@ -267,7 +266,6 @@ def test_compile_decompose_into_basis_gates(self, wires):
"CNOT",
"RY",
"CNOT",
"GlobalPhase",
]

wires_expected = [
Expand All @@ -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."""
Expand Down
15 changes: 6 additions & 9 deletions tests/transforms/test_tape_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b9e6401

Please sign in to comment.