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

Add unicode diagram printer for parsed-program instances #918

Merged
merged 3 commits into from
Apr 2, 2024

Conversation

macrologist
Copy link
Contributor

Adds a textual circuit diagrammer to tools.

e.g.

(defvar parsed 
    (cl-quil:parse-quil "
CONTROLLED H 0 1
CAN(1,2,3) 0 2
Y 0 
Z 1 
X 2 
SWAP 0 2
Z 2
CAN(pi/2,pi/2,pi/4) 1 2
SWAP 1 0
CNOT 1 0
SWAP 0 3
CNOT 0 1
CNOT 2 0
CONTROLLED CONTROLLED H 3 2 1
"))

(cl-quil:tools:print-program-diagram parsed)

prints


            0                       1                        2             3
            ┬                       ┬                        ┬             ┬
            │                       │                        │             │
            │                      ╭┴╮                       │             │
            ·──────────────────────│H│                       │             │
            │                      ╰┬╯                       │             │
            │                       │                        │             │
            │                      ╭┴╮                       │             │
            │                      │Z│                       │             │
            │                      ╰┬╯                       │             │
            │                       │                        │             │
          ╭─┴────────────────────────────────────────────────┴─╮           │
          │                CAN(1.0, 2.0, 3.0)                  │           │
          ╰─┬────────────────────────────────────────────────┬─╯           │
            │                       │                        │             │
           ╭┴╮                      │                       ╭┴╮            │
           │Y│                      │                       │X│            │
           ╰┬╯                      │                       ╰┬╯            │
            │                       │                        │             │
            │                       │                        │             │
            ×────────────────────────────────────────────────×             │
            │                       │                        │             │
            │                       │                        │             │
            │                       │                       ╭┴╮            │
            │                       │                       │Z│            │
            │                       │                       ╰┬╯            │
            │                       │                        │             │
            │                     ╭─┴────────────────────────┴─╮           │
            │                     │   CAN(pi/2, pi/2, pi/4)    │           │
            │                     ╰─┬────────────────────────┬─╯           │
            │                       │                        │             │
            │                       │                        │             │
            ×───────────────────────×                        │             │
            │                       │                        │             │
            │                       │                        │             │
            │                       │                        │             │
            ⊕───────────────────────·                        │             │
            │                       │                        │             │
            │                       │                        │             │
            │                       │                        │             │
            ×──────────────────────────────────────────────────────────────×
            │                       │                        │             │
            │                       │                        │             │
            │                       │                        │             │
            ·───────────────────────⊕                        │             │
            │                       │                        │             │
            │                       │                        │             │
            │                       │                        │             │
            ⊕────────────────────────────────────────────────·             │
            │                       │                        │             │
            │                       │                        │             │
            │                      ╭┴╮                       │             │
            │                      │H│───────────────────────2─────────────1
            │                      ╰┬╯                       │             │
NIL

A logical schedule of the circuit is firsted built, and then the circuit diagram is printed to reflect the logical ordering of operations. Hence, in the above, Z 1 appears before the CAN gate in the diagram. This is because the two operations commute.

Copy link
Member

@stylewarning stylewarning left a comment

Choose a reason for hiding this comment

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

Cool code. Only request is to add some comment documentation to the top the file (give the lay of the land, key ideas/objects/methods), and add some docstrings that would help a future you in 5 years.

Copy link
Member

@stylewarning stylewarning left a comment

Choose a reason for hiding this comment

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

LGTM; let me know if you're ready to have this merged

@macrologist
Copy link
Contributor Author

Yeah I think I'm good.

@stylewarning stylewarning merged commit 01eb624 into quil-lang:master Apr 2, 2024
3 checks passed
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.

2 participants