@@ -103,54 +103,58 @@ function paulibasis2majoranasquares(P::AbstractVector, locs=1:Int(log(4, length(
103103
104104 areconsecutive (locs) || throw (NonFLOException (" P contains terms that are not the product of two Majoranas" ))
105105
106+
107+ threshold = √ eps (real (eltype (P)))
108+ nonzero_indices = filter (0x1 : UInt32 (4 ^ n- 1 )) do i
109+ abs (P[i+ 1 ]) >= threshold
110+ end
111+
106112 out = zeros (real (eltype (P)), 2 n, 2 n)
107- for i in 0x1 : UInt32 ( 4 ^ n - 1 )
113+ for i in nonzero_indices
108114 coeff = P[i+ 1 ]
109- if abs (coeff) >= √ eps (eltype (out)) # really only look at non-zero coeffs
110- firstmajorana = - 1 # these will index into the 2n×2n matrix holding the majorana coeffs
111- secondmajorana = - 1
112- for k in n- 1 : - 1 : 0
113- opi = i % 4 # i gets shifted 2 bits to the right in every iteration
114- if secondmajorana == - 1
115- if opi == 1 # so we got an X operator
116- secondmajorana = 2 k # so the 2nd majoran op is of the 1st type
117- @debug " second operator is γ1 on site $k "
118- elseif opi == 2 # so we got an Y operator
119- secondmajorana = 2 k+ 1 # so the 2nd majorana op is of the 2nd type
120- coeff *= - 1
121- @debug " second operator is γ2 on site $k "
122- elseif opi == 3 # so we got a Z operator
123- firstmajorana = 2 k+ 1 # so both majoranas act on the site
124- secondmajorana = 2 k
125- @debug " second operator is γ2 on site $k "
126- @debug " first operator is γ1 on site $k "
127- else
128- @debug " nothing on site $k "
129- end
130- elseif firstmajorana == - 1
131- if opi == 1
132- firstmajorana = 2 k+ 1
133- @debug " first operator is γ2 on site $k "
134- elseif opi == 2
135- firstmajorana = 2 k
136- @debug " first operator is γ1 on site $k "
137- elseif opi == 3
138- coeff *= - 1
139- @debug " nothing on site $k "
140- else
141- throw (NonFLOException (" P contains terms that are not the product of two Majoranas" ))
142- end
143- elseif opi != 0
144- throw (NonFLOException (" P contains terms that are not the product of two Majoranas" ))
115+ firstmajorana = - 1 # these will index into the 2n×2n matrix holding the majorana coeffs
116+ secondmajorana = - 1
117+ for k in n- 1 : - 1 : 0
118+ opi = i % 4 # i gets shifted 2 bits to the right in every iteration
119+ if secondmajorana == - 1
120+ if opi == 1 # so we got an X operator
121+ secondmajorana = 2 k # so the 2nd majorana op is of the 1st type
122+ @debug " second operator is γ1 on site $k "
123+ elseif opi == 2 # so we got an Y operator
124+ secondmajorana = 2 k+ 1 # so the 2nd majorana op is of the 2nd type
125+ coeff *= - 1
126+ @debug " second operator is γ2 on site $k "
127+ elseif opi == 3 # so we got a Z operator
128+ firstmajorana = 2 k+ 1 # so both majoranas act on the site
129+ secondmajorana = 2 k
130+ @debug " second operator is γ2 on site $k "
131+ @debug " first operator is γ1 on site $k "
145132 else
146133 @debug " nothing on site $k "
147134 end
148- i = i >> 2
135+ elseif firstmajorana == - 1
136+ if opi == 1
137+ firstmajorana = 2 k+ 1
138+ @debug " first operator is γ2 on site $k "
139+ elseif opi == 2
140+ firstmajorana = 2 k
141+ @debug " first operator is γ1 on site $k "
142+ elseif opi == 3
143+ coeff *= - 1
144+ @debug " nothing on site $k "
145+ else
146+ throw (NonFLOException (" P contains terms that are not the product of two Majoranas" ))
147+ end
148+ elseif opi != 0
149+ throw (NonFLOException (" P contains terms that are not the product of two Majoranas" ))
150+ else
151+ @debug " nothing on site $k "
149152 end
150- firstmajorana == - 1 && throw (NonFLOException (" P contains terms that are not the product of two Majoranas" ))
151- out[firstmajorana+ 1 , secondmajorana+ 1 ] = - 2 * real (coeff)
152- out[secondmajorana+ 1 , firstmajorana+ 1 ] = 2 * real (coeff)
153+ i = i >> 2
153154 end
155+ firstmajorana == - 1 && throw (NonFLOException (" P contains terms that are not the product of two Majoranas" ))
156+ out[firstmajorana+ 1 , secondmajorana+ 1 ] = - 2 * real (coeff)
157+ out[secondmajorana+ 1 , firstmajorana+ 1 ] = 2 * real (coeff)
154158 end
155159 return out
156160end
0 commit comments