Skip to content

Commit

Permalink
Merge pull request #250 from GTorlai/combinegates
Browse files Browse the repository at this point in the history
fixed bug
  • Loading branch information
GTorlai authored Oct 18, 2021
2 parents b0899ba + 6d6ddf2 commit 168f37a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PastaQ"
uuid = "30b07047-aa8b-4c78-a4e8-24d720215c19"
authors = ["Giacomo Torlai <[email protected]>", "Matthew Fishman <[email protected]>"]
version = "0.0.14"
version = "0.0.15"

[deps]
Convex = "f65535da-76fb-5f13-bab9-19810c17039a"
Expand Down
50 changes: 26 additions & 24 deletions src/circuits/gates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,13 @@ gate(s::String, args...; kwargs...) = gate(GateName(s), args...; kwargs...)
# Version that accepts a dimension for the gate,
gate(gn::GateName, dims::Tuple; kwargs...) = gate(gn; kwargs...)

function gate(gn::GateName, s1::Index, ss::Index...;
dag::Bool = false,
f = nothing,
kwargs...)
s = tuple(s1, ss...)
rs = reverse(s)

"""
combinegates(gn::GateName, s::Tuple; kwargs...)
Generate a gate (matrix) given a set of operations in the gatename string
"""
function combinegates(gn::GateName, s::Tuple; kwargs...)
name = string(ITensors.name(gn))
name = filter(x -> !isspace(x), name)

Expand All @@ -425,36 +425,38 @@ function gate(gn::GateName, s1::Index, ss::Index...;
!isempty(kwargs) && error("Composition of parametric gates not allowed")
gate1 = name[1:prevind(name, pluspos.start)]
gate2 = name[nextind(name, pluspos.start):end]
return gate(GateName(gate1), s...; dag=dag,f=f,kwargs...) + gate(GateName(gate2), s...; dag=dag,f=f,kwargs...)
return combinegates(GateName(gate1), dim.(s); kwargs...) + combinegates(GateName(gate2), dim.(s); kwargs...)
end

# next check for multiplication
starpos = findfirst("*", name)
if !isnothing(starpos)
!isempty(kwargs) && error("Composition of parametric gates not allowed")
gate1 = name[1:prevind(name, starpos.start)]
gate2 = name[nextind(name, starpos.start):end]
# note the inverted order, which is related to how we apply the gates
return product(gate(GateName(gate1), s...; dag=dag,f=f,kwargs...),
gate(GateName(gate2), s...; dag=dag,f=f,kwargs...))
return combinegates(GateName(gate1), dim.(s); kwargs...) * combinegates(GateName(gate2), dim.(s); kwargs...)
end
return gate(gn, dim.(s); kwargs...)
end

# if `f` is being passed
if !isnothing(f)
# if `f` isa a function, apply `f` to the gate
if f isa Function
g = f(gate(gn, dim.(s); kwargs...))
# if not, pass it as a regular argument
else
g = gate(gn, dim.(s); f = f, kwargs...)
end
else
g = gate(gn, dim.(s); kwargs...)
end
function gate(gn::GateName, s1::Index, ss::Index...;
dag::Bool = false,
f = nothing,
kwargs...)
s = tuple(s1, ss...)
rs = reverse(s)
# temporary block on f. To be revised in gate system refactoring.
!isnothing(f) && !(f isa Function) && error("gate parameter `f` not allowed")

# generate dense gate
g = combinegates(gn, s; kwargs...)

# conjugate the gate if `dag=true`
g = dag ? Array(g') : g

# apply a function if passed
g = !isnothing(f) ? f(g) : g

# generate itensor gate
if ndims(g) == 1
# TODO:
#error("gate must have more than one dimension, use state(...) for state vectors.")
Expand Down
11 changes: 11 additions & 0 deletions test/gates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,17 @@ end
G = gate("Z")*gate("S") + gate("T") * gate("X") * gate("Y")
gtest = gate("Z*S + T * X * Y", s[1])
@test PastaQ.array(gtest) G


exp_cx = exp(0.1*cx)
gtest = gate("CX",s[1],s[2]; f = x -> exp(0.1*x))
@test PastaQ.array(gtest) exp_cx

d = 3
q = siteinds("Qudit", 4; dim = d)
g1 = gate("a†a", q[1]; f = x -> exp(0.1*x))
g2 = gate("a† * a", q[1]; f = x -> exp(0.1*x))
@test g1 g2
end


Expand Down

2 comments on commit 168f37a

@GTorlai
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/46965

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.0.15 -m "<description of version>" 168f37a1e13ab1cc7f3cc7002ecec97d8d1ffe97
git push origin v0.0.15

Please sign in to comment.