Skip to content

Commit

Permalink
optimize projection gate
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Jan 14, 2024
1 parent bb7a6ec commit 48bde94
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ YaoToEinsumCUDAExt = "CUDA"

[compat]
CUDA = "4, 5"
OMEinsum = "0.7"
OMEinsum = "0.8"
Yao = "0.8"
julia = "1.9"

Expand Down
26 changes: 26 additions & 0 deletions src/circuitmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ function add_matrix!(eb::EinBuilder{T}, k::Int, m::AbstractMatrix, locs::Vector)
nlabels = [newlabel!(eb) for _=1:k]
add_tensor!(eb, reshape(Matrix{T}(m), fill(2, 2k)...), [nlabels..., eb.slots[locs]...])
eb.slots[locs] .= nlabels
elseif m isa Yao.OuterProduct # low rank
nlabels = [newlabel!(eb) for _=1:k]
K = rank(m)
if K == 1 # projector
add_tensor!(eb, reshape(Vector{T}(m.right), fill(2, k)...), [eb.slots[locs]...])
add_tensor!(eb, reshape(Vector{T}(m.left), fill(2, k)...), [nlabels...])
eb.slots[locs] .= nlabels
else
midlabel = newlabel!(eb)
add_tensor!(eb, reshape(Matrix{T}(m.right), fill(2, k)..., K), [eb.slots[locs]..., midlabel])
add_tensor!(eb, reshape(Matrix{T}(m.left), fill(2, k)..., K), [nlabels..., midlabel])

Check warning on line 39 in src/circuitmap.jl

View check run for this annotation

Codecov / codecov/patch

src/circuitmap.jl#L37-L39

Added lines #L37 - L39 were not covered by tests
eb.slots[locs] .= nlabels
end
else
add_tensor!(eb, reshape(Vector{T}(diag(m)), fill(2, k)...), eb.slots[locs])
end
Expand All @@ -39,6 +52,19 @@ function add_gate!(eb::EinBuilder{T}, b::PutBlock{2,2,ConstGate.SWAPGate}) where
return eb
end

# projection gate, todo: generalize to arbitrary low rank gate
function add_gate!(eb::EinBuilder{T}, b::PutBlock{2,1,ConstGate.P0Gate}) where {T}
add_matrix!(eb, 1, Yao.OuterProduct(T[1, 0], T[1, 0]), collect(b.locs))
return eb
end

# projection gate, todo: generalize to arbitrary low rank gate
function add_gate!(eb::EinBuilder{T}, b::PutBlock{2,1,ConstGate.P1Gate}) where {T}
add_matrix!(eb, 1, Yao.OuterProduct(T[0, 1], T[0, 1]), collect(b.locs))
return eb
end


# control gates
function add_gate!(eb::EinBuilder{T}, b::ControlBlock{BT,C,M}) where {T, BT,C,M}
return add_controlled_matrix!(eb, M, mat(T, b.content), collect(b.locs), collect(b.ctrl_locs), collect(b.ctrl_config))
Expand Down
2 changes: 1 addition & 1 deletion test/circuitmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using SymEngine

@testset "YaoToEinsum.jl" begin
n = 5
for c in [put(n, 2=>Y), put(n, (5,3)=>SWAP), put(n, (4,2)=>ConstGate.CNOT), put(n, (2,3,1)=>kron(ConstGate.CNOT, X)),
for c in [put(n, 2=>Y), put(n, 2=>ConstGate.P0), put(n, 2=>ConstGate.P1), put(n, (5,3)=>SWAP), put(n, (4,2)=>ConstGate.CNOT), put(n, (2,3,1)=>kron(ConstGate.CNOT, X)),
put(n, 2=>Z), control(n, -3, 2=>X), control(n, 3, 2=>X), control(n, (2, -1), 3=>Y), control(n, (4,1,-2), 5=>Z)]
@show c
C = chain([put(n, i=>Rx(rand()*2π)) for i=1:n]..., c)
Expand Down

0 comments on commit 48bde94

Please sign in to comment.