Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type promotion between MixedDestabilizer, Stabilizer and others, also used in ⊗ #354

Merged
merged 10 commits into from
Sep 27, 2024
10 changes: 10 additions & 0 deletions src/QuantumClifford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,16 @@ function _apply!(stab::AbstractStabilizer, p::PauliOperator, indices; phases::Va
stab
end

##############################
# Conversion and promotion
##############################

Base.promote_rule(::Type{<:Destabilizer{T}} , ::Type{<:MixedDestabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T}
Base.promote_rule(::Type{<:MixedStabilizer{T}}, ::Type{<:MixedDestabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T}
Base.promote_rule(::Type{<:Stabilizer{T}} , ::Type{<:S} ) where {T<:Tableau, S<:Union{MixedStabilizer{T}, Destabilizer{T}, MixedDestabilizer{T}}} = S

Base.convert(::Type{<:MixedDestabilizer{T}}, x::Union{Destabilizer{T}, MixedStabilizer{T}, Stabilizer{T}}) where {T <: Tableau} = MixedDestabilizer(x)

##############################
# Helpers for binary codes
##############################
Expand Down
6 changes: 4 additions & 2 deletions src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ julia> tensor(s, s)
See also [`tensor_pow`](@ref)."""
function tensor end

function tensor(ops::AbstractStabilizer...) # TODO optimize this by doing conversion to common type to enable preallocation
foldl(⊗, ops[2:end], init=ops[1])
function tensor(ops::AbstractStabilizer...) # TODO optimize by pre-allocating one large tableau instead of the current quadratic fold
ct = promote_type(map(typeof, ops)...)
conv_ops = map(x -> convert(ct, x), ops)
return foldl(⊗, conv_ops)
end

"""Repeated tensor product of an operators or a tableau.
Expand Down
6 changes: 6 additions & 0 deletions test/test_stabs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
stabs = [s[1:i] for s in [random_stabilizer(n) for n in [32,16,16,64,63,65,129,128,127]] for i in rand(1:10)];
mdstabs = MixedDestabilizer.(stabs);
@test canonicalize!(⊗(stabs...)) == canonicalize!(stabilizerview(⊗(mdstabs...)))
md = MixedDestabilizer(random_destabilizer(n))
s = random_stabilizer(n)
mds = md⊗s
@test mixed_destab_looks_good(mds)
estab = stabilizerview(md)⊗s
@test canonicalize!(copy(stabilizerview(mds))) == canonicalize!(estab)
end
end

Expand Down
Loading