From 7da1f9c28b1f98940ca5ca1973cce55997b9426e Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sat, 7 Sep 2024 12:23:30 +0500 Subject: [PATCH 1/9] =?UTF-8?q?Bug=20fix=20to=20StackOverflowError=20for?= =?UTF-8?q?=20md=20=E2=8A=97=20SX=20where=20typeof(md)=20is=20MixedDestabi?= =?UTF-8?q?lizer{...}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/linalg.jl | 70 ++++++++++++++++++++++++++++++++++++++++++++-- test/test_stabs.jl | 6 ++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/linalg.jl b/src/linalg.jl index 16b4eaf52..26a4a07e1 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -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...) + 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. @@ -264,3 +266,67 @@ function tensor(ops::CliffordOperator...) # TODO implement \otimes for Destabili end CliffordOperator(tab) end + +""" +Tensor product between [`MixedDestabilizer`](@ref) and [`Stabilizer`](@ref). + +```jldoctest +julia> md = MixedDestabilizer(T"Z//X", 1) +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท ++ Z +๐’ฎ๐“‰๐’ถ๐’ท ++ X + +julia> md โŠ— S"X" +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท ++ Z_ ++ _Z +๐’ฎ๐“‰๐’ถ๐’ท ++ X_ ++ _X + +julia> S"X" โŠ— md +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท ++ Z_ ++ _Z +๐’ฎ๐“‰๐’ถ๐’ท ++ X_ ++ _X + +julia> S"X" โŠ— md โŠ— S"X" โŠ— md +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท ++ Z___ ++ _Z__ ++ __Z_ ++ ___Z +๐’ฎ๐“‰๐’ถ๐’ทโ”โ” ++ X___ ++ _X__ ++ __X_ ++ ___X +``` +""" +function tensor(md::MixedDestabilizer, s::Stabilizer) + ntot_md = nqubits(md) + ntot_s = nqubits(s) + ntot = ntot_md + ntot_s + rtot_md = LinearAlgebra.rank(md) + rtot_s = length(s) + rtot = rtot_md + rtot_s + tab = zero(Tableau, 2*ntot, ntot) + last_dvrow = 0 + last_svrow = ntot + last_col = 0 + md1 = MixedDestabilizer(s) + last_lxrow = rtot + last_lzrow = ntot+rtot + for op in [md, md1] + _, last_svrow, _ = puttableau!(tab, stabilizerview(op), last_svrow, last_col) + _, last_dvrow, _ = puttableau!(tab, destabilizerview(op), last_dvrow, last_col) + _, last_lxrow, _ = puttableau!(tab, logicalxview(op), last_lxrow, last_col) + _, last_lzrow, last_col = puttableau!(tab, logicalzview(op), last_lzrow, last_col) + end + return MixedDestabilizer(tab, rtot) +end + +tensor(s::Stabilizer, md::MixedDestabilizer) = tensor(md, s) diff --git a/test/test_stabs.jl b/test/test_stabs.jl index 63a5c562e..d415621ea 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -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 From 0435a8b81c6c572848e80cc6ff44a1c44f287f80 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sun, 15 Sep 2024 03:19:06 +0500 Subject: [PATCH 2/9] add code review suggestions --- src/QuantumClifford.jl | 13 +++++ src/linalg.jl | 113 ++++++++++++++++++----------------------- test/test_stabs.jl | 6 +++ 3 files changed, 68 insertions(+), 64 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index 407af5959..c025b2d2e 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -9,6 +9,7 @@ module QuantumClifford import LinearAlgebra using LinearAlgebra: inv, mul!, rank, Adjoint, dot +import Base: promote_rule, convert import DataStructures using DataStructures: DefaultDict, Accumulator using Combinatorics: combinations @@ -989,6 +990,18 @@ function _apply!(stab::AbstractStabilizer, p::PauliOperator, indices; phases::Va stab end +############################## +# Conversion and promotion +############################## + +promote_rule(::Type{<:MixedStabilizer{T}}, ::Type{<:Stabilizer{T}}) where {T<:Tableau} = Stabilizer{T} +promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} +promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Destabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} + +convert(::Type{<:MixedDestabilizer{T}}, x::Stabilizer{T}) where {T<:Tableau} = MixedDestabilizer(x) +convert(::Type{<:Stabilizer{T}}, x::MixedStabilizer{T}) where {T<:Tableau} = Stabilizer(tab(x)) +convert(::Type{<:MixedDestabilizer{T}}, x::Destabilizer{T}) where {T<:Tableau} = MixedDestabilizer(x) + ############################## # Helpers for binary codes ############################## diff --git a/src/linalg.jl b/src/linalg.jl index 26a4a07e1..4a012e969 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -158,6 +158,55 @@ julia> tensor(s, s) - ____Z_ ``` +Tensor product between [`MixedDestabilizer`](@ref) and [`Stabilizer`](@ref). + +```jldoctest promote +julia> md = MixedDestabilizer(T"Z//X", 1) +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท ++ Z +๐’ฎ๐“‰๐’ถ๐’ท ++ X + +julia> tensor(S"X", md) +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท ++ Z_ ++ _Z +๐’ฎ๐“‰๐’ถ๐’ท ++ X_ ++ _X +``` + +Tensor product between [`MixedDestabilizer`](@ref) and [`Destabilizer`](@ref). + +```jldoctest promote +julia> d = Destabilizer(T"-Y//-X") +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท +- Y +๐’ฎ๐“‰๐’ถ๐’ท +- X + +julia> tensor(d, md) +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท +- Y_ ++ _Z +๐’ฎ๐“‰๐’ถ๐’ท +- X_ ++ _X +``` + +Tensor product between [`MixedStabilizer`](@ref) and [`Stabilizer`](@ref). + +```jldoctest +julia> ms = MixedStabilizer(S"-XZ//-ZX") +- XZ +- ZX + +julia> tensor(S"X", ms) ++ X__ +- _XZ +- _ZX +``` + See also [`tensor_pow`](@ref).""" function tensor end @@ -266,67 +315,3 @@ function tensor(ops::CliffordOperator...) # TODO implement \otimes for Destabili end CliffordOperator(tab) end - -""" -Tensor product between [`MixedDestabilizer`](@ref) and [`Stabilizer`](@ref). - -```jldoctest -julia> md = MixedDestabilizer(T"Z//X", 1) -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -+ Z -๐’ฎ๐“‰๐’ถ๐’ท -+ X - -julia> md โŠ— S"X" -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -+ Z_ -+ _Z -๐’ฎ๐“‰๐’ถ๐’ท -+ X_ -+ _X - -julia> S"X" โŠ— md -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -+ Z_ -+ _Z -๐’ฎ๐“‰๐’ถ๐’ท -+ X_ -+ _X - -julia> S"X" โŠ— md โŠ— S"X" โŠ— md -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -+ Z___ -+ _Z__ -+ __Z_ -+ ___Z -๐’ฎ๐“‰๐’ถ๐’ทโ”โ” -+ X___ -+ _X__ -+ __X_ -+ ___X -``` -""" -function tensor(md::MixedDestabilizer, s::Stabilizer) - ntot_md = nqubits(md) - ntot_s = nqubits(s) - ntot = ntot_md + ntot_s - rtot_md = LinearAlgebra.rank(md) - rtot_s = length(s) - rtot = rtot_md + rtot_s - tab = zero(Tableau, 2*ntot, ntot) - last_dvrow = 0 - last_svrow = ntot - last_col = 0 - md1 = MixedDestabilizer(s) - last_lxrow = rtot - last_lzrow = ntot+rtot - for op in [md, md1] - _, last_svrow, _ = puttableau!(tab, stabilizerview(op), last_svrow, last_col) - _, last_dvrow, _ = puttableau!(tab, destabilizerview(op), last_dvrow, last_col) - _, last_lxrow, _ = puttableau!(tab, logicalxview(op), last_lxrow, last_col) - _, last_lzrow, last_col = puttableau!(tab, logicalzview(op), last_lzrow, last_col) - end - return MixedDestabilizer(tab, rtot) -end - -tensor(s::Stabilizer, md::MixedDestabilizer) = tensor(md, s) diff --git a/test/test_stabs.jl b/test/test_stabs.jl index d415621ea..548e92d1c 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -61,6 +61,12 @@ @test mixed_destab_looks_good(mds) estab = stabilizerview(md)โŠ—s @test canonicalize!(copy(stabilizerview(mds))) == canonicalize!(estab) + d = random_stabilizer(n) + dmd = dโŠ—md + @test mixed_destab_looks_good(dmd) + ms = MixedStabilizer(random_stabilizer(n)) + mss = sโŠ—ms + @test mixed_stab_looks_good(mss) end end From a06f66e19e25bbd853ef8d3c28662059bdf93bc1 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sun, 15 Sep 2024 03:29:47 +0500 Subject: [PATCH 3/9] improve code formatting --- src/QuantumClifford.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index c025b2d2e..96833ff69 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -9,7 +9,6 @@ module QuantumClifford import LinearAlgebra using LinearAlgebra: inv, mul!, rank, Adjoint, dot -import Base: promote_rule, convert import DataStructures using DataStructures: DefaultDict, Accumulator using Combinatorics: combinations @@ -994,13 +993,13 @@ end # Conversion and promotion ############################## -promote_rule(::Type{<:MixedStabilizer{T}}, ::Type{<:Stabilizer{T}}) where {T<:Tableau} = Stabilizer{T} -promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} -promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Destabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} +Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:Stabilizer{T}}) where {T<:Tableau} = Stabilizer{T} +Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} +Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Destabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} -convert(::Type{<:MixedDestabilizer{T}}, x::Stabilizer{T}) where {T<:Tableau} = MixedDestabilizer(x) -convert(::Type{<:Stabilizer{T}}, x::MixedStabilizer{T}) where {T<:Tableau} = Stabilizer(tab(x)) -convert(::Type{<:MixedDestabilizer{T}}, x::Destabilizer{T}) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:Stabilizer{T}} , x::MixedStabilizer{T}) where {T<:Tableau} = Stabilizer(tab(x)) +Base.convert(::Type{<:MixedDestabilizer{T}} , x::Stabilizer{T}) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:MixedDestabilizer{T}} , x::Destabilizer{T}) where {T<:Tableau} = MixedDestabilizer(x) ############################## # Helpers for binary codes From be8ce6fa6a791c10e2d4166428a393c166854428 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sun, 15 Sep 2024 03:37:05 +0500 Subject: [PATCH 4/9] minor formatting --- src/QuantumClifford.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index 96833ff69..db41792c2 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -993,13 +993,13 @@ end # Conversion and promotion ############################## -Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:Stabilizer{T}}) where {T<:Tableau} = Stabilizer{T} -Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} +Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = Stabilizer{T} +Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Destabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} -Base.convert(::Type{<:Stabilizer{T}} , x::MixedStabilizer{T}) where {T<:Tableau} = Stabilizer(tab(x)) -Base.convert(::Type{<:MixedDestabilizer{T}} , x::Stabilizer{T}) where {T<:Tableau} = MixedDestabilizer(x) -Base.convert(::Type{<:MixedDestabilizer{T}} , x::Destabilizer{T}) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:Stabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = Stabilizer(tab(x)) +Base.convert(::Type{<:MixedDestabilizer{T}} , x::Stabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:MixedDestabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) ############################## # Helpers for binary codes From 066d7c2beed7160e0ac71063b37c48d6b4c33315 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sun, 15 Sep 2024 15:28:12 +0500 Subject: [PATCH 5/9] further polish --- src/QuantumClifford.jl | 24 ++++++++++----- src/linalg.jl | 70 +++++++++++++++++++++++++++++++++++++++--- test/test_stabs.jl | 12 +++++++- 3 files changed, 94 insertions(+), 12 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index db41792c2..71879ee0a 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -993,13 +993,23 @@ end # Conversion and promotion ############################## -Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = Stabilizer{T} -Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} -Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Destabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} - -Base.convert(::Type{<:Stabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = Stabilizer(tab(x)) -Base.convert(::Type{<:MixedDestabilizer{T}} , x::Stabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) -Base.convert(::Type{<:MixedDestabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) +Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = Stabilizer{T} +Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:MixedStabilizer{T}}) where {T<:Tableau} = Stabilizer{T} +Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} +Base.promote_rule(::Type{<:Stabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} +Base.promote_rule(::Type{<:Destabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} +Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} +Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} +Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:MixedStabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} + +Base.convert(::Type{<:Stabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = Stabilizer(tab(x)) +Base.convert(::Type{<:MixedStabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = Stabilizer(tab(x)) +Base.convert(::Type{<:Stabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:Destabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:MixedDestabilizer{T}} , x::Stabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:MixedDestabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:Destabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(Stabilizer(tab(x))) +Base.convert(::Type{<:MixedDestabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(Stabilizer(tab(x))) ############################## # Helpers for binary codes diff --git a/src/linalg.jl b/src/linalg.jl index 4a012e969..d9b1ec0f1 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -158,7 +158,7 @@ julia> tensor(s, s) - ____Z_ ``` -Tensor product between [`MixedDestabilizer`](@ref) and [`Stabilizer`](@ref). +Tensor product between [`MixedDestabilizer`](@ref) and [`Stabilizer`](@ref): ```jldoctest promote julia> md = MixedDestabilizer(T"Z//X", 1) @@ -176,7 +176,7 @@ julia> tensor(S"X", md) + _X ``` -Tensor product between [`MixedDestabilizer`](@ref) and [`Destabilizer`](@ref). +Tensor product between [`MixedDestabilizer`](@ref) and [`Destabilizer`](@ref): ```jldoctest promote julia> d = Destabilizer(T"-Y//-X") @@ -194,9 +194,9 @@ julia> tensor(d, md) + _X ``` -Tensor product between [`MixedStabilizer`](@ref) and [`Stabilizer`](@ref). +Tensor product between [`MixedStabilizer`](@ref) and [`Stabilizer`](@ref): -```jldoctest +```jldoctest promote julia> ms = MixedStabilizer(S"-XZ//-ZX") - XZ - ZX @@ -207,6 +207,68 @@ julia> tensor(S"X", ms) - _ZX ``` +Tensor product between [`Stabilizer`](@ref) and [`Destabilizer`](@ref): + +```jldoctest promote +julia> tensor(S"X", d) +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท ++ Z_ +- _Y +๐’ฎ๐“‰๐’ถ๐’ท ++ X_ +- _X +``` + +Tensor product between [`MixedStabilizer`](@ref) and [`MixedDestabilizer`](@ref): + +```jldoctest promote +julia> tensor(md, ms) +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท ++ Z__ ++ _Z_ ++ __Z +๐’ฎ๐“‰๐’ถ๐’ทโ” ++ X__ +- _XZ +- _ZX +``` + +Tensor product between [`MixedStabilizer`](@ref) and [`Destabilizer`](@ref): + +```jldoctest promote +julia> tensor(d, ms) +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท +- Y__ ++ _Z_ ++ __Z +๐’ฎ๐“‰๐’ถ๐’ทโ” +- X__ +- _XZ +- _ZX +``` + +Tensor product between [`Destabilizer`](@ref) and [`Destabilizer`](@ref): + +```jldoctest promote +julia> tensor(d, d) +๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท +- Y_ +- _Y +๐’ฎ๐“‰๐’ถ๐’ท +- X_ +- _X +``` + +Tensor product between [`MixedStabilizer`](@ref) and [`MixedStabilizer`](@ref): + +```jldoctest promote +julia> tensor(ms, ms) +- XZ__ +- ZX__ +- __XZ +- __ZX +``` + See also [`tensor_pow`](@ref).""" function tensor end diff --git a/test/test_stabs.jl b/test/test_stabs.jl index 548e92d1c..0aca351bc 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -61,12 +61,22 @@ @test mixed_destab_looks_good(mds) estab = stabilizerview(md)โŠ—s @test canonicalize!(copy(stabilizerview(mds))) == canonicalize!(estab) - d = random_stabilizer(n) + d = random_destabilizer(n) dmd = dโŠ—md @test mixed_destab_looks_good(dmd) ms = MixedStabilizer(random_stabilizer(n)) mss = sโŠ—ms @test mixed_stab_looks_good(mss) + sd = random_stabilizer(n)โŠ—random_destabilizer(n) + @test mixed_destab_looks_good(sd) + mdms = mdโŠ—ms + @test mixed_destab_looks_good(mdms) + dms = dโŠ—ms + @test mixed_destab_looks_good(dms) + dd = dโŠ—d + @test destab_looks_good(dd) + msms = msโŠ—ms + @test mixed_stab_looks_good(msms) end end From a30997b1ac25db97c67b0ca905bb1f835ae9b4c5 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sun, 15 Sep 2024 22:58:28 +0500 Subject: [PATCH 6/9] remove nonsense code --- src/QuantumClifford.jl | 6 ---- src/linalg.jl | 75 ------------------------------------------ test/test_stabs.jl | 9 ----- 3 files changed, 90 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index 71879ee0a..b29c06945 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -993,23 +993,17 @@ end # Conversion and promotion ############################## -Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = Stabilizer{T} Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:MixedStabilizer{T}}) where {T<:Tableau} = Stabilizer{T} -Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} Base.promote_rule(::Type{<:Stabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} Base.promote_rule(::Type{<:Destabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} -Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:MixedStabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} -Base.convert(::Type{<:Stabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = Stabilizer(tab(x)) Base.convert(::Type{<:MixedStabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = Stabilizer(tab(x)) Base.convert(::Type{<:Stabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) Base.convert(::Type{<:Destabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) Base.convert(::Type{<:MixedDestabilizer{T}} , x::Stabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) Base.convert(::Type{<:MixedDestabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) -Base.convert(::Type{<:Destabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(Stabilizer(tab(x))) -Base.convert(::Type{<:MixedDestabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(Stabilizer(tab(x))) ############################## # Helpers for binary codes diff --git a/src/linalg.jl b/src/linalg.jl index d9b1ec0f1..638c8583c 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -194,81 +194,6 @@ julia> tensor(d, md) + _X ``` -Tensor product between [`MixedStabilizer`](@ref) and [`Stabilizer`](@ref): - -```jldoctest promote -julia> ms = MixedStabilizer(S"-XZ//-ZX") -- XZ -- ZX - -julia> tensor(S"X", ms) -+ X__ -- _XZ -- _ZX -``` - -Tensor product between [`Stabilizer`](@ref) and [`Destabilizer`](@ref): - -```jldoctest promote -julia> tensor(S"X", d) -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -+ Z_ -- _Y -๐’ฎ๐“‰๐’ถ๐’ท -+ X_ -- _X -``` - -Tensor product between [`MixedStabilizer`](@ref) and [`MixedDestabilizer`](@ref): - -```jldoctest promote -julia> tensor(md, ms) -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -+ Z__ -+ _Z_ -+ __Z -๐’ฎ๐“‰๐’ถ๐’ทโ” -+ X__ -- _XZ -- _ZX -``` - -Tensor product between [`MixedStabilizer`](@ref) and [`Destabilizer`](@ref): - -```jldoctest promote -julia> tensor(d, ms) -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -- Y__ -+ _Z_ -+ __Z -๐’ฎ๐“‰๐’ถ๐’ทโ” -- X__ -- _XZ -- _ZX -``` - -Tensor product between [`Destabilizer`](@ref) and [`Destabilizer`](@ref): - -```jldoctest promote -julia> tensor(d, d) -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -- Y_ -- _Y -๐’ฎ๐“‰๐’ถ๐’ท -- X_ -- _X -``` - -Tensor product between [`MixedStabilizer`](@ref) and [`MixedStabilizer`](@ref): - -```jldoctest promote -julia> tensor(ms, ms) -- XZ__ -- ZX__ -- __XZ -- __ZX -``` - See also [`tensor_pow`](@ref).""" function tensor end diff --git a/test/test_stabs.jl b/test/test_stabs.jl index 0aca351bc..e5730ca66 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -64,19 +64,10 @@ d = random_destabilizer(n) dmd = dโŠ—md @test mixed_destab_looks_good(dmd) - ms = MixedStabilizer(random_stabilizer(n)) - mss = sโŠ—ms - @test mixed_stab_looks_good(mss) sd = random_stabilizer(n)โŠ—random_destabilizer(n) @test mixed_destab_looks_good(sd) - mdms = mdโŠ—ms - @test mixed_destab_looks_good(mdms) - dms = dโŠ—ms - @test mixed_destab_looks_good(dms) dd = dโŠ—d @test destab_looks_good(dd) - msms = msโŠ—ms - @test mixed_stab_looks_good(msms) end end From cc5b84d74f2705a3f98fb04e41009bf1467e0d14 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Mon, 16 Sep 2024 07:28:00 +0500 Subject: [PATCH 7/9] add code review suggestions: cut down on power_rules, simplify conversions --- src/QuantumClifford.jl | 9 +-------- src/linalg.jl | 18 ------------------ test/test_stabs.jl | 4 ---- 3 files changed, 1 insertion(+), 30 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index b29c06945..e7f083981 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -993,17 +993,10 @@ end # Conversion and promotion ############################## -Base.promote_rule(::Type{<:MixedStabilizer{T}} , ::Type{<:MixedStabilizer{T}}) where {T<:Tableau} = Stabilizer{T} Base.promote_rule(::Type{<:Stabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} -Base.promote_rule(::Type{<:Destabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} -Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} -Base.convert(::Type{<:MixedStabilizer{T}} , x::MixedStabilizer{T} ) where {T<:Tableau} = Stabilizer(tab(x)) -Base.convert(::Type{<:Stabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) -Base.convert(::Type{<:Destabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) -Base.convert(::Type{<:MixedDestabilizer{T}} , x::Stabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) -Base.convert(::Type{<:MixedDestabilizer{T}} , x::Destabilizer{T} ) where {T<:Tableau} = MixedDestabilizer(x) +Base.convert(::Type{<:MixedDestabilizer{T}}, x::Union{Destabilizer{T}, Stabilizer{T}}) where {T <: Tableau} = MixedDestabilizer(x) ############################## # Helpers for binary codes diff --git a/src/linalg.jl b/src/linalg.jl index 638c8583c..b71204209 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -176,24 +176,6 @@ julia> tensor(S"X", md) + _X ``` -Tensor product between [`MixedDestabilizer`](@ref) and [`Destabilizer`](@ref): - -```jldoctest promote -julia> d = Destabilizer(T"-Y//-X") -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -- Y -๐’ฎ๐“‰๐’ถ๐’ท -- X - -julia> tensor(d, md) -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -- Y_ -+ _Z -๐’ฎ๐“‰๐’ถ๐’ท -- X_ -+ _X -``` - See also [`tensor_pow`](@ref).""" function tensor end diff --git a/test/test_stabs.jl b/test/test_stabs.jl index e5730ca66..1324c22d3 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -62,12 +62,8 @@ estab = stabilizerview(md)โŠ—s @test canonicalize!(copy(stabilizerview(mds))) == canonicalize!(estab) d = random_destabilizer(n) - dmd = dโŠ—md - @test mixed_destab_looks_good(dmd) sd = random_stabilizer(n)โŠ—random_destabilizer(n) @test mixed_destab_looks_good(sd) - dd = dโŠ—d - @test destab_looks_good(dd) end end From 11376fd1cb31b9a79c6fbae5e634c6002c6f659e Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Tue, 17 Sep 2024 14:07:13 +0500 Subject: [PATCH 8/9] revamp promote rules --- src/QuantumClifford.jl | 12 ++++++++---- test/test_stabs.jl | 3 --- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index e7f083981..f1b3b6aa6 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -993,10 +993,14 @@ end # Conversion and promotion ############################## -Base.promote_rule(::Type{<:Stabilizer{T}} , ::Type{<:Destabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} -Base.promote_rule(::Type{<:MixedDestabilizer{T}}, ::Type{<:Stabilizer{T}} ) where {T<:Tableau} = MixedDestabilizer{T} - -Base.convert(::Type{<:MixedDestabilizer{T}}, x::Union{Destabilizer{T}, Stabilizer{T}}) where {T <: Tableau} = MixedDestabilizer(x) +# Destabilizer, MixedDestabilizer -> MixedDestabilizer +Base.promote_rule(::Type{<:Destabilizer{T}} , ::Type{<:MixedDestabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} +# MixedStabilizer, MixedDestabilizer -> MixedDestabilizer +Base.promote_rule(::Type{<:MixedStabilizer{T}}, ::Type{<:MixedDestabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} +# Stabilizer, T<:Union{MixedStabilizer, Destabilizer, 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 diff --git a/test/test_stabs.jl b/test/test_stabs.jl index 1324c22d3..d415621ea 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -61,9 +61,6 @@ @test mixed_destab_looks_good(mds) estab = stabilizerview(md)โŠ—s @test canonicalize!(copy(stabilizerview(mds))) == canonicalize!(estab) - d = random_destabilizer(n) - sd = random_stabilizer(n)โŠ—random_destabilizer(n) - @test mixed_destab_looks_good(sd) end end From 99ab972bcce83c59a31583d72d25e6f51b876529 Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Thu, 26 Sep 2024 23:21:57 -0400 Subject: [PATCH 9/9] remove redundant comments and add back an incorrectly removed TODO --- src/QuantumClifford.jl | 3 --- src/linalg.jl | 20 +------------------- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index f1b3b6aa6..459367293 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -993,11 +993,8 @@ end # Conversion and promotion ############################## -# Destabilizer, MixedDestabilizer -> MixedDestabilizer Base.promote_rule(::Type{<:Destabilizer{T}} , ::Type{<:MixedDestabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} -# MixedStabilizer, MixedDestabilizer -> MixedDestabilizer Base.promote_rule(::Type{<:MixedStabilizer{T}}, ::Type{<:MixedDestabilizer{T}}) where {T<:Tableau} = MixedDestabilizer{T} -# Stabilizer, T<:Union{MixedStabilizer, Destabilizer, 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) diff --git a/src/linalg.jl b/src/linalg.jl index b71204209..b95fba888 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -158,28 +158,10 @@ julia> tensor(s, s) - ____Z_ ``` -Tensor product between [`MixedDestabilizer`](@ref) and [`Stabilizer`](@ref): - -```jldoctest promote -julia> md = MixedDestabilizer(T"Z//X", 1) -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -+ Z -๐’ฎ๐“‰๐’ถ๐’ท -+ X - -julia> tensor(S"X", md) -๐’Ÿโ„ฏ๐“ˆ๐“‰๐’ถ๐’ท -+ Z_ -+ _Z -๐’ฎ๐“‰๐’ถ๐’ท -+ X_ -+ _X -``` - See also [`tensor_pow`](@ref).""" function tensor end -function tensor(ops::AbstractStabilizer...) +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)