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

Fixes bug where applying the Torch interface repeatedly after expansion caused an error #1223

Merged
merged 5 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
josh146 marked this conversation as resolved.
Show resolved Hide resolved
[(#1223)](https://github.com/PennyLaneAI/pennylane/pull/1223)

<h3>Documentation</h3>

- Typos addressed in templates documentation.
Expand Down
1 change: 1 addition & 0 deletions pennylane/tape/tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/interfaces/test_tape_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)