-
Notifications
You must be signed in to change notification settings - Fork 586
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
[TEMPLATES] Rewrite state preparation templates as operations #1190
Conversation
Hello. You may have forgotten to update the changelog!
|
.. warning:: | ||
|
||
Due to non-trivial classical processing of the state vector, | ||
this template is not always fully differentiable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this warning for now.
pennylane/tape/tape.py
Outdated
@@ -1103,7 +1103,9 @@ def to_openqasm(self, wires=None, rotations=True): | |||
op.inv() | |||
|
|||
# decompose the queue | |||
operations = tape.expand(stop_at=lambda obj: obj.name in OPENQASM_GATES).operations | |||
operations = tape.expand( | |||
depth=10, stop_at=lambda obj: obj.name in OPENQASM_GATES |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have one more level of "nesting", so need to define depth here.
@@ -1572,7 +1572,7 @@ def test_phase_estimated_two_qubit(self): | |||
) | |||
qml.probs(estimation_wires) | |||
|
|||
tape = tape.expand() | |||
tape = tape.expand(depth=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same problem as above...
@@ -728,7 +729,7 @@ def test_nesting_and_decomposition(self): | |||
qml.probs(wires=0), qml.probs(wires="a") | |||
|
|||
new_tape = tape.expand() | |||
assert len(new_tape.operations) == 5 | |||
assert len(new_tape.operations) == 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought here we do not have to expand to higher depth, but can just adjust the count...
pennylane/tape/tape.py
Outdated
@@ -1103,7 +1103,9 @@ def to_openqasm(self, wires=None, rotations=True): | |||
op.inv() | |||
|
|||
# decompose the queue | |||
operations = tape.expand(stop_at=lambda obj: obj.name in OPENQASM_GATES).operations | |||
operations = tape.expand( | |||
depth=2, stop_at=lambda obj: obj.name in OPENQASM_GATES |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have one more level of nesting, so need to increase the depth here.
Codecov Report
@@ Coverage Diff @@
## master #1190 +/- ##
=======================================
Coverage 98.12% 98.12%
=======================================
Files 145 145
Lines 10893 10903 +10
=======================================
+ Hits 10689 10699 +10
Misses 204 204
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to catch if state preparation is not occurring at the beginning of a circuit on a 00000... state?
I tested
@qml.qnode(dev)
def circuit():
qml.PauliX(wires=(0))
qml.templates.state_preparations.basis.BasisStatePreparation([1,0,0], wires=(0,1,2))
return qml.state()
and that lead to non-intuitive behaviour.
I left a lot of comments, but most of them are just suggestions.
The things that should really be changed are:
- placing
test_basis_state_prep.TestInputs.test_exception_wrong_dim
in the correct file - using
pytest.importorskip
in interface tests - docstring for
ArbitraryStatePreparation
pennylane/templates/state_preparations/arbitrary_state_preparation.py
Outdated
Show resolved
Hide resolved
assert shape == expected_shape | ||
|
||
|
||
def circuit_template(weights): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be fixtures? Just curious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's ok if they are just functions defined somewhere...since they change in every test, so it would be no use to make them global fixtures...Or did I understand wrong?
I am never sure if I should put them into the test class, but I find it so ugly to refer to them with self
...
tests/templates/test_state_preparations/test_arbitrary_state_prep.py
Outdated
Show resolved
Hide resolved
tests/templates/test_state_preparations/test_arbitrary_state_prep.py
Outdated
Show resolved
Hide resolved
tests/templates/test_state_preparations/test_arbitrary_state_prep.py
Outdated
Show resolved
Hide resolved
tests/templates/test_state_preparations/test_basis_state_prep.py
Outdated
Show resolved
Hide resolved
…tion.py Co-authored-by: Christina Lee <christina@xanadu.ai>
…e into rewrite-state-preps
Thanks @albi3ro for your great review! The bug about |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
It was great to learn about a new section of code and thanks for answering all my questions and nitpicks 👍
This is the third PR to refactor the templates (following 1156 and 1163).
Changes:
expand
function.__init__
function.Drawbacks:
The existing
MottonenStatePreparation
gradient test is insufficient, and it turns out that for most cases gradients do not yet work. However, this is a major task and should be done in a separate PR.