From 2f3e2849a24719317f187e30dbd48f59e98e3b60 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Wed, 23 Dec 2020 16:25:06 -0500 Subject: [PATCH] Allow longer gate names by using Symbol instead of SmallString (#161) --- Project.toml | 4 ++-- src/circuits/gates.jl | 35 +++++++++-------------------------- test/gates.jl | 12 +++++++++++- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/Project.toml b/Project.toml index 4fe65767..28d7a065 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "PastaQ" uuid = "30b07047-aa8b-4c78-a4e8-24d720215c19" authors = ["Giacomo Torlai ", "Matthew Fishman "] -version = "0.0.2" +version = "0.0.3" [deps] HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" @@ -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" diff --git a/src/circuits/gates.jl b/src/circuits/gates.jl index b150c614..07897973 100644 --- a/src/circuits/gates.jl +++ b/src/circuits/gates.jl @@ -6,7 +6,7 @@ const GateName = OpName macro GateName_str(s) - GateName{ITensors.SmallString(s)} + GateName{Symbol(s)} end # @@ -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) @@ -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) @@ -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...) @@ -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") @@ -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...) @@ -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...) diff --git a/test/gates.jl b/test/gates.jl index b4d18c5f..80a2ccdf 100644 --- a/test/gates.jl +++ b/test/gates.jl @@ -1,5 +1,5 @@ using PastaQ -using ITensors +using PastaQ.ITensors using Test using LinearAlgebra @@ -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