diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index 6eb4d6ede..29d17a739 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -926,15 +926,24 @@ end Base.vcat(stabs::Stabilizer...) = Stabilizer(vcat((tab(s) for s in stabs)...)) function Base.hcat(tabs::Tableau...) - # Ensure all Tableaus have the same number of rows - nqubits = tabs[1].nqubits - if !all(t -> t.nqubits == nqubits, tabs) - throw(ArgumentError("All Tableaus must have the same number of qubits for horizontal concatenation.")) + rows = size(tabs[1], 1) + cols = sum(map(tab -> size(tab, 2), tabs)) + newtab = zero(Tableau, rows, cols) + cols_idx = 1 + for tab in tabs + rows_tab, cols_tab = size(tab) + if rows_tab != rows + throw(ArgumentError("All input Tableaux/Stabilizers must have the same number of rows.")) + end + for i in 1:rows + newtab.phases[i] |= tab.phases[i] + for j in 1:cols_tab + newtab[i, cols_idx+j-1] = tab[i, j] + end + end + cols_idx += cols_tab end - Tableau( - tabs[1].phases, #does not make sense, better method needed for phases concatenation - sum(s.nqubits for s in tabs), - hcat((s.xzs for s in tabs)...)) + return newtab end Base.hcat(stabs::Stabilizer...) = Stabilizer(hcat((tab(s) for s in stabs)...)) diff --git a/test/test_stabs.jl b/test/test_stabs.jl index 63a5c562e..a697c0899 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -90,4 +90,15 @@ @test stab_to_gf2(s2a) == stab_to_gf2(s2b) end end + + @testset "horizontal concatenation" begin + @test hcat(ghz(2), ghz(2)) == S"XXXX ZZZZ" + s1 = S"YZ -XX" + s2 = S"-ZY -YX" + @test hcat(copy(s1), copy(s2)) == S"-YZZY -XXYX" + @test hcat(copy(s1), copy(s2), copy(s1), copy(s2)) == S"-YZZYYZZY -XXYXXXYX" + @test_throws ArgumentError hcat(copy(s1), random_stabilizer(3)) + @test hcat(copy(tab(s1)), copy(tab(s2))) == T"-YZZY -XXYX" + @test hcat(copy(tab(s1)), copy(tab(s2)), copy(tab(s1)), copy(tab(s2))) == T"-YZZYYZZY -XXYXXXYX" + end end