From c4d42ec086c6d501edf30d8e31880af41c4867ec Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Fri, 16 Apr 2021 20:41:41 +0800 Subject: [PATCH 1/4] Fixes bug where repeated Torch interface after expansion caused an error --- pennylane/tape/tape.py | 1 + tests/interfaces/test_tape_torch.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pennylane/tape/tape.py b/pennylane/tape/tape.py index 60764fcf6d7..674575d1b0c 100644 --- a/pennylane/tape/tape.py +++ b/pennylane/tape/tape.py @@ -153,6 +153,7 @@ def expand_tape(tape, depth=1, stop_at=None, expand_measurements=False): stop_at = lambda obj: False new_tape = tape.__class__() + new_tape.__bare__ = getattr(tape, "__bare__", tape.__class__) # Check for observables acting on the same wire. If present, observables must be # qubit-wise commuting Pauli words. In this case, the tape is expanded with joint diff --git a/tests/interfaces/test_tape_torch.py b/tests/interfaces/test_tape_torch.py index 144104e1d78..8e9963d1876 100644 --- a/tests/interfaces/test_tape_torch.py +++ b/tests/interfaces/test_tape_torch.py @@ -453,3 +453,24 @@ def test_complex_min_version(self, monkeypatch): m.setattr(qml.interfaces.torch, "COMPLEX_SUPPORT", False) with pytest.raises(qml.QuantumFunctionError, match=r"Version 1\.6\.0 or above of PyTorch"): TorchInterface.apply(JacobianTape(), dtype=torch.complex128) + + def test_repeated_application_after_expand(self, tol): + """Test that the Torch interface continues to work after + tape expansions, and repeated torch application""" + n_qubits = 2 + dev = qml.device("default.qubit", wires=n_qubits) + + weights = torch.ones((3,)) + + with TorchInterface.apply(qml.tape.QuantumTape()) as tape: + qml.U3(*weights, wires=0) + qml.expval(qml.PauliZ(wires=0)) + + tape = tape.expand() + + res1 = tape.execute(dev) + + TorchInterface.apply(tape) + res2 = tape.execute(dev) + + assert np.allclose(res1, res2, atol=tol, rtol=0) From 1b480180a3253ae136f37fde5cc8b15a0c04fccc Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Fri, 16 Apr 2021 20:42:24 +0800 Subject: [PATCH 2/4] update changelog --- .github/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 49086b34e1f..918ee7eb296 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -770,6 +770,9 @@ second-order observable. [(#1197)](https://github.com/PennyLaneAI/pennylane/pull/1197) +* Fixes a bug where repeated Torch interface after expansion caused an error. + [(#)](https://github.com/PennyLaneAI/pennylane/pull/) +

Documentation

- Typos addressed in templates documentation. From 3613fd50805a8dd68f702467b3403954c9060a3e Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Fri, 16 Apr 2021 20:48:33 +0800 Subject: [PATCH 3/4] Update .github/CHANGELOG.md --- .github/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 918ee7eb296..1addc2d3a52 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -771,7 +771,7 @@ [(#1197)](https://github.com/PennyLaneAI/pennylane/pull/1197) * Fixes a bug where repeated Torch interface after expansion caused an error. - [(#)](https://github.com/PennyLaneAI/pennylane/pull/) + [(#1223)](https://github.com/PennyLaneAI/pennylane/pull/1223)

Documentation

From b828b2ebec5bbc80c0e0841b1a227a25bd2b771b Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Fri, 16 Apr 2021 22:24:43 +0800 Subject: [PATCH 4/4] Update .github/CHANGELOG.md Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> --- .github/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 1addc2d3a52..c5ab0bf1f94 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -770,7 +770,7 @@ second-order observable. [(#1197)](https://github.com/PennyLaneAI/pennylane/pull/1197) -* Fixes a bug where repeated Torch interface after expansion caused an error. +* Fixes a bug where repeated Torch interface applications after expansion caused an error. [(#1223)](https://github.com/PennyLaneAI/pennylane/pull/1223)

Documentation