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

Commit

Permalink
barrier for chain (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu authored Oct 20, 2022
1 parent a6740b6 commit 0157a3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/vizcircuit.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module CircuitStyles
using Luxor
const unit = Ref(60) # number of points in a unit
const barrier_for_chain = Ref(false)
const r = Ref(0.2)
const lw = Ref(1.0)
const textsize = Ref(16.0)
Expand Down Expand Up @@ -284,7 +285,20 @@ end

# composite
function draw!(c::CircuitGrid, p::ChainBlock, address, controls)
draw!.(Ref(c), subblocks(p), Ref(address), Ref(controls))
for block in subblocks(p)
draw!(c, block, address, controls)
CircuitStyles.barrier_for_chain[] && set_barrier!(c, Int[address..., controls...])
end
end

function set_barrier!(c::CircuitGrid, locs::AbstractVector{Int})
front = maximum(c.frontier[locs])
for loc in locs
if c.frontier[loc] < front
CircuitStyles.render(c.gatestyles.line, c[(c.frontier[loc], loc); (front, loc)])
c.frontier[loc] = front
end
end
end

function draw!(c::CircuitGrid, p::PutBlock, address, controls)
Expand Down Expand Up @@ -327,7 +341,10 @@ for B in [:LabelBlock, :GeneralMatrixBlock, :Add]
end
for GT in [:KronBlock, :RepeatedBlock, :CachedBlock, :Subroutine, :(YaoBlocks.AD.NoParams)]
@eval function draw!(c::CircuitGrid, p::$GT, address, controls)
barrier_style = CircuitStyles.barrier_for_chain[]
CircuitStyles.barrier_for_chain[] = false
draw!(c, YaoBlocks.Optimise.to_basictypes(p), address, controls)
CircuitStyles.barrier_for_chain[] = barrier_style
end
end
for (GATE, SYM) in [(:XGate, :Rx), (:YGate, :Ry), (:ZGate, :Rz)]
Expand Down
11 changes: 11 additions & 0 deletions test/vizcircuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@ end

@testset "regression" begin
@test vizcircuit(put(10, (8,2,3)=>EasyBuild.heisenberg(3)), starting_texts=string.(1:10), ending_texts=string.(1:10), show_ending_bar=true) isa Drawing
end

@testset "readme" begin
circuit = chain(
4,
kron(X, H, H, H),
kron(1=>Y, 4=>H),
put(2=>Y),
)
YaoPlots.CircuitStyles.barrier_for_chain[] = true
@test vizcircuit(circuit) isa Drawing
end

0 comments on commit 0157a3e

Please sign in to comment.