Skip to content

Commit

Permalink
Allow longer gate names by using Symbol instead of SmallString (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Dec 23, 2020
1 parent d0fa5ed commit 2f3e284
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "PastaQ"
uuid = "30b07047-aa8b-4c78-a4e8-24d720215c19"
authors = ["Giacomo Torlai <[email protected]>",
"Matthew Fishman <[email protected]>"]
version = "0.0.2"
version = "0.0.3"

[deps]
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
Expand All @@ -14,6 +14,6 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
HDF5 = "0.13.1, 0.14"
ITensors = "0.1.33"
ITensors = "0.1.34"
StatsBase = "0.33"
julia = "1.4"
35 changes: 9 additions & 26 deletions src/circuits/gates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const GateName = OpName

macro GateName_str(s)
GateName{ITensors.SmallString(s)}
GateName{Symbol(s)}
end

#
Expand Down Expand Up @@ -311,7 +311,7 @@ function gate(::GateName"AD"; γ::Number)
end

# To accept the gate name "amplitude_damping"
gate(::GateName"amplitud"; kwargs...) = gate("AD"; kwargs...)
gate(::GateName"amplitude_damping"; kwargs...) = gate("AD"; kwargs...)

function gate(::GateName"PD"; γ::Number)
kraus = zeros(2,2,2)
Expand All @@ -323,10 +323,10 @@ function gate(::GateName"PD"; γ::Number)
end

# To accept the gate name "phase_damping"
gate(::GateName"phase_da"; kwargs...) = gate("PD"; kwargs...)
gate(::GateName"phase_damping"; kwargs...) = gate("PD"; kwargs...)

# To accept the gate name "dephasing"
gate(::GateName"dephasin"; kwargs...) = gate("PD"; kwargs...)
gate(::GateName"dephasing"; kwargs...) = gate("PD"; kwargs...)

function gate(::GateName"DEP"; p::Number)
kraus = zeros(Complex{Float64},2,2,4)
Expand All @@ -342,7 +342,7 @@ function gate(::GateName"DEP"; p::Number)
end

# To accept the gate name "depolarizing"
gate(::GateName"depolari"; kwargs...) = gate("DEP"; kwargs...)
gate(::GateName"depolarizing"; kwargs...) = gate("DEP"; kwargs...)

gate(::GateName"noiseDEP"; kwargs...) =
gate("DEP";kwargs...)
Expand Down Expand Up @@ -397,15 +397,6 @@ end
# Get an ITensor gate from a gate definition
#

function Base.startswith(ss::ITensors.SmallString, st::String)
for j in 1:length(st)
if ss[j] st[j]
return false
end
end
return true
end

function gate(::GateName{gn}; kwargs...) where {gn}
gn_st = String(gn)
if startswith(gn_st, "basis")
Expand Down Expand Up @@ -441,12 +432,8 @@ function gate(gn::GateName, s1::Index, ss::Index...; kwargs...)
error("Gate definitions must be either Vector{T} (for a state), Matrix{T} (for a gate) or Array{T,3} (for a noise model). For gate name $gn, gate size is $(size(g)).")
end

function gate(gn::String, s::Index...; kwargs...)
if length(gn) > 8
gn = gn[1:8]
end
return gate(GateName(gn), s...; kwargs...)
end
gate(gn::String, s::Index...; kwargs...) =
gate(GateName(gn), s...; kwargs...)

gate(gn::String, s::Vector{<: Index}, ns::Int...; kwargs...) =
gate(gn, s[[ns...]]...; kwargs...)
Expand All @@ -456,10 +443,6 @@ gate(gn::String, s::Vector{<: Index}, ns::Int...; kwargs...) =
# definitions of the "Qubit" site type
#

function ITensors.op(gn::GateName,
::SiteType"Qubit",
s::Index...;
kwargs...)
return gate(gn, s...; kwargs...)
end
ITensors.op(gn::GateName, ::SiteType"Qubit", s::Index...; kwargs...) =
gate(gn, s...; kwargs...)

12 changes: 11 additions & 1 deletion test/gates.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using PastaQ
using ITensors
using PastaQ.ITensors
using Test
using LinearAlgebra

Expand Down Expand Up @@ -144,5 +144,15 @@ using LinearAlgebra
end
end

@testset "Custom gate with long name" begin
PastaQ.gate(::GateName"my_favorite_gate") = [0.11 0.12; 0.21 0.22]
s = Index(2, "Qubit, Site")
gate("my_favorite_gate", s)
g = gate("my_favorite_gate", s)
@test g[s'=>1, s=>1] == 0.11
@test g[s'=>1, s=>2] == 0.12
@test g[s'=>2, s=>1] == 0.21
@test g[s'=>2, s=>2] == 0.22
end
end

2 comments on commit 2f3e284

@mtfishman
Copy link
Collaborator 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/26853

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.3 -m "<description of version>" 2f3e2849a24719317f187e30dbd48f59e98e3b60
git push origin v0.0.3

Please sign in to comment.