QuantumSavory / QuantumClifford.jl

Clifford circuits, graph states, and other quantum Stabilizer formalism tools.
MIT License
103 stars 42 forks source link

Correct set_index(Stabilizer_Tableau, rows, cols) #287

Closed Fe-r-oz closed 2 weeks ago

Fe-r-oz commented 3 weeks ago

Suppose

julia> H
+ X_X_ZYZY
+ X_YZX_YZ
+ XZ_Y_YXZ

I want to apply Hadamard Rotation operaton to each element in each row, H*X

julia> for rows in 1:3
                 for cols in 1:2^3
                       setindex!(H, rows, cols) = sHadamard(1)*H[[rows], [cols]]                                                                                                
                  end 
          end     

I get wrong output, no change!
julia> H
+ X_X_ZYZY
+ X_YZX_YZ
+ XZ_Y_YXZ

Expected Output

 Z_Z_XYXY
Z_YXZ_YX
ZX_Y_YZX

There seems to be a problem with setindex!Because H operation is applied correctly as shown in the output

julia> for rows in 1:3
              for cols in 1:2^3
                    println(sHadamard(1)*Hⱼ[[rows], [cols]] )                                                                                                                                                                                                                                                        
              end
           end
+ Z
+ _
+ Z
+ _
+ X
- Y
+ X
- Y
+ Z
+ _
- Y
+ X
+ Z
+ _
- Y
+ X
+ Z
+ X
+ _
- Y
+ _
- Y
+ Z
+ X

The problem either lies in the assignment ( setindex!(H, rows, cols))or in setindex! Please feel free to assign an better name to the issue.

Krastanov commented 2 weeks ago

I think there is some conceptual confusion here. A gate is supposed to be applied to the tableau (state) as a whole, not row-by-row. Thus I am not sure what setindex!(H, rows, cols) = sHadamard(1)*H[[rows], [cols]] is supposed to do.

If one wants to apply a gate, they should just do apply!(state, sHadamard(qubit_index)) (which is also non-allocating, unlike this loop).

If the Hadamard needs to be applied to all qubits, it would looks something like:

for q_i in 1:nqubits(state)
    apply!(state, sHadamard(q_i))
end

There is a bit of a misuse of julia syntax here, which is a separate problem. If you want to set an index, you should write:

H[rows, cols] = sHadamard(1)*H[[rows], [cols]]

Instead, you are writing

setindex!(H, rows, cols) = sHadamard(1)*H[[rows], [cols]]                                                                                                

which simply redefines the setindex! function. You are defining new methods, not calling a function with this syntax.

For the moment I will close this issue, but feel free to reopen it if something was unclear.

Fe-r-oz commented 2 weeks ago

Thanks a lot!