Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
[WIP] Switch to Luxor backend (#56)
Browse files Browse the repository at this point in the history
* update luxor

* fix line break

* fix labelled example

* fix margin

* fix test

* ...

* more spacious

* ...

* seperate zx calculus
  • Loading branch information
GiggleLiu authored Jun 18, 2022
1 parent 2d0fc74 commit 67a79c2
Show file tree
Hide file tree
Showing 25 changed files with 286 additions and 546 deletions.
20 changes: 3 additions & 17 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
name = "YaoPlots"
uuid = "32cfe2d9-419e-45f2-8191-2267705d8dbc"
version = "0.7.5"
version = "0.8.0"

[deps]
BitBasis = "50ba71b6-fa0f-514d-ae9a-0916efc90dcf"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Compose = "a81c6b42-2e10-5240-aca2-a61377ecd94b"
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Multigraphs = "7ebac608-6c66-46e6-9856-b5f43e107bac"
Viznet = "52a3aca4-6234-47fd-b74a-806bdf78ede9"
Luxor = "ae8d54c2-7ccd-5906-9d76-62fc9837b5bc"
Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"
ZXCalculus = "3525faa3-032d-4235-a8d4-8c2939a218dd"

[compat]
BitBasis = "0.8"
Colors = "0.11, 0.12"
Compose = "0.8, 0.9"
GraphPlot = "0.5"
Graphs = "1.7"
Multigraphs = "0.3"
Viznet = "0.3.1"
ZXCalculus = "0.5"
Luxor = "3"
Yao = "0.8"
julia = "1"

Expand Down
67 changes: 9 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@

```julia
using Yao.EasyBuild, YaoPlots
using Compose

# show a qft circuit
plot(qft_circuit(5))
vizcircuit(qft_circuit(5))
```

If you are using a Pluto/Jupyter notebook, Atom/VSCode editor, you should see the following image in your plotting panel.

![qft](examples/qft.png)

Otherwise, you might be interested to learn [how to save it as an image](https://giovineitalia.github.io/Compose.jl/latest/tutorial/).

See more [examples](examples/circuits.jl).

### Adjusting the plot attributes
Expand All @@ -29,9 +26,9 @@ The plot can be modified, if we change the following attributes
- `YaoPlots.CircuitStyles.linecolor[]` for line color, default value being `"#000000"` (black color)
- `YaoPlots.CircuitStyles.gate_bgcolor[]` for background color of square blocks, the default value being `"#FFFFFF"` (white color)
- `YaoPlots.CircuitStyles.textcolor[]` for text color, default value being `"#000000`
- `YaoPlots.CircuitStyles.lw[]` for line width, default value being `1pt`
- `YaoPlots.CircuitStyles.textsize[]` for text size, default value being `16pt`
- `YaoPlots.CircuitStyles.paramtextsize[]` for parameter text size, for parameterized gates, default value being `10pt`
- `YaoPlots.CircuitStyles.lw[]` for line width, default value being `1` (pt)
- `YaoPlots.CircuitStyles.textsize[]` for text size, default value being `16` (pt)
- `YaoPlots.CircuitStyles.paramtextsize[]` for parameter text size, for parameterized gates, default value being `10` (pt)

For example,

Expand All @@ -41,57 +38,11 @@ YaoPlots.CircuitStyles.linecolor[] = "pink"
YaoPlots.CircuitStyles.gate_bgcolor[] = "yellow"
YaoPlots.CircuitStyles.textcolor[] = "#000080" # the navy blue color
YaoPlots.CircuitStyles.fontfamily[] = "JuliaMono"
YaoPlots.CircuitStyles.lw[] = 2.5pt
YaoPlots.CircuitStyles.textsize[] = 13pt
YaoPlots.CircuitStyles.paramtextsize[] = 8pt
YaoPlots.CircuitStyles.lw[] = 2.5
YaoPlots.CircuitStyles.textsize[] = 13
YaoPlots.CircuitStyles.paramtextsize[] = 8

plot(chain(3, put(1=>X), repeat(3, H), put(2=>Y), repeat(3, Rx/2))))
```

![attribute_example_2](examples/attr_circuit_2.svg)


## Example 2: Visualize ZX-diagrams in ZXCalculus
```julia
using ZXCalculus, YaoPlots
using Compose

function generate_example()
zxd = ZXDiagram(4)
push_gate!(zxd, Val{:Z}(), 1, 3//2)
push_gate!(zxd, Val{:H}(), 1)
push_gate!(zxd, Val{:Z}(), 1, 1//2)
push_gate!(zxd, Val{:H}(), 4)
push_gate!(zxd, Val{:CZ}(), 4, 1)
push_gate!(zxd, Val{:CNOT}(), 1, 4)
push_gate!(zxd, Val{:H}(), 1)
push_gate!(zxd, Val{:H}(), 4)
push_gate!(zxd, Val{:Z}(), 1, 1//4)
push_gate!(zxd, Val{:Z}(), 4, 3//2)
push_gate!(zxd, Val{:X}(), 4, 1//1)
push_gate!(zxd, Val{:H}(), 1)
push_gate!(zxd, Val{:Z}(), 4, 1//2)
push_gate!(zxd, Val{:X}(), 4, 1//1)
push_gate!(zxd, Val{:Z}(), 2, 1//2)
push_gate!(zxd, Val{:CNOT}(), 3, 2)
push_gate!(zxd, Val{:H}(), 2)
push_gate!(zxd, Val{:CNOT}(), 3, 2)
push_gate!(zxd, Val{:Z}(), 2, 1//4)
push_gate!(zxd, Val{:Z}(), 3, 1//2)
push_gate!(zxd, Val{:H}(), 2)
push_gate!(zxd, Val{:H}(), 3)
push_gate!(zxd, Val{:Z}(), 3, 1//2)
push_gate!(zxd, Val{:CNOT}(), 3, 2)

return zxd
end

zxd = generate_example() # define a example
plot(zxd) # draw a ZX-diagram
plot(ZXGraph(zxd)) # draw a graph-like ZX-diagram
vizcircuit(chain(3, put(1=>X), repeat(3, H), put(2=>Y), repeat(3, Rx/2))))
```

If you are using a Pluto/Jupyter notebook, Atom/VSCode editor, you should see the following images in your plotting panel.

![zxd](examples/demo_zxd.svg)
![zxg](examples/demo_zxg.svg)
![attribute_example_2](examples/attr_circuit_2.svg)
31 changes: 14 additions & 17 deletions examples/circuits.jl
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
using Yao.EasyBuild, YaoPlots, Yao
using Compose, Cairo

_save(str) = PNG(joinpath(@__DIR__, str))

lighttheme!()
YaoPlots.CircuitStyles.gate_bgcolor[] = "white" # default is transparent
# qft circuit
vizcircuit(qft_circuit(5)) |> _save("qft.png")
vizcircuit(qft_circuit(5); filename=joinpath(@__DIR__, "qft.png"))

# labeled and time evolution
vizcircuit(chain(control(5, 3, (2,4)=>matblock(rand_unitary(4); tag="label")),
put(5, (2,4)=>matblock(rand_unitary(4); tag="label")), time_evolve(put(5, 2=>X), 0.2))) |> _save("labelled.png")
put(5, (2,4)=>matblock(rand_unitary(4); tag="labellabel")), time_evolve(+(put(5, 2=>X), kron(5, 2=>Z, 3=>Y)), 0.2)), filename=joinpath(@__DIR__, "labelled.png"))

# variational circuit
vizcircuit(variational_circuit(5)) |> _save("variational.png")
vizcircuit(variational_circuit(5), filename=joinpath(@__DIR__, "variational.png"))
# vizcircuit(variational_circuit(5; mode=:Merged))

# general U4 gate
vizcircuit(general_U4()) |> _save("u4.png")
vizcircuit(general_U4(), filename=joinpath(@__DIR__, "u4.png"))

# quantum supremacy circuit
vizcircuit(rand_supremacy2d(2, 2, 8)) |> _save("supremacy2d.png")
vizcircuit(rand_supremacy2d(2, 2, 8), filename=joinpath(@__DIR__, "supremacy2d.png"))

# google 52 qubit
vizcircuit(rand_google53(5); scale=0.5) |> _save("google53.png")
vizcircuit(rand_google53(5); filename=joinpath(@__DIR__, "google53.png"))

# control blocks
vizcircuit(chain(control(5, (2,-3), 4=>X), control(5, (-4, -2), 1=>Z))) |> _save("controls.png")
vizcircuit(chain(control(5, (2,-3), 4=>X), control(5, (-4, -2), 1=>Z)), filename=joinpath(@__DIR__, "controls.png"))

# controlled kron
control(4, 2, (1, 3)=>kron(X, X)) |> vizcircuit |> _save("cxx.png")
vizcircuit(control(4, 2, (1, 3)=>kron(X, X)), filename=joinpath(@__DIR__, "cxx.png"))

control(4, -collect(1:4-1), 4=>-Z) |> vizcircuit |> _save("reflect.png")
vizcircuit(control(4, -collect(1:4-1), 4=>-Z), filename=joinpath(@__DIR__, "reflect.png"))

chain(5, [put(5, 2=>X), Yao.Measure(5; locs=(2,3)), Yao.Measure(5;locs=(2,)), Yao.Measure(5; resetto=bit"00110")]) |> vizcircuit |> _save("measure.png")
vizcircuit(chain(5, [put(5, 2=>X), Yao.Measure(5; locs=(2,3)), Yao.Measure(5;locs=(2,)), Yao.Measure(5; resetto=bit"00110")]), filename=joinpath(@__DIR__, "measure.png"))

chain(5, [put(5, 2=>ConstGate.Sdag), put(5, 3=>ConstGate.Tdag),
vizcircuit(chain(5, [put(5, 2=>ConstGate.Sdag), put(5, 3=>ConstGate.Tdag),
put(5, (2,3)=>ConstGate.CNOT), put(5, (1,4)=>ConstGate.CZ), put(5, (1,2,5)=>ConstGate.Toffoli),
put(5, (2,3)=>ConstGate.SWAP), put(5, (1,)=>ConstGate.P0), put(5, (1,)=>ConstGate.I2),
put(5, (2,)=>ConstGate.P1), put(5, (1,)=>ConstGate.Pu), put(5, (1,)=>ConstGate.Pd),
put(5, (2,)=>ConstGate.T),
put(5, (2,)=>phase(0.4π)),
put(5, (2,)=>shift(0.4π)),
]) |> vizcircuit |> _save("constgates.png")
]), filename=joinpath(@__DIR__, "constgates.png"))

chain(5, [put(5, (2,3)=>matblock(Matrix(SWAP), tag="SWAP")'), put(5, 2=>matblock(mat(I2), tag="id")), put(5, 2=>label(X, "X")), control(5, (5,3), (2,4,1)=>put(3, (1,3)=>label(SWAP, "SWAP")))]) |> vizcircuit |> _save("multiqubit.png")
vizcircuit(chain(5, [put(5, (2,3)=>matblock(Matrix(SWAP), tag="SWAP")'), put(5, 2=>matblock(mat(I2), tag="id")), put(5, 2=>addlabel(X, "X")), control(5, (5,3), (2,4,1)=>put(3, (1,3)=>addlabel(SWAP, "SWAP")))]), filename=joinpath(@__DIR__, "multiqubit.png"))

YaoPlots.darktheme!()
YaoPlots.CircuitStyles.gate_bgcolor[] = "transparent" # default is transparent
# qft circuit
vizcircuit(qft_circuit(5)) |> _save("qft-white.png")
vizcircuit(qft_circuit(5), filename=joinpath(@__DIR__, "qft-white.png"))

Binary file modified examples/constgates.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/controls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/cxx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/google53.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/labelled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/measure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/multiqubit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/qft-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/qft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/reflect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/supremacy2d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/u4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/variational.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 0 additions & 38 deletions examples/zx_diagrams.jl

This file was deleted.

8 changes: 5 additions & 3 deletions src/YaoPlots.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module YaoPlots

export plot
using Yao
import Luxor

using Compose
export CircuitStyles, CircuitGrid, circuit_canvas, vizcircuit, darktheme!, lighttheme!
export plot

plot(;kwargs...) = x->plot(x;kwargs...)
plot(blk::AbstractBlock; kwargs...) = vizcircuit(blk; kwargs...)

include("helperblock.jl")
include("vizcircuit.jl")
include("zx_plot.jl")

end
6 changes: 3 additions & 3 deletions src/helperblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ end

YaoBlocks.PropertyTrait(::LabelBlock) = YaoBlocks.PreserveAll()
YaoBlocks.mat(::Type{T}, blk::LabelBlock) where {T} = mat(T, content(blk))
YaoBlocks.apply!(reg::YaoBlocks.AbstractRegister, blk::LabelBlock) = apply!(reg, content(blk))
YaoBlocks.unsafe_apply!(reg::YaoBlocks.AbstractRegister, blk::LabelBlock) = YaoBlocks.unsafe_apply!(reg, content(blk))
YaoBlocks.chsubblocks(blk::LabelBlock, target::AbstractBlock) = LabelBlock(target, blk.name)

Base.adjoint(x::LabelBlock) = LabelBlock(adjoint(content(x)), endswith(x.name, "") ? x.name[1:end-1] : x.name*"")
Base.copy(x::LabelBlock) = LabelBlock(copy(content(x)), x.name)
YaoBlocks.Optimise.to_basictypes(block::LabelBlock) = block

export label
label(b::AbstractBlock, str::String) = LabelBlock(b, str)
export addlabel
addlabel(b::AbstractBlock, str::String) = LabelBlock(b, str)

Loading

0 comments on commit 67a79c2

Please sign in to comment.