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

qml.layer support for tensorflow Variable #1615

Merged
merged 7 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -394,6 +394,9 @@ and requirements-ci.txt (unpinned). This latter would be used by the CI.

<h3>Bug fixes</h3>

* The `qml.layer` template now works with tensorflow variables.
[(#1615)](https://github.com/PennyLaneAI/pennylane/pull/1615)

albi3ro marked this conversation as resolved.
Show resolved Hide resolved
* Remove `QFT` from possible operations in `default.qubit` and `default.mixed`.
[(#1600)](https://github.com/PennyLaneAI/pennylane/pull/1600)

Expand Down
3 changes: 2 additions & 1 deletion pennylane/templates/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""
# pylint: disable-msg=too-many-branches,too-many-arguments,protected-access
from pennylane.templates.decorator import template as temp
from pennylane.math import shape


def _preprocess(args, depth):
Expand All @@ -30,7 +31,7 @@ def _preprocess(args, depth):

for arg in args:

if len(arg) != depth:
if shape(arg)[0] != depth:
raise ValueError(
"Each positional argument must have length matching 'depth'; expected {} got {}".format(
depth, len(arg)
Expand Down
18 changes: 18 additions & 0 deletions tests/templates/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,21 @@ def test_layer(self, unitary, depth, arguments, keywords, gates):
target = [gates[i].name, gates[i].parameters, gates[i].wires]

assert prep == target

def test_layer_tf(self):
albi3ro marked this conversation as resolved.
Show resolved Hide resolved

tf = pytest.importorskip("tensorflow")

def unitary(param):
qml.RX(param, wires=0)

x = tf.Variable([0.1, 0.2, 0.3])

with qml.tape.OperationRecorder() as rec:
layer(unitary, 3, x)

assert len(rec.operations) == 3

for ii, op in enumerate(rec.operations):
assert qml.math.allclose(op.parameters[0], x[ii])
assert isinstance(op, qml.RX)