Commit b69bbbb
authored
Fix bogoliubov_transform for spin-preserving Gaussian transformations (#1360)
Fixes #776.
`bogoliubov_transform` checks whether the transformation matrix is
spin-block-diagonal before it ever looks at the matrix shape, and that
check only makes sense for a square N x N matrix. For the N x 2N matrix
you get out of a general (non-particle-conserving) Bogoliubov
transformation it ends up inspecting the wrong blocks, decides that a
transform which doesn't actually mix spin is "block diagonal", and then
slices it into pieces of the wrong shape. The matrix from the issue dies
with:
```
ValueError: Bad shape for transformation_matrix. Expected (1, 1) or (1, 2) but got (1, 3).
```
### What I changed
I moved the block detection into a helper, `_spin_blocks`, that handles
both shapes. For the N x 2N case, write the matrix as `W = (W1 W2)` (it
acts on all the creation operators followed by all the annihilation
operators). The transform leaves the two spin sectors alone exactly when
both `W1` and `W2` are block diagonal, and in that case each sector is
its own (N/2) x N Gaussian transform built from the matching diagonal
blocks of `W1` and `W2`. The recursion that was already there handles
the rest. As a bonus, for spin-symmetric systems (BCS-style pairing
Hamiltonians, say) you now do two half-size Givens decompositions
instead of one full-size one.
### The part that took some digging
I didn't want to call this correct based on eigenstate energies alone,
so I checked the circuit at the operator level: does it actually give `U
a_p† U⁻¹ = b_p†` for every mode? That caught something the energy checks
can't.
Under Jordan-Wigner a spin-down ladder operator drags a Z string across
all of the spin-up qubits, so it anticommutes with the spin-up parity
operator. If the spin-up circuit flips spin-up parity (which happens
whenever its Gaussian decomposition uses an odd number of particle-hole
transforms), then splitting the sectors apart flips the sign of every
spin-down operator. What makes it sneaky is that it's invisible to
energy tests: starting from a fixed occupation state, that bad sign is
just a global phase, so the state still comes out as the correct
eigenstate with the correct energy even though the underlying unitary is
wrong.
So when the spin-up circuit flips parity I add a layer of Z gates on the
spin-down qubits to put the signs back (`_preserves_parity` decides
whether that's needed). With a fixed computational-basis initial state
it's only a global phase, so I skip it there.
I also ran into the general Gaussian decomposition not reliably
diagonalizing exactly-block-structured matrices (`U† H U` comes out
non-diagonal for some perfectly valid block inputs), so I went with
always splitting these instead of trying to detect when it's safe to
fall back. Splitting is the more correct path here, not just the faster
one.
### Tests
- `test_bogoliubov_transform_spin_block_gaussian_regression`: the actual
matrix from #776, normalized so it satisfies `W1 W1† + W2 W2† = I`. This
used to raise ValueError.
- `test_bogoliubov_transform_spin_block_operator_algebra`: checks `U
a_p† U⁻¹ = b_p†` on the full unitary, for both a parity-preserving and a
parity-flipping spin-up sector. This is the one that fails without the
sign fix.
- `test_bogoliubov_transform_spin_block_gaussian`: eigenstate energies
through the state-prep path.
- `test_bogoliubov_transform_spin_mixing_gaussian_not_split`: a
transform that genuinely mixes spin should still go through the general
path rather than getting split.
Outside the committed tests I ran a wider sweep (k = 2, 3, 4, real and
complex, a range of seeds) cross-checking three things that each catch a
sign error on their own: the operator algebra, whether `U† H U` is
diagonal, and the eigenstate energies. Everything passes, including 27
of the parity-flipping cases. The full `circuits/` suite passes and
black/pylint are clean.1 parent e6af378 commit b69bbbb
2 files changed
Lines changed: 250 additions & 6 deletions
Lines changed: 74 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
99 | 102 | | |
100 | | - | |
101 | | - | |
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
| |||
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
113 | 125 | | |
114 | 126 | | |
115 | 127 | | |
| |||
122 | 134 | | |
123 | 135 | | |
124 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
125 | 169 | | |
126 | 170 | | |
127 | 171 | | |
| |||
131 | 175 | | |
132 | 176 | | |
133 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
134 | 202 | | |
135 | 203 | | |
136 | 204 | | |
| |||
Lines changed: 176 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
119 | 295 | | |
120 | 296 | | |
121 | 297 | | |
| |||
0 commit comments