Skip to content

Commit f017de5

Browse files
jarrodmccbabbush
authored andcommitted
Minor fixes to InteractionRDM (#73)
* Fixed error from qubit operator convention change in InteractionRDM related to qubit operator expectation values * More minor fixes to fermion RDM bugs * Fixed RDM expecatations and tests to work normally without requiring calculating the identity externally, then removing it internally
1 parent 674abd6 commit f017de5

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/fermilib/ops/_interaction_rdm.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class InteractionRDM(InteractionTensor):
3535
one_body_tensor: The expectation values <a^\dagger_p a_q>.
3636
two_body_tensor: The expectation values
3737
<a^\dagger_p a^\dagger_q a_r a_s>.
38-
n_qubits: An int giving the number of qubits.
3938
"""
4039

4140
def __init__(self, one_body_tensor, two_body_tensor):
@@ -70,9 +69,11 @@ def expectation(self, operator):
7069
InteractionRDMError: Invalid operator provided.
7170
"""
7271
if isinstance(operator, QubitOperator):
73-
expectation_value = 0.
74-
for qubit_term in operator:
75-
expectation += qubit_term_expectation(self, qubit_term)
72+
expectation_op = self.get_qubit_expectations(operator)
73+
expectation = 0.0
74+
for qubit_term in operator.terms:
75+
expectation += (operator.terms[qubit_term] *
76+
expectation_op.terms[qubit_term])
7677
elif isinstance(operator, InteractionOperator):
7778
expectation = operator.constant
7879
expectation += numpy.sum(self.one_body_tensor *
@@ -99,13 +100,12 @@ def get_qubit_expectations(self, qubit_operator):
99100
"""
100101
from fermilib.transforms import reverse_jordan_wigner
101102
qubit_operator_expectations = copy.deepcopy(qubit_operator)
102-
del qubit_operator_expectations.terms[()]
103103
for qubit_term in qubit_operator_expectations.terms:
104104
expectation = 0.
105105

106106
# Map qubits back to fermions.
107107
reversed_fermion_operators = reverse_jordan_wigner(
108-
QubitOperator(qubit_term), self.n_qubits)
108+
QubitOperator(qubit_term))
109109
reversed_fermion_operators = normal_ordered(
110110
reversed_fermion_operators)
111111

src/fermilib/ops/_interaction_rdm_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_get_qubit_expectations(self):
3939
qubit_operator = jordan_wigner(self.hamiltonian)
4040
qubit_expectations = self.rdm.get_qubit_expectations(qubit_operator)
4141

42-
test_energy = qubit_operator.terms[()]
42+
test_energy = 0.0
4343
for qubit_term in qubit_expectations.terms:
4444
term_coefficient = qubit_operator.terms[qubit_term]
4545
test_energy += (term_coefficient *

src/fermilib/tests/_hydrogen_integration_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def test_rdm_numerically(self):
193193

194194
# Confirm expectation on qubit Hamiltonian using reverse JW matches.
195195
qubit_rdm = self.fci_rdm.get_qubit_expectations(self.qubit_hamiltonian)
196-
qubit_energy = self.qubit_hamiltonian.terms[()]
196+
qubit_energy = 0.0
197197
for term, expectation in qubit_rdm.terms.items():
198198
qubit_energy += expectation * self.qubit_hamiltonian.terms[term]
199199
self.assertAlmostEqual(qubit_energy, self.molecule.fci_energy)

src/fermilib/tests/_lih_integration_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_all(self):
9494

9595
# Confirm expectation on qubit Hamiltonian using reverse JW matches.
9696
qubit_rdm = self.fci_rdm.get_qubit_expectations(self.qubit_hamiltonian)
97-
qubit_energy = self.qubit_hamiltonian.terms[()]
97+
qubit_energy = 0.0
9898
for term, coefficient in qubit_rdm.terms.items():
9999
qubit_energy += coefficient * self.qubit_hamiltonian.terms[term]
100100
self.assertAlmostEqual(qubit_energy, self.molecule.fci_energy)

0 commit comments

Comments
 (0)