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 qml.cond: Part 1 #4803

Merged
merged 43 commits into from
Nov 24, 2023
Merged

Support drawing qml.cond: Part 1 #4803

merged 43 commits into from
Nov 24, 2023

Conversation

mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented Nov 7, 2023

Context:
Adding support for qml.cond for mid-circuit measurement drawing with qml.draw. This PR adds support for using one mid-circuit measurement with multiple qml.conds, where each qml.cond uses the same mid-circuit measurement. An example of the drawing is shown below:

import pennylane as qml

def circuit():
    qml.RX(0.5, 0)
    qml.RX(0.5, 1)
    m0 = qml.measure(0, reset=True, postselect=1)
    qml.cond(m0, qml.MultiControlledX)(wires=[1, 2, 3, 0], control_values="110")

    qml.CNOT([3, 2])
    qml.cond(m0, qml.ctrl(qml.MultiRZ, control=[1, 2], control_values=[True, False]))(0.5, wires=[0, 3])

    return qml.expval(qml.PauliZ(0))

print(qml.draw(circuit)())
0: ──RX(0.50)──┤↗₁│  │0⟩─╭X────╭MultiRZ(0.50)─┤  <Z>
1: ──RX(0.50)───║────────├●────├●─────────────┤     
2: ─────────────║────────├●─╭X─├○─────────────┤     
3: ─────────────║────────╰○─╰●─╰MultiRZ(0.50)─┤     
                ╚═════════╩═════╝                   

Note: This PR contains work that is foundational for follow up PRs but isn't used for drawing the cases mentioned above. I'll add arbitrary testing to get full coverage, but the aforementioned cases will be the only ones considered to currently be complete.

Description of the Change:

drawable_layers:

  • drawable_layers no longer stacks mid-circuit measurements used by a qml.cond in the same layer.
  • drawable layers now considers all wires under a mid-circuit measurement used for conditioning to be used.
  • drawable_layers now considers all wires under a qml.cond to be used.

tape_text:

  • Added helper functions to tape_text.py for correctly adding labels for mid-circuit measurements and qml.conds, as well as for correctly padding the width of the layers for unused wires.
  • Added helper function to tape_text.py for getting information about connectivity between mid-circuit measurements and conditional operations to keep track of when to start and stop drawing classical bits.
  • tape_text() now tracks a bit_map, which stores a mapping between mid-circuit measurements and classical bits that are drawn for them. Classical bits are drawn using double lines , and are placed under the wires in the drawing. Each mid-circuit measurement used for conditioning uses a classical bit. Measurements not used for conditioning are not allocated a bit.
  • tape_text() now pads the classical bits between each layer of the drawing along with the wires.
  • This PR also addresses [BUG] Drawing circuits with arbitrary measurements fails #4809. The changes pertaining to that are contained in tape_text.py::_add_measurements().

Other changes:

Benefits:
Drawing support for mid-circuit measurements is better :)

Possible Drawbacks:
Code in tape_text.py is less readable. After a discussion with @trbromley , we decided that the best course of action is to leave refactoring as technical debt for the end of the release cycle. Reviews pertaining to major refactors are still appreciated :)

Related GitHub Issues:
#4809

Base automatically changed from draw-mcm-1 to master November 8, 2023 17:04
@mudit2812
Copy link
Contributor Author

[sc-49112]

@mudit2812 mudit2812 linked an issue Nov 9, 2023 that may be closed by this pull request
1 task
@mudit2812
Copy link
Contributor Author

[sc-49764]

Copy link

codecov bot commented Nov 10, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (370d9d4) 99.65% compared to head (f1a9df1) 99.64%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4803      +/-   ##
==========================================
- Coverage   99.65%   99.64%   -0.02%     
==========================================
  Files         385      385              
  Lines       34719    34569     -150     
==========================================
- Hits        34599    34446     -153     
- Misses        120      123       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

mudit2812 and others added 5 commits November 21, 2023 10:01
Co-authored-by: Christina Lee <christina@xanadu.ai>
* Moved `condition.py` from the `transforms` folder to `ops/op_math`.
`qml.transforms.Conditional` is now available as `qml.ops.Conditional`.
* Updated docs accordingly.
@mudit2812 mudit2812 requested a review from a team November 21, 2023 15:51
mudit2812 and others added 2 commits November 22, 2023 13:58
Co-authored-by: Christina Lee <christina@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.

This is looking really and an impressive amount of work 🎉

@mudit2812 mudit2812 requested a review from a team November 22, 2023 22:04
@mudit2812 mudit2812 requested review from a team and removed request for a team November 23, 2023 20:31
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.

amazing! some of the content can be a bit tough to parse through... but that's tape_text for ya

@mudit2812 mudit2812 merged commit fca89b8 into master Nov 24, 2023
@mudit2812 mudit2812 deleted the draw-mcm-2 branch November 24, 2023 22:32
albi3ro added a commit that referenced this pull request Dec 7, 2023
This PR depends on #4832 and #4803 



![midmeasure_mpl](https://github.com/PennyLaneAI/pennylane/assets/6364575/06147924-684d-4bd5-a8aa-5a266bf19852)

[sc-48302]

---------

Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
mudit2812 added a commit that referenced this pull request Jan 19, 2024
This PR depends on #4832 and #4803 



![midmeasure_mpl](https://github.com/PennyLaneAI/pennylane/assets/6364575/06147924-684d-4bd5-a8aa-5a266bf19852)

[sc-48302]

---------

Co-authored-by: Mudit Pandey <mudit.pandey@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.

[BUG] Drawing circuits with arbitrary measurements fails
4 participants