Commit be75d71
authored
Closes #880.
## Problem
`get_sparse_operator` raises on the operator produced by
`symmetry_conserving_bravyi_kitaev`:
```python
from openfermion import get_sparse_operator, FermionOperator, symmetry_conserving_bravyi_kitaev
fermion_op = FermionOperator("0^ 1^")
qubit_op = symmetry_conserving_bravyi_kitaev(fermion_op, 4, 2)
get_sparse_operator(qubit_op) # ValueError: axis 0 index 7 exceeds matrix dimension 4
```
## Root cause
`symmetry_conserving_bravyi_kitaev` finishes by calling
`remove_indices`, which shifts qubit indices. When two qubits are mapped
onto the **same** new index, a term ends up with multiple Paulis acting
on one qubit, e.g.:
```
((0, X), (1, Y), (1, X))
```
`remove_indices` writes these terms straight into the operator’s
`.terms` dict, so they bypass the simplification `QubitOperator`
normally performs on construction. `qubit_operator_sparse` assumes each
qubit appears at most once per term (it grows the tensor product one
factor per Pauli), so a repeated qubit makes the per-term matrix larger
than the `n_qubits` Hilbert space and the assembly raises.
## Fix
Rebuild the operator after `remove_indices` so every term is routed back
through `QubitOperator`’s simplification, restoring canonical
one-Pauli-per-qubit form (e.g. `((0, X), (1, Y), (1, X)) → ((0, X), (1,
Z))`). This keeps the operator mathematically identical — the existing
eigenspectrum-based tests still pass — while satisfying the invariant
that `QubitOperator`s are simplified, which `get_sparse_operator` and
other consumers rely on.
## Tests
Added `test_output_is_simplified_qubit_operator`, a regression test
built from the reporter's reproducer. It now asserts the transform
equals an independently worked-out, fully-simplified `QubitOperator` for
that input (rather than re-deriving the expectation from the output),
checks every term is canonical (one Pauli per qubit), and checks
`get_sparse_operator` no longer raises. The existing eigenspectrum-based
tests continue to pass, confirming the operator is unchanged
mathematically.
Verified with the repo's check scripts: `check/pytest -m "not slow"
src/openfermion/transforms/opconversions/remove_symmetry_qubits_test.py`
passes, and `check/format-incremental` and `check/pylint-changed-files`
are clean. `check/mypy` reports no issues in the changed module.
---------
Co-authored-by: stark256-spec <stark256-spec@users.noreply.github.com>
1 parent 2871f09 commit be75d71
2 files changed
Lines changed: 49 additions & 5 deletions
File tree
- src/openfermion/transforms/opconversions
Lines changed: 14 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
101 | 111 | | |
102 | | - | |
| 112 | + | |
103 | 113 | | |
104 | 114 | | |
105 | 115 | | |
| |||
Lines changed: 35 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
29 | | - | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
152 | 153 | | |
153 | 154 | | |
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 | + | |
0 commit comments