Commit a4d0bf2
Fixed expectation_computational_basis_state (#1194)
Trying to fix `expectation_computational_basis_state`. The implemented
fix was proposed in issue #515.
I also added a test that covers the new exception that is raised when
the provided operator is normal ordered.
### Changes I made:
1. Checking for occupied orbitals in the inner `j`-loop:
```python
for j in range(i + 1, len(occupied_orbitals)):
if occupied_orbitals[j]:
expectation_value -= operator.terms.get(((j, 1), (i, 1), (j, 0), (i, 0)), 0.0)
```
2. Making more clear how a list of occupations has to be provided:
```markdown
computational basis state (if scipy.sparse vector), or list of
zeros and ones for occupied and unoccupied orbitals, respectively.
```
3. Adding a test that varifies that the new `ValueError` exception is
raised if the provided operator is not normal-ordered:
```python
def test_expectation_bad_operator_order(self):
operator = (
FermionOperator('2^ 2', 1.9)
+ FermionOperator('2^ 1')
+ FermionOperator('2^ 1 2 1^', -1.7)
)
state = [0, 1, 1]
with self.assertRaises(ValueError):
expectation_computational_basis_state(operator, state)
```
---------
Co-authored-by: Michael Hucka <mhucka@google.com>1 parent 0ac6357 commit a4d0bf2
2 files changed
Lines changed: 22 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
121 | | - | |
| 122 | + | |
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
| |||
178 | 179 | | |
179 | 180 | | |
180 | 181 | | |
181 | | - | |
| 182 | + | |
182 | 183 | | |
183 | 184 | | |
184 | 185 | | |
| |||
700 | 701 | | |
701 | 702 | | |
702 | 703 | | |
703 | | - | |
| 704 | + | |
704 | 705 | | |
705 | 706 | | |
706 | 707 | | |
| |||
714 | 715 | | |
715 | 716 | | |
716 | 717 | | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
717 | 721 | | |
718 | 722 | | |
719 | 723 | | |
| |||
730 | 734 | | |
731 | 735 | | |
732 | 736 | | |
733 | | - | |
| 737 | + | |
| 738 | + | |
734 | 739 | | |
735 | 740 | | |
736 | 741 | | |
| |||
1229 | 1234 | | |
1230 | 1235 | | |
1231 | 1236 | | |
1232 | | - | |
| 1237 | + | |
1233 | 1238 | | |
1234 | 1239 | | |
1235 | 1240 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
727 | 728 | | |
728 | 729 | | |
729 | 730 | | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
730 | 742 | | |
731 | 743 | | |
732 | 744 | | |
| |||
0 commit comments