quil-lang / quilc

The optimizing Quil compiler.
Apache License 2.0
448 stars 73 forks source link

Add unicode diagram printer for parsed-program instances #918

Closed macrologist closed 3 months ago

macrologist commented 3 months ago

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.

macrologist commented 3 months ago

Yeah I think I'm good.