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

Support drawing mid-circuit measurement statistics with qml.draw #4930

Merged
merged 61 commits into from
Dec 14, 2023

Conversation

mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented Dec 8, 2023

Context:
This PR adds support for drawing the collection of mid-circuit measurement statistics with qml.draw. An example is shown below:

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 Draw qml.cond: Part 2; qml.map_wires correctly maps nested tape wires #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:

Copy link

codecov bot commented Dec 8, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (5eb005e) 99.49% compared to head (2e2ae25) 99.49%.

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.
📢 Have feedback on the report? Share it here.

@mudit2812 mudit2812 marked this pull request as ready for review December 12, 2023 23:33
@mudit2812 mudit2812 requested a review from a team December 13, 2023 16:23
Base automatically changed from mcm-measurements to master December 13, 2023 20:32
@mudit2812
Copy link
Contributor Author

[sc-49830]

@mudit2812 mudit2812 requested review from albi3ro and a team December 13, 2023 23:18
Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

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

nice stuff!

mudit2812 and others added 2 commits December 14, 2023 11:34
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

👍 Looks great :)

Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

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

✏️ 🔬 :electron: 🎉

@mudit2812 mudit2812 merged commit 90e9b5b into master Dec 14, 2023
@mudit2812 mudit2812 deleted the mcm-draw-4 branch December 14, 2023 21:06
mudit2812 added a commit that referenced this pull request Dec 15, 2023
…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>
mudit2812 added a commit that referenced this pull request Jan 19, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants