Skip to content

Commit 3218273

Browse files
authored
Merge branch 'main' into mh-revised-fix-481
2 parents d1be2c4 + a4d0bf2 commit 3218273

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/openfermion/linalg/sparse_tools.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
"""This module provides functions to interface with scipy.sparse."""
13+
1314
import itertools
1415
from functools import reduce
1516
import numpy.linalg
@@ -118,7 +119,7 @@ def jordan_wigner_sparse(fermion_operator, n_qubits=None):
118119
# Extract triplets from sparse_term.
119120
sparse_matrix = sparse_matrix.tocoo(copy=False)
120121
values_list.append(sparse_matrix.data)
121-
(row, column) = sparse_matrix.nonzero()
122+
row, column = sparse_matrix.nonzero()
122123
row_list.append(row)
123124
column_list.append(column)
124125

@@ -178,7 +179,7 @@ def qubit_operator_sparse(qubit_operator, n_qubits=None):
178179
# Extract triplets from sparse_term.
179180
sparse_matrix = kronecker_operators(sparse_operators)
180181
values_list.append(sparse_matrix.tocoo(copy=False).data)
181-
(column, row) = sparse_matrix.nonzero()
182+
column, row = sparse_matrix.nonzero()
182183
column_list.append(column)
183184
row_list.append(row)
184185

@@ -700,7 +701,7 @@ def expectation_computational_basis_state(operator, computational_basis_state):
700701
If operator is a FermionOperator, it must be normal-ordered.
701702
computational_basis_state (scipy.sparse vector / list): normalized
702703
computational basis state (if scipy.sparse vector), or list of
703-
occupied orbitals.
704+
zeros and ones for occupied and unoccupied orbitals, respectively.
704705
705706
Returns:
706707
A real float giving expectation value.
@@ -714,6 +715,9 @@ def expectation_computational_basis_state(operator, computational_basis_state):
714715
if not isinstance(operator, FermionOperator):
715716
raise TypeError('operator must be a FermionOperator.')
716717

718+
if not operator.is_normal_ordered():
719+
raise ValueError('operator must be a normal ordered.')
720+
717721
occupied_orbitals = computational_basis_state
718722

719723
if not isinstance(occupied_orbitals, list):
@@ -730,7 +734,8 @@ def expectation_computational_basis_state(operator, computational_basis_state):
730734
expectation_value += operator.terms.get(((i, 1), (i, 0)), 0.0)
731735

732736
for j in range(i + 1, len(occupied_orbitals)):
733-
expectation_value -= operator.terms.get(((j, 1), (i, 1), (j, 0), (i, 0)), 0.0)
737+
if occupied_orbitals[j]:
738+
expectation_value -= operator.terms.get(((j, 1), (i, 1), (j, 0), (i, 0)), 0.0)
734739

735740
return expectation_value
736741

@@ -1229,7 +1234,7 @@ def boson_operator_sparse(operator, trunc, hbar=1.0):
12291234

12301235
# Extract triplets from sparse_term.
12311236
values_list.append(term_operator.tocoo(copy=False).data)
1232-
(row, column) = term_operator.nonzero()
1237+
row, column = term_operator.nonzero()
12331238
column_list.append(column)
12341239
row_list.append(row)
12351240

src/openfermion/linalg/sparse_tools_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
"""Tests for sparse_tools.py."""
13+
1314
import os
1415

1516
import unittest
@@ -727,6 +728,17 @@ def test_expectation_qubit_operator_not_implemented(self):
727728
QubitOperator(), csc_matrix(([1], ([6], [0])), shape=(16, 1))
728729
)
729730

731+
def test_expectation_bad_operator_order(self):
732+
operator = (
733+
FermionOperator('2^ 2', 1.9)
734+
+ FermionOperator('2^ 1')
735+
+ FermionOperator('2^ 1 2 1^', -1.7)
736+
)
737+
state = [0, 1, 1]
738+
739+
with self.assertRaises(ValueError):
740+
expectation_computational_basis_state(operator, state)
741+
730742

731743
class ExpectationDualBasisOperatorWithPlaneWaveBasisState(unittest.TestCase):
732744
def setUp(self):

0 commit comments

Comments
 (0)