Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change wire repr #5958

Merged
merged 12 commits into from
Jul 12, 2024
Merged
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* `QuantumScript.hash` is now cached, leading to performance improvements.
[(#5919)](https://github.com/PennyLaneAI/pennylane/pull/5919)

* The representation for `Wires` has now changed to be more copy-paste friendly.
[(#5958)](https://github.com/PennyLaneAI/pennylane/pull/5958)

<h3>Breaking changes 💔</h3>

<h3>Deprecations 👋</h3>
Expand All @@ -34,5 +37,6 @@ This release contains contributions from (in alphabetical order):

Yushao Chen,
Christina Lee,
Austin Huang,
William Maxwell,
Erik Schultheis.
18 changes: 9 additions & 9 deletions pennylane/wires.py
austingmhuang marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def __contains__(self, item):

def __repr__(self):
"""Method defining the string representation of this class."""
return f"<Wires = {list(self._labels)}>"
return f"Wires({list(self._labels)})"

def __eq__(self, other):
"""Method to support the '==' operator.
Expand Down Expand Up @@ -286,7 +286,7 @@ def map(self, wire_map):
>>> wires = Wires(['a', 'b', 'c'])
>>> wire_map = {'a': 4, 'b':2, 'c': 3}
>>> wires.map(wire_map)
<Wires = [4, 2, 3]>
Wires([4, 2, 3])
"""
# Make sure wire_map has `Wires` keys and values so that the `in` operator always works

Expand Down Expand Up @@ -322,17 +322,17 @@ def subset(self, indices, periodic_boundary=False):

>>> wires = Wires([4, 0, 1, 5, 6])
>>> wires.subset([2, 3, 0])
<Wires = [1, 5, 4]>
Wires([1, 5, 4])
>>> wires.subset(1)
<Wires = [0]>
Wires([0])

If ``periodic_boundary`` is True, the modulo of the number of wires of an index is used instead of an index,
so that ``wires.subset(i) == wires.subset(i % n_wires)`` where ``n_wires`` is the number of wires of this
object.

>>> wires = Wires([4, 0, 1, 5, 6])
>>> wires.subset([5, 1, 7], periodic_boundary=True)
<Wires = [4, 0, 1]>
Wires([4, 0, 1])

"""

Expand Down Expand Up @@ -389,9 +389,9 @@ def shared_wires(list_of_wires):
>>> wires2 = Wires([3, 0, 4])
>>> wires3 = Wires([4, 0])
>>> Wires.shared_wires([wires1, wires2, wires3])
<Wires = [4, 0]>
Wires([4, 0])
>>> Wires.shared_wires([wires2, wires1, wires3])
<Wires = [0, 4]>
Wires([0, 4])
"""

for wires in list_of_wires:
Expand Down Expand Up @@ -431,7 +431,7 @@ def all_wires(list_of_wires, sort=False):
>>> wires3 = Wires([5, 3])
>>> list_of_wires = [wires1, wires2, wires3]
>>> Wires.all_wires(list_of_wires)
<Wires = [4, 0, 1, 3, 5]>
Wires([4, 0, 1, 3, 5])
"""
converted_wires = (
wires if isinstance(wires, Wires) else Wires(wires) for wires in list_of_wires
Expand Down Expand Up @@ -463,7 +463,7 @@ def unique_wires(list_of_wires):
>>> wires2 = Wires([0, 2, 3])
>>> wires3 = Wires([5, 3])
>>> Wires.unique_wires([wires1, wires2, wires3])
<Wires = [4, 1, 2, 5]>
Wires([4, 1, 2, 5])
"""

for wires in list_of_wires:
Expand Down
2 changes: 1 addition & 1 deletion tests/measurements/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_state(ket):

def test_wire_ordering_error(self):
"""Test that a wire order error is raised when unknown wires are given."""
with pytest.raises(WireError, match=r"Unexpected unique wires <Wires = \[0, 1, 2\]> found"):
with pytest.raises(WireError, match=r"Unexpected unique wires Wires\(\[0, 1, 2\]\) found"):
StateMP(wires=[0, 1]).process_state([1, 0], wire_order=Wires(2))

@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions tests/pytrees/test_pytrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def test_structure_repr_str():
"""Test the repr of the structure class."""
op = qml.RX(0.1, wires=0)
_, structure = qml.pytrees.flatten(op)
expected = "PyTreeStructure(RX, (<Wires = [0]>, ()), [PyTreeStructure()])"
expected = "PyTreeStructure(RX, (Wires([0]), ()), [PyTreeStructure()])"
assert repr(structure) == expected
expected_str = "PyTree(RX, (<Wires = [0]>, ()), [Leaf])"
expected_str = "PyTree(RX, (Wires([0]), ()), [Leaf])"
assert str(structure) == expected_str


Expand Down
3 changes: 1 addition & 2 deletions tests/templates/test_subroutines/test_prepselprep.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def test_repr():

op = qml.PrepSelPrep(lcu, control)
assert (
repr(op)
== "PrepSelPrep(coeffs=(0.25, 0.75), ops=(Z(2), X(1) @ X(2)), control=<Wires = [0]>)"
repr(op) == "PrepSelPrep(coeffs=(0.25, 0.75), ops=(Z(2), X(1) @ X(2)), control=Wires([0]))"
)


Expand Down
2 changes: 1 addition & 1 deletion tests/templates/test_subroutines/test_qrom.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def test_repr():
["1", "0", "0", "1"], control_wires=[0, 1], target_wires=[2], work_wires=[3], clean=True
)
res = op.__repr__()
expected = "QROM(control_wires=<Wires = [0, 1]>, target_wires=<Wires = [2]>, work_wires=<Wires = [3]>, clean=True)"
expected = "QROM(control_wires=Wires([0, 1]), target_wires=Wires([2]), work_wires=Wires([3]), clean=True)"
assert res == expected


Expand Down
2 changes: 1 addition & 1 deletion tests/templates/test_subroutines/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_repr():
control = [1]

op = qml.Select(ops, control)
assert repr(op) == "Select(ops=(X(0), Y(0)), control=<Wires = [1]>)"
assert repr(op) == "Select(ops=(X(0), Y(0)), control=Wires([1]))"


class TestSelect:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_wires.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ def test_representation_and_string(self):

wires_str = str(Wires([1, 2, 3]))
wires_repr = repr(Wires([1, 2, 3]))
assert wires_str == "<Wires = [1, 2, 3]>"
assert wires_repr == "<Wires = [1, 2, 3]>"
assert wires_str == "Wires([1, 2, 3])"
assert wires_repr == "Wires([1, 2, 3])"

def test_array_representation(self):
"""Tests that Wires object has an array representation."""
Expand Down
Loading