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+
1314import itertools
1415from functools import reduce
1516import 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
0 commit comments