Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
timmysilv authored and mlxd committed Oct 20, 2023
1 parent 95880a3 commit 30ca228
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 47 deletions.
5 changes: 0 additions & 5 deletions pennylane/ops/op_math/controlled.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,6 @@ def hash(self):
def has_matrix(self):
return self.base.has_matrix

# pylint: disable=missing-function-docstring
@property
def basis(self):
return self.base.basis

# pylint: disable=protected-access
def _check_batching(self, params):
self.base._check_batching(params)
Expand Down
42 changes: 3 additions & 39 deletions pennylane/templates/subroutines/controlled_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def control(self):

@property
def wires(self):
return self.base.wires + self.control
return self.control + self.base.wires

@property
def has_matrix(self):
Expand All @@ -124,45 +124,9 @@ def map_wires(self, wire_map: dict):
)
return new_op

def decomposition(self): # pylint:disable=arguments-differ
r"""Representation of the operator as a product of other operators.
.. math:: O = O_1 O_2 \dots O_n.
.. seealso:: :meth:`~.CtrlSequence.decomposition`.
Args:
base (Operator): the operator that acts as the base for the sequence
control_wires (Any or Iterable[Any]): the control wires for the sequence
Returns:
list[.Operator]: decomposition of the operator
**Example**
.. code-block:: python
dev = qml.device("default.qubit")
op = qml.ControlledSequence(qml.RX(0.25, wires = 3), control = [0, 1, 2])
@qml.qnode(dev)
def circuit():
op.decomposition()
return qml.state()
>>> print(qml.draw(circuit, wire_order=[0,1,2,3])())
0: ─╭●─────────────────────────────────────┤ State
1: ─│────────────╭●────────────────────────┤ State
2: ─│────────────│────────────╭●───────────┤ State
3: ─╰(RX(0.25))⁴─╰(RX(0.25))²─╰(RX(0.25))¹─┤ State
"""

return self.compute_decomposition(self.base, self.control)

# pylint:disable=arguments-differ
@staticmethod
def compute_decomposition(base, control_wires): # pylint:disable=arguments-differ
def compute_decomposition(*_, base=None, control_wires=None, **__):
r"""Representation of the operator as a product of other operators.
.. math:: O = O_1 O_2 \dots O_n.
Expand Down
8 changes: 5 additions & 3 deletions tests/templates/test_subroutines/test_controlled_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_control(self):
def test_wires(self):
"""Test that the wires property returns all wires, including both base and control"""
op = qml.ControlledSequence(qml.CNOT([17, "3"]), control=["b", 2])
assert op.wires == Wires([17, "3", "b", 2])
assert op.wires == Wires(["b", 2, 17, "3"])

def test_has_matrix(self):
"""Test that a ControlledSequence returns False for has_matrix, even if the base returns True"""
Expand All @@ -129,7 +129,7 @@ def test_map_wires(self):
assert type(new_op.base) == type(op.base)
assert new_op.data == op.data

assert new_op.wires == Wires(["a", "b", "c", "d"])
assert new_op.wires == Wires(["c", "d", "a", "b"])
assert new_op.base.wires == Wires(["a", "b"])
assert new_op.control == Wires(["c", "d"])

Expand All @@ -138,7 +138,9 @@ def test_compute_decomposition(self):
base = qml.RZ(4.3, 1)
control_wires = [0, 2, 3]

decomp = qml.ControlledSequence.compute_decomposition(base, control_wires=control_wires)
decomp = qml.ControlledSequence.compute_decomposition(
base=base, control_wires=control_wires
)

assert len(decomp) == len(control_wires)
for i, op in enumerate(decomp):
Expand Down

0 comments on commit 30ca228

Please sign in to comment.