-
Notifications
You must be signed in to change notification settings - Fork 643
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
Support drawing mid-circuit measurement statistics with qml.draw
#4930
Conversation
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #4930 +/- ##
==========================================
- Coverage 99.49% 99.49% -0.01%
==========================================
Files 388 388
Lines 35467 35245 -222
==========================================
- Hits 35289 35066 -223
- Misses 178 179 +1 ☔ View full report in Codecov by Sentry. |
[sc-49830] |
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.
nice stuff!
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
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 :)
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.
✏️ 🔬 🎉
…4930) **Context:** This PR adds support for drawing the collection of mid-circuit measurement statistics with `qml.draw`. An example is shown below: ```python def circ(): qml.Hadamard(0) m0 = qml.measure(0) qml.Hadamard(1) m1 = qml.measure(1) qml.Hadamard(2) m2 = qml.measure(2) qml.Hadamard(3) qml.measure(3) qml.cond(m0, qml.PauliX)(0) qml.cond(m0 & m1, qml.PauliY)(1) return qml.expval(m2), qml.sample([m1, m2]) print(qml.draw(circ)()) ``` ``` 0: ──H──┤↗├──────────────────────────X────┤ 1: ──────║───H──┤↗├──────────────────║──Y─┤ 2: ──────║───────║───H──┤↗├──────────║──║─┤ 3: ──────║───────║───────║───H──┤↗├──║──║─┤ ╚═══════║═══════║═══════════╩══╣ ╚═══════║══════════════╩═╡ ╭Sample[MCM] ╚════════════════╡ <MCM> ╰Sample[MCM] ``` **Description of the Change:** * Refactor `tape_text` to only run through one loop containing all layers, not separate loops for op layers and measurement layers. The logic inside the loop has been adjusted accordingly. I also added big block comments to make the separation between different steps more obvious. * Add helpers to `tape_text.py` to add grouping symbols and labels for mid-circuit measurement statistics correctly. * Update `drawable_layers` to find layers for mid-circuit measurement statistics correctly. This included adding a new helper to recursively find layers for terminal measurements that use measurement values. * The bit map is now initialized with `None` values before everything else, and this is used to keep track of which mid-circuit measurements are used. Mostly a refactor to tidy up the code. * Remove code from `draw_mpl` that deferred measurements unconditionally for qnodes with mid-circuit measurements. This code was removed from `qml.draw` in #4901 and `qml.draw_mpl` is now updated as well. * The example in the `iterative_qpe` docstring was written before we had advanced mid-circuit measurement support and drawing capabilities, so I updated the example as well. **Benefits:** * We can draw mid-circuit measurement statistics! * Code related to the text drawer is slightly more readable. **Possible Drawbacks:** **Related GitHub Issues:** --------- Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> Co-authored-by: Christina Lee <christina@xanadu.ai> Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
…4930) **Context:** This PR adds support for drawing the collection of mid-circuit measurement statistics with `qml.draw`. An example is shown below: ```python def circ(): qml.Hadamard(0) m0 = qml.measure(0) qml.Hadamard(1) m1 = qml.measure(1) qml.Hadamard(2) m2 = qml.measure(2) qml.Hadamard(3) qml.measure(3) qml.cond(m0, qml.PauliX)(0) qml.cond(m0 & m1, qml.PauliY)(1) return qml.expval(m2), qml.sample([m1, m2]) print(qml.draw(circ)()) ``` ``` 0: ──H──┤↗├──────────────────────────X────┤ 1: ──────║───H──┤↗├──────────────────║──Y─┤ 2: ──────║───────║───H──┤↗├──────────║──║─┤ 3: ──────║───────║───────║───H──┤↗├──║──║─┤ ╚═══════║═══════║═══════════╩══╣ ╚═══════║══════════════╩═╡ ╭Sample[MCM] ╚════════════════╡ <MCM> ╰Sample[MCM] ``` **Description of the Change:** * Refactor `tape_text` to only run through one loop containing all layers, not separate loops for op layers and measurement layers. The logic inside the loop has been adjusted accordingly. I also added big block comments to make the separation between different steps more obvious. * Add helpers to `tape_text.py` to add grouping symbols and labels for mid-circuit measurement statistics correctly. * Update `drawable_layers` to find layers for mid-circuit measurement statistics correctly. This included adding a new helper to recursively find layers for terminal measurements that use measurement values. * The bit map is now initialized with `None` values before everything else, and this is used to keep track of which mid-circuit measurements are used. Mostly a refactor to tidy up the code. * Remove code from `draw_mpl` that deferred measurements unconditionally for qnodes with mid-circuit measurements. This code was removed from `qml.draw` in #4901 and `qml.draw_mpl` is now updated as well. * The example in the `iterative_qpe` docstring was written before we had advanced mid-circuit measurement support and drawing capabilities, so I updated the example as well. **Benefits:** * We can draw mid-circuit measurement statistics! * Code related to the text drawer is slightly more readable. **Possible Drawbacks:** **Related GitHub Issues:** --------- Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> Co-authored-by: Christina Lee <christina@xanadu.ai> Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Context:
This PR adds support for drawing the collection of mid-circuit measurement statistics with
qml.draw
. An example is shown below:Description of the Change:
tape_text
to only run through one loop containing all layers, not separate loops for op layers and measurement layers. The logic inside the loop has been adjusted accordingly. I also added big block comments to make the separation between different steps more obvious.tape_text.py
to add grouping symbols and labels for mid-circuit measurement statistics correctly.drawable_layers
to find layers for mid-circuit measurement statistics correctly. This included adding a new helper to recursively find layers for terminal measurements that use measurement values.None
values before everything else, and this is used to keep track of which mid-circuit measurements are used. Mostly a refactor to tidy up the code.draw_mpl
that deferred measurements unconditionally for qnodes with mid-circuit measurements. This code was removed fromqml.draw
in Drawqml.cond
: Part 2;qml.map_wires
correctly maps nested tape wires #4901 andqml.draw_mpl
is now updated as well.iterative_qpe
docstring was written before we had advanced mid-circuit measurement support and drawing capabilities, so I updated the example as well.Benefits:
Possible Drawbacks:
Related GitHub Issues: