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

New text circuit drawer #1885

Merged
merged 44 commits into from
Jan 26, 2022
Merged

New text circuit drawer #1885

merged 44 commits into from
Jan 26, 2022

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Nov 10, 2021

This method can hopefully replace CircuitGraph.draw() and associated objects like CircuitDrawer, RepresentationResolver and Charset.

This function already:

Downsides: This function does not display matrix-valued parameters below the circuit diagram. Circuit drawings are for inspecting whether or not everything is generally in the correct place. Users can inspect specific parameters via other means.

with qml.tape.QuantumTape() as tape:
    qml.QFT(wires=(0,1,2))
    qml.RX(1.234, wires=0)
    qml.RY(1.234, wires=1)
    qml.RZ(1.234, wires=2)
    qml.Toffoli(wires=(0,1,"aux"))
    
    qml.expval(qml.PauliZ("aux"))
    qml.state()


>>> print(draw_text(tape))
  0: ─╭QFT──RX─╭C─┤     State
  1: ─├QFT──RY─├C─┤     State
  2: ─╰QFT──RZ─│──┤     State
aux: ──────────╰X─┤ <Z> State

@albi3ro albi3ro added the WIP 🚧 Work-in-progress label Nov 10, 2021
@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.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.

@josh146
Copy link
Member

josh146 commented Nov 10, 2021

Users can inspect specific parameters via other means.

@albi3ro does the new circuit drawer preserve the ability to print scalar parameters, e.g.,

>>> print(draw_text(tape))
  0: ─╭QFT──RX(0.1)─╭C─┤     State
  1: ─├QFT──RY(0.2)─├C─┤     State
  2: ─╰QFT──RZ(0.3)─│──┤     State
aux: ───────────────╰X─┤ <Z> State

I would say that this is a very important feature, since we use this to visualize circuit diagrams with rotation parameters across the docs + blog + demos!

@albi3ro
Copy link
Contributor Author

albi3ro commented Nov 10, 2021

@josh146 Yes it can print scaler parameters via something like draw_text(tape, decimals=2). It just won't print out matrices. Though we could add that in if people really want it.

@josh146
Copy link
Member

josh146 commented Dec 16, 2021

Hey all, just curious on the timeframe for this PR?

@albi3ro
Copy link
Contributor Author

albi3ro commented Dec 16, 2021

@josh146 I'm going to make it my first priority next quarter. Mostly just test writing at this point, but those are going to be extensive. It could probably use a round of pre-review if anyone has the time.

@albi3ro albi3ro changed the title [WIP] New text circuit drawer New text circuit drawer Jan 12, 2022
@albi3ro albi3ro removed the WIP 🚧 Work-in-progress label Jan 12, 2022
@dime10
Copy link
Contributor

dime10 commented Jan 14, 2022

Something I noticed while comparing the old drawer to the new, is the change in the qml.PauliRot representation deliberate? :)

0: ─╭QFT──X─╭C─╭X─────╭R(XYY)─┤ ╭<X@Y>
1: ─├QFT────│──├C──RX─├R(XYY)─┤ ╰<X@Y>
2: ─╰QFT────╰X─╰C─────╰R(XYY)─┤  Probs
 0: ──╭QFT──X──╭C──╭X───────────────╭RX(0.1)──╭┤ ⟨X ⊗ Y⟩ 
 1: ──├QFT─────│───├C──RX(0.00335)──├RY(0.1)──╰┤ ⟨X ⊗ Y⟩ 
 2: ──╰QFT─────╰X──╰C───────────────╰RY(0.1)───┤ Probs   

Another thing I'm curious about is the capability of drawing multiple measurement layers. Was this added with complex measurement processes in mind or for practical reasons?

@albi3ro
Copy link
Contributor Author

albi3ro commented Jan 21, 2022

[sc-12939]

@albi3ro albi3ro requested a review from dime10 January 21, 2022 17:32
Copy link
Contributor

@Jaybsoni Jaybsoni left a comment

Choose a reason for hiding this comment

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

Just a few small things but other than that it looks really good! 8x speed up is insane!

pennylane/drawer/tape_text.py Outdated Show resolved Hide resolved
pennylane/drawer/tape_text.py Show resolved Hide resolved

for op in layer:
# Currently can't use `isinstance(op, QuantumTape)` due to circular imports
if isinstance(op, qml.tape.QuantumTape): # isa tape
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if isinstance(op, qml.tape.QuantumTape): # isa tape
if isinstance(op, qml.tape.QuantumTape): # is a tape

pennylane/drawer/tape_text.py Outdated Show resolved Hide resolved
2: ─╰QFT──RZ─│──┤ Probs
aux: ──────────╰X─┤ <Z> Probs

.. UsageDetails::
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be worth it to add a usage case example for the tape_offset kwarg?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

adding.

tests/ops/qubit/test_hamiltonian.py Outdated Show resolved Hide resolved
@Jaybsoni
Copy link
Contributor

Just another thing I came across when playing around with it. There are two features of the old text drawer that don't pass over: the numbering of QubitUnitary operations don't show up and the basis with respect to which we sample our circuit isn't specified.

I think the first feature is nice to have since we now omit showing the unitary matrices; if they are labeled the user can at least keep track of them and see if / where they repeat in the circuit.

The second feature would also be a nice to have as it just provides more info in general, but maybe not fully required.

Here is an example of what the current behaviour is:
image

albi3ro and others added 2 commits January 24, 2022 14:06
Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
@albi3ro albi3ro requested a review from Jaybsoni January 25, 2022 16:45
Copy link
Contributor

@Jaybsoni Jaybsoni 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!

@albi3ro albi3ro merged commit c597abc into master Jan 26, 2022
@albi3ro albi3ro deleted the new_text_drawer branch January 26, 2022 16:13
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.

None yet

5 participants