Skip to content

Commit

Permalink
Type promotion between MixedDestabilizer, Stabilizer and others, also…
Browse files Browse the repository at this point in the history
… used in ⊗ (#354)


---------

Co-authored-by: Stefan Krastanov <github.acc@krastanov.org>
  • Loading branch information
Fe-r-oz and Krastanov committed Sep 27, 2024
1 parent e5b7bd5 commit 8173b45
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
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 = mds
@test mixed_destab_looks_good(mds)
estab = stabilizerview(md)s
@test canonicalize!(copy(stabilizerview(mds))) == canonicalize!(estab)
end
end

Expand Down

2 comments on commit 8173b45

@Krastanov
Copy link
Member

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/116102

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.9.10 -m "<description of version>" 8173b45ad4fbc417d24cd10516f86a0d123a02e8
git push origin v0.9.10

Please sign in to comment.