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

Fix CircuitDrawer single wire multiple observables #1353

Merged
merged 16 commits into from May 27, 2021
Merged

Conversation

antalszava
Copy link
Contributor

@antalszava antalszava commented May 21, 2021

Context
QNodes can have multiple measurements on the same wire so long as they are part of qubit-wise commuting observables. Drawing such QNodes results in an error, unfortunately.

This boils down to how the CircuitGraph processes observables. It assumes that there is a single observable for every QNode or alternatively it merges observables for separate return measurement processes. When there are more than one observable per wire, it causes an error

Changes

Changes the logic in the greedy_layers method of the CircuitGraph class to account for multiple cases where there was a wire that was measured more than once.

Note: in such cases the circuit includes diagonalizations and the observables will be drawn as PauliZ operators.

Ralated Issues
Closes #1326

@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit .github/CHANGELOG.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@antalszava antalszava marked this pull request as ready for review May 21, 2021 21:56
@codecov
Copy link

codecov bot commented May 21, 2021

Codecov Report

Merging #1353 (f22d7ac) into master (305279a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1353   +/-   ##
=======================================
  Coverage   98.27%   98.28%           
=======================================
  Files         150      150           
  Lines       11262    11285   +23     
=======================================
+ Hits        11068    11091   +23     
  Misses        194      194           
Impacted Files Coverage Δ
pennylane/circuit_graph.py 93.30% <100.00%> (+0.76%) ⬆️

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 305279a...f22d7ac. Read the comment docs.

@antalszava antalszava changed the title Fix CircuitDrawer multiple outputs Fix CircuitDrawer single wire multiple observables May 21, 2021
@antalszava antalszava added the bug 🐛 Something isn't working label May 22, 2021
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

Thanks @antalszava! Have left some relatively minor thoughts and questions.

# Initialize to None everywhere
observables[wire] = [None] * max_obs_per_wire

def is_returned_observable(op):
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason this helper function is nested/using closure? If not, you could just define it top level at the top of the module file (_is_returned_obs) to avoid the overhead of creating the function within the loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right! Moved it to the top.

Comment on lines 639 to 640
def compute_max_obs_per_wire(self):
"""Computes the maximum number of observables defined per wire.
Copy link
Member

Choose a reason for hiding this comment

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

Two thoughts when reading this:

  • I would prefer a shorter name for this method; the compute seems a bit redundant. Maybe something like max_concurrent_measurements? Measurements is more general than observable.

  • Maybe the summary docstring could be made a bit more clear, it took me a couple of times to read it to realise what it meant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Went with max_measure_per_wire, how would that sound? Also adjusted the docstring.

Copy link
Member

Choose a reason for hiding this comment

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

To be honest, I find the 'per wire' a bit confusing, since it sounds like it will be returning a list (with each element corresponding to a wire).

Something like 'max_simultaneous_measurements` is much more identifiable to me?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, changed to that! Thanks :)

pennylane/circuit_graph.py Outdated Show resolved Hide resolved
pennylane/circuit_graph.py Outdated Show resolved Hide resolved
tests/circuit_drawer/test_circuit_drawer.py Outdated Show resolved Hide resolved
tests/circuit_graph/test_circuit_graph.py Show resolved Hide resolved
@antalszava antalszava requested a review from josh146 May 26, 2021 15:54
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

Thanks Antal! Happy to approve now, but recommend renaming the method to avoid the 'per_wire' terminology

pennylane/circuit_graph.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Drawing fails for (a) qubit-wise commuting observables, and (b) QNodes with no operations
2 participants