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

Conversation

Fe-r-oz
Copy link
Contributor

@Fe-r-oz Fe-r-oz commented Jul 1, 2024

For hcat, it seems we would need a something like this if we suppose that we have a s1 as 4x4 and s2 as 4x4 stabilizers

julia> hcat(s1, s2),
1-element Vector{Stabilizer{QuantumClifford.Tableau{Vector{UInt8}, Matrix{UInt64}}}}:
 Stabilizer 4×8

julia> vcat(s1, s2)
2-element Vector{Stabilizer{QuantumClifford.Tableau{Vector{UInt8}, Matrix{UInt64}}}}:
 Stabilizer 4×4
 Stabilizer 4×4

This will give method error was we are dealing with AbstractVectors

julia> hcat(s1, s2)
1×2 Matrix{Stabilizer{QuantumClifford.Tableau{Vector{UInt8}, Matrix{UInt64}}}}:
 Stabilizer 4×4  Stabilizer 4×4

I have added the if check that Tableus rows must be the same for horizontal concatenation.

julia> using QuantumClifford.ECC
julia> s1 = random_stabilizer(4)
+ X_XZ
+ X_ZX
- ZX_Y
+ _YXZ

julia> s2 = random_stabilizer(4)
+ _XYY
+ ZYXY
+ _YYX
- ZXYY

julia> vcat(s1,s2)
+ X_XZ
+ X_ZX
- ZX_Y
+ _YXZ
+ _XYY
+ ZYXY
+ _YYX
- ZXYY

I am trying to figure out what happends to the phase vector, In the vcat it is concatenated vertically which makes sense but in hcat, we don't want. We want the size of phase vector to remain the same but phases modified maybe


julia> hcat(s1,s2)
+ + becomes + ? X_XZ_XYY
+ + becomes + ? X_ZXZYXY
+ - becomes - ? ZX_Y_YYX
- + becomes - ? _YXZZXYY

At the moment, hcat is not performing as expected

julia> using QuantumClifford.ECC
julia> s1 = random_stabilizer(4)
- X_XZ
- _YYX
- Y__X
- ZYXY

julia> s2 = random_stabilizer(4)
+ ZXYZ
- XX_X
- ZYZ_
+ _XXX

julia> s2 = random_stabilizer(2)
+ XX
- YZ

julia> hcat(s1, s2)
ERROR: ArgumentError: All Tableaus must have the same number of qubits for horizontal concatenation.
Stacktrace:
 [1] hcat
   @ ~/GitHub/QuantumClifford/QuantumClifford.jl/src/QuantumClifford.jl:929 [inlined]
 [2] hcat(::Stabilizer{QuantumClifford.Tableau{Vector{UInt8}, Matrix{UInt64}}}, ::Stabilizer{QuantumClifford.Tableau{Vector{UInt8}, Matrix{UInt64}}})
   @ QuantumClifford ~/GitHub/QuantumClifford/QuantumClifford.jl/src/QuantumClifford.jl:937
 [3] top-level scope
   @ REPL[6]:1

julia> s2 = random_stabilizer(4)
+ X_ZY
- XYZ_
- X__Y
+ ZZZX

julia> hcat(s1, s2) gives me the s1
- X_XZ
- _YYX
- Y__X
- ZYXY

I have trouble understanding what's hcat xzs is doing inside the vcat function, If xzs are horizontal arrays then horizontally concatenating them form a matrix which makes sense. Since xzs is an AbstractMatrix and hcating them (not sure whats hcat is doing, maybe we are creating a vector of matrices by hcatting then we can perform vcat by wrapper of stabilizers for vcat?

function Base.vcat(tabs::Tableau...)
    Tableau(
        vcat((s.phases for s in tabs)...),
        tabs[1].nqubits,
        hcat((s.xzs for s in tabs)...)) ❓ 
end

Thanks for your help!

@Fe-r-oz
Copy link
Contributor Author

Fe-r-oz commented Jul 1, 2024

A very minor addition towards right track maybe, We need sum(s.nqubits for s in tabs), previouly I was doing tabs[1].nqubits,

julia> s1 = random_stabilizer(4)
+ XX_X
- ZXZY
+ ZYX_
+ YYZ_

julia> s2 = random_stabilizer(4)
- _XYX
+ ZZZX
+ YYYZ
- YZX_

julia> hcat(s1, s2) # maybe right track
+ XX_X____
- ZXZY____
+ ZYX_____
+ YYZ_____

Copy link

codecov bot commented Jul 1, 2024

Codecov Report

Attention: Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.

Project coverage is 82.77%. Comparing base (dddaedb) to head (f7e0a71).

Files Patch % Lines
src/QuantumClifford.jl 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #304      +/-   ##
==========================================
- Coverage   82.85%   82.77%   -0.08%     
==========================================
  Files          60       60              
  Lines        3971     3977       +6     
==========================================
+ Hits         3290     3292       +2     
- Misses        681      685       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@Krastanov Krastanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for starting this draft. I left a few quick comments that hopefully would be of help.

You know, but just a reminder, this also will need to have tests.

CHANGELOG.md Outdated Show resolved Hide resolved
src/QuantumClifford.jl Outdated Show resolved Hide resolved
src/QuantumClifford.jl Outdated Show resolved Hide resolved
@Fe-r-oz Fe-r-oz changed the title Horizontal concatenation for Tableaus Horizontal concatenation for Tableaux Jul 1, 2024
@Fe-r-oz Fe-r-oz changed the title Horizontal concatenation for Tableaux Horizontal concatenation (hcat) for Tableaux and Stabilizers Sep 1, 2024
@Fe-r-oz Fe-r-oz marked this pull request as ready for review September 1, 2024 09:20
@Fe-r-oz
Copy link
Contributor Author

Fe-r-oz commented Sep 1, 2024

I left a few quick comments that hopefully would be of help.

Indeed.

I have added tests, and doctests as well.

hcat

julia> using QuantumClifford

julia> s1 = random_stabilizer(4)
+ Z_YX
- ZZ_X
+ XZ_Z
- _XXY

julia> s2 = random_stabilizer(4)
+ Z__X
- Z_XX
+ ZZX_
+ XX_Y

julia> hcat(s1, s2)
+ Z_YXZ__X
- ZZ_XZ_XX
+ XZ_ZZZX_
- _XXYXX_Y

julia> hcat(s1, s2, s1, s2)
+ Z_YXZ__XZ_YXZ__X
- ZZ_XZ_XXZZ_XZ_XX
+ XZ_ZZZX_XZ_ZZZX_
- _XXYXX_Y_XXYXX_Y

@Fe-r-oz Fe-r-oz requested a review from Krastanov September 1, 2024 11:44
Copy link
Member

@Krastanov Krastanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it is a nice quality of life improvement. I left in a few minor comments.

src/QuantumClifford.jl Outdated Show resolved Hide resolved
src/QuantumClifford.jl Outdated Show resolved Hide resolved
src/QuantumClifford.jl Outdated Show resolved Hide resolved
src/QuantumClifford.jl Outdated Show resolved Hide resolved
@Fe-r-oz
Copy link
Contributor Author

Fe-r-oz commented Sep 14, 2024

Thank you, all of your comments have been incorporated. Also, fixed the JET errors for vcat and hcat.

The julia 1 - t=1 mac-os-latest test error is not related to this PR. I think the PR is ready for review.

@Fe-r-oz Fe-r-oz requested a review from Krastanov September 14, 2024 11:43
Copy link
Member

@Krastanov Krastanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! I added a TODO about some inefficiencies in this implementation, but they can be addressed separately after this is merged.

I will merge it tomorrow after tests pass, etc.

@Krastanov Krastanov merged commit a9af4b0 into QuantumSavory:master Sep 27, 2024
15 of 16 checks passed
@Fe-r-oz Fe-r-oz deleted the hcat branch September 27, 2024 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants