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

Horizontal concatenation (hcat) for Tableaux and Stabilizers #304

Merged
merged 16 commits into from
Sep 27, 2024
Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

# News

## v0.9.11-dev

- `hcat` of Tableaux objects

## v0.9.10 - 2024-09-26

- The lifted product class of quantum LDPC codes is implemented in the ECC submodule.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumClifford"
uuid = "0525e862-1e90-11e9-3e4d-1b39d7109de1"
authors = ["Stefan Krastanov <stefan@krastanov.org> and QuantumSavory community members"]
version = "0.9.10"
version = "0.9.11-dev"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down
49 changes: 48 additions & 1 deletion src/QuantumClifford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -935,14 +935,61 @@ function check_allrowscommute(stabilizer::Tableau)
end
check_allrowscommute(stabilizer::Stabilizer)=check_allrowscommute(tab(stabilizer))

"""
Vertically concatenates tableaux.

```jldoctest
julia> vcat(ghz(2), ghz(2))
+ XX
+ ZZ
+ XX
+ ZZ
```

See also: [`hcat`](@ref)
"""
function Base.vcat(tabs::Tableau...)
Tableau(
vcat((s.phases for s in tabs)...),
tabs[1].nqubits,
hcat((s.xzs for s in tabs)...))
end

Base.vcat(stabs::Stabilizer...) = Stabilizer(vcat((tab(s) for s in stabs)...))
Base.vcat(stabs::Stabilizer{T}...) where {T} = Stabilizer(vcat((tab(s) for s in stabs)...))

"""
Horizontally concatenates tableaux.

```jldoctest
julia> hcat(ghz(2), ghz(2))
+ XXXX
+ ZZZZ
```

See also: [`vcat`](@ref)
"""
function Base.hcat(tabs::Tableau...) # TODO this implementation is slow as it unpacks each bitvector into bits and repacks them -- reuse the various tableau inset functionality we have to speed this up
rows = size(tabs[1], 1)
cols = sum(map(nqubits, 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
for j in 1:cols_tab
newtab[i, cols_idx+j-1]::Tuple{Bool,Bool} = tab[i, j]::Tuple{Bool,Bool}
end
newtab.phases[i] = (newtab.phases[i]+tab.phases[i])%4
end
cols_idx += cols_tab
end
return newtab
end

Base.hcat(stabs::Stabilizer{T}...) where {T} = Stabilizer(hcat((tab(s) for s in stabs)...))

##############################
# Unitary Clifford Operations
Expand Down
2 changes: 1 addition & 1 deletion test/test_jet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@

@show rep
@test_broken length(JET.get_reports(rep)) == 0
@test length(JET.get_reports(rep)) <= 11
@test length(JET.get_reports(rep)) <= 10
end
11 changes: 11 additions & 0 deletions test/test_stabs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,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
Loading