From e1bb8b9bfef36b3903e0d8d228cea33823893874 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sun, 22 Sep 2024 09:48:48 +0500 Subject: [PATCH 1/5] fix #191 - too restrictive typeassert for MixedDestabilizer --- src/QuantumClifford.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index 407af5959..29114bdbc 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -569,8 +569,13 @@ function MixedDestabilizer(stab::Stabilizer{T}; undoperm=true, reportperm=false) t[n+r+s+1:end] = sZ # The other logical set in the tableau end if undoperm - t = t[:,invperm(permx[permz])]::T - return MixedDestabilizer(t, r+s)::MixedDestabilizer{T} + if isa(t, SubArray) + t = t[:, invperm(permx[permz])]::T + return MixedDestabilizer(t, r+s)::MixedDestabilizer{T} + else + t = t[:, invperm(permx[permz])] + return MixedDestabilizer(t, r+s) + end end if reportperm return (MixedDestabilizer(t, r+s)::MixedDestabilizer{T}, r, permx, permz) From 0a13fa74271930ede782d744f64a67d3a14af003 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Mon, 28 Oct 2024 11:08:52 +0500 Subject: [PATCH 2/5] add a comment explaining the change --- src/QuantumClifford.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index 29114bdbc..5abf0be7f 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -569,6 +569,7 @@ function MixedDestabilizer(stab::Stabilizer{T}; undoperm=true, reportperm=false) t[n+r+s+1:end] = sZ # The other logical set in the tableau end if undoperm + # Handle SubArray separately to ensure the correct type is maintained after applying the inverse permutation. if isa(t, SubArray) t = t[:, invperm(permx[permz])]::T return MixedDestabilizer(t, r+s)::MixedDestabilizer{T} From d49f5b164a53b2320ad5e1561626d1075c255db4 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Mon, 28 Oct 2024 12:36:40 +0500 Subject: [PATCH 3/5] add tests that would have failed withoutthis change --- test/test_stabs.jl | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/test_stabs.jl b/test/test_stabs.jl index 63a5c562e..2cada11b6 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -90,4 +90,25 @@ @test stab_to_gf2(s2a) == stab_to_gf2(s2b) end end -end + + @testset "MixedDestabilizer: Regular Array vs Subarray Using Tableau" begin + # Case 1: QuantumClifford.Tableau{Vector{UInt8}, Matrix{UInt64}} + for _ in 1:5 + n = rand(2:6) + stab = random_stabilizer(n) + regular_arr = MixedDestabilizer(stab; undoperm=true) + @test isa(regular_arr, MixedDestabilizer) + end + + # Case 2: Tableau{SubArray{...}, SubArray{...}, Tuple{Base.Slice{...}}} + for _ in 1:5 + n = rand(2:6) + stab = random_stabilizer(n) + start_idx = rand(1:n) + end_idx = rand(start_idx:n) + t_subarr = @view stab[start_idx:end_idx] + md_via_subarr = MixedDestabilizer(Stabilizer(t_subarr); undoperm=true) + @test isa(md_via_subarr, MixedDestabilizer) + end + end +end \ No newline at end of file From 81ae47f458c26e8a8bac2e67ed54415adca7483b Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Sat, 2 Nov 2024 11:04:36 -0400 Subject: [PATCH 4/5] let's try a much simpler solution -- maybe JET and the compiler have improved since this was necessary; also simplified the tests --- src/QuantumClifford.jl | 10 ++-------- test/test_stabs.jl | 27 ++++++++++----------------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index 6a1a066fd..859b00d9a 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -571,14 +571,8 @@ function MixedDestabilizer(stab::Stabilizer{T}; undoperm=true, reportperm=false) t[n+r+s+1:end] = sZ # The other logical set in the tableau end if undoperm - # Handle SubArray separately to ensure the correct type is maintained after applying the inverse permutation. - if isa(t, SubArray) - t = t[:, invperm(permx[permz])]::T - return MixedDestabilizer(t, r+s)::MixedDestabilizer{T} - else - t = t[:, invperm(permx[permz])] - return MixedDestabilizer(t, r+s) - end + t = t[:,invperm(permx[permz])] + return MixedDestabilizer(t, r+s) end if reportperm return (MixedDestabilizer(t, r+s)::MixedDestabilizer{T}, r, permx, permz) diff --git a/test/test_stabs.jl b/test/test_stabs.jl index 4a3e809e3..a26606b0d 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -1,4 +1,5 @@ @testitem "Stabilizers" begin + using QuantumClifford using QuantumClifford: stab_looks_good, destab_looks_good, mixed_stab_looks_good, mixed_destab_looks_good test_sizes = [1,2,10,63,64,65,127,128,129] # Including sizes that would test off-by-one errors in the bit encoding. @testset "Pure and Mixed state initialization" begin @@ -108,24 +109,16 @@ @test hcat(copy(tab(s1)), copy(tab(s2)), copy(tab(s1)), copy(tab(s2))) == T"YZZYYZZY XXYXXXYX" end - @testset "MixedDestabilizer via Stabilizer{Tableau}: Regular Array vs. Subarray in Tableau Structure" begin + @testset "MixedDestabilizer over subarrays (#191)" begin # Case 1: QuantumClifford.Tableau{Vector{UInt8}, Matrix{UInt64}} - for _ in 1:5 - n = rand(2:6) - stab = random_stabilizer(n) - regular_arr = MixedDestabilizer(stab; undoperm=true) - @test isa(regular_arr, MixedDestabilizer) - end - + n = 6 + stab = random_stabilizer(n) + regular_arr = MixedDestabilizer(stab; undoperm=true) + @test isa(regular_arr, MixedDestabilizer) # Case 2: Tableau{SubArray{...}, SubArray{...}, Tuple{Base.Slice{...}}} - for _ in 1:5 - n = rand(2:6) - stab = random_stabilizer(n) - start_idx = rand(1:n) - end_idx = rand(start_idx:n) - t_subarr = @view stab[start_idx:end_idx] - md_via_subarr = MixedDestabilizer(Stabilizer(t_subarr); undoperm=true) - @test isa(md_via_subarr, MixedDestabilizer) - end + stab = random_stabilizer(n) + substab = @view stab[3:end_idx] + md_via_subarr = MixedDestabilizer(substab; undoperm=true) + @test isa(md_via_subarr, MixedDestabilizer) end end From 0122d7297f264eeb19952715f3d7192c0ae9c6d3 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sat, 2 Nov 2024 20:34:34 +0500 Subject: [PATCH 5/5] remove end_idx --- test/test_stabs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_stabs.jl b/test/test_stabs.jl index a26606b0d..d6b9041de 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -117,7 +117,7 @@ @test isa(regular_arr, MixedDestabilizer) # Case 2: Tableau{SubArray{...}, SubArray{...}, Tuple{Base.Slice{...}}} stab = random_stabilizer(n) - substab = @view stab[3:end_idx] + substab = @view stab[3:n] md_via_subarr = MixedDestabilizer(substab; undoperm=true) @test isa(md_via_subarr, MixedDestabilizer) end