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

Adds a print_applied method to QNodes #378

Merged
merged 2 commits into from Oct 24, 2019
Merged

Adds a print_applied method to QNodes #378

merged 2 commits into from Oct 24, 2019

Conversation

josh146
Copy link
Member

@josh146 josh146 commented Oct 22, 2019

Context: Currently, it is hard to debug QNodes to ensure that they are executing the correct quantum circuit, for instance, if they include classical branching/logic within the QNode.

Description of the Change: Adds a print_applied method to QNodes, that prints the operation and observable queue as last constructed.

Example:

import pennylane as qml
from pennylane.templates.embeddings import AngleEmbedding
from pennylane.templates.layers import StronglyEntanglingLayers
from pennylane.init import strong_ent_layer_uniform

dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev, interface="torch")
def circuit(weights, x=None):
    AngleEmbedding(x, [0,1])
    StronglyEntanglingLayers(weights=weights, wires=[0,1])
    return qml.expval(qml.PauliZ(0))

init_weights = strong_ent_layer_uniform(n_wires=2)

circuit(init_weights, x=[1., 2.])
circuit.print_applied()

Output:

Operations
==========
RX(1.0, wires=[0])
RX(2.0, wires=[1])
Rot(5.2934758701132685, 2.6339939891643005, 6.268309774410444, wires=[0])
Rot(2.599559228882876, 2.0359675113467888, 0.7480448828174393, wires=[1])
CNOT(wires=[0, 1])
CNOT(wires=[1, 0])

Observables
===========
expval(PauliZ(wires=[0]))

Benefits: Easier to debug complicated QNodes.

Possible Drawbacks: Since the queue is not constructed until evaluation, using print_applied() will prior to first evaluation will emit a warning message and no information.

Related GitHub Issues: n/a (was mentioned on the discussion forum).

@josh146 josh146 added enhancement ✨ New feature or request review-ready 👌 PRs which are ready for review by someone from the core team. labels Oct 22, 2019
@codecov
Copy link

codecov bot commented Oct 22, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@1953725). Click here to learn what that means.
The diff coverage is 100%.

@@            Coverage Diff            @@
##             master     #378   +/-   ##
=========================================
  Coverage          ?   99.58%           
=========================================
  Files             ?       32           
  Lines             ?     2404           
  Branches          ?        0           
=========================================
  Hits              ?     2394           
  Misses            ?       10           
  Partials          ?        0
Impacted Files Coverage Δ
pennylane/interfaces/tf.py 100% <100%> (ø)
pennylane/qnode.py 100% <100%> (ø)
pennylane/interfaces/torch.py 100% <100%> (ø)
pennylane/decorator.py 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1953725...0cacb9f. Read the comment docs.


print("\nObservables")
print("===========")
for op in self.ev:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, are the names of self.queue and self.ev changed in the new QNode? I think they are very suboptimal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should be changed, yes; feel free to double check in #354

Copy link
Contributor

@johannesjmeyer johannesjmeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this neat contribution! It looks good, I have no objections.

node.print_applied()
out = f.getvalue().strip()

assert out == "QNode has not yet been executed."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One could think of having this in a separate test, but I think two assert statements in one test are fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to keep it contained for now, as it will need to be moved to the new QNode 😆

@josh146 josh146 merged commit d3cc053 into master Oct 24, 2019
@josh146 josh146 deleted the add-print-statement branch October 24, 2019 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request review-ready 👌 PRs which are ready for review by someone from the core team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants