-
Notifications
You must be signed in to change notification settings - Fork 575
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
Fix tape expand tests #2065
Fix tape expand tests #2065
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Code for the adjoint transform.""" | ||
|
||
from collections.abc import Sequence | ||
from functools import wraps | ||
from pennylane.tape import QuantumTape, stop_recording | ||
|
||
|
@@ -112,11 +112,11 @@ def circuit(): | |
@wraps(fn) | ||
def wrapper(*args, **kwargs): | ||
with stop_recording(), QuantumTape() as tape: | ||
fn(*args, **kwargs) | ||
res = fn(*args, **kwargs) | ||
|
||
if not tape.operations: | ||
# we called op.expand(): get the outputted tape | ||
tape = fn(*args, **kwargs) | ||
tape = res | ||
|
||
adjoint_ops = [] | ||
for op in reversed(tape.operations): | ||
|
@@ -130,6 +130,9 @@ def wrapper(*args, **kwargs): | |
if isinstance(new_ops, QuantumTape): | ||
new_ops = new_ops.operations | ||
|
||
if not isinstance(new_ops, Sequence): | ||
new_ops = [new_ops] | ||
Comment on lines
+133
to
+134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a missing edge case that was not being properly tested |
||
|
||
adjoint_ops.extend(new_ops) | ||
|
||
if len(adjoint_ops) == 1: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -363,9 +363,9 @@ def custom_rot(phi, theta, omega, wires): | |
|
||
|
||
# Decompose a template into another template | ||
def custom_basic_entangler_layers(weights, wires): | ||
def custom_basic_entangler_layers(weights, wires, **kwargs): | ||
return [ | ||
qml.AngleEmbedding(weights, wires=wires), | ||
qml.AngleEmbedding(weights[0], wires=wires), | ||
Comment on lines
+366
to
+368
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was not being properly tested before; |
||
qml.broadcast(qml.CNOT, pattern="ring", wires=wires), | ||
] | ||
|
||
|
@@ -495,7 +495,6 @@ def circuit(x): | |
assert np.allclose(original_grad, decomp_grad) | ||
|
||
expected_ops = ["Hadamard", "RZ", "PauliX", "RY", "PauliX", "RZ", "Hadamard"] | ||
print(decomp_qnode.qtape.operations) | ||
assert all( | ||
[op.name == name for op, name in zip(decomp_qnode.qtape.operations, expected_ops)] | ||
) | ||
|
@@ -624,12 +623,12 @@ def circuit(): | |
return qml.expval(qml.PauliZ(0)) | ||
|
||
# BasicEntanglerLayers custom decomposition involves AngleEmbedding. If | ||
# expansion depth is 1, the AngleEmbedding will still be decomposed into | ||
# expansion depth is 2, the AngleEmbedding will still be decomposed into | ||
# RX (since it's not a supported operation on the device), but the RX will | ||
# not be further decomposed even though the custom decomposition is specified. | ||
Comment on lines
625
to
628
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was not being properly tested before, and I think the logic in this comment is incorrect, it should be |
||
custom_decomps = {"BasicEntanglerLayers": custom_basic_entangler_layers, "RX": custom_rx} | ||
decomp_dev = qml.device( | ||
"default.qubit", wires=2, custom_decomps=custom_decomps, decomp_depth=1 | ||
"default.qubit", wires=2, custom_decomps=custom_decomps, decomp_depth=2 | ||
) | ||
decomp_qnode = qml.QNode(circuit, decomp_dev, expansion_strategy="device") | ||
_ = decomp_qnode() | ||
|
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 am not sure why this fixes things