Skip to content

Commit

Permalink
Fix bug in fixing HF energy with asymmetric bitstrings. Other bugfixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-johnson committed Jul 12, 2023
1 parent 8f6fba9 commit 36ad956
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
23 changes: 8 additions & 15 deletions circuit_knitting/forging/entanglement_forging_knitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from __future__ import annotations

import copy
from typing import Sequence, Any
from concurrent.futures import ThreadPoolExecutor

Expand Down Expand Up @@ -206,6 +207,8 @@ def __call__(
tensor_ansatze_u = [
prep_circ.compose(circuit_u) for prep_circ in self._tensor_circuits_u
]
if self.fixed_hf_value is not None:
tensor_ansatze_u = tensor_ansatze_u[1:]
superposition_ansatze_u = [
prep_circ.compose(circuit_u) for prep_circ in self._superposition_circuits_u
]
Expand All @@ -216,6 +219,8 @@ def __call__(
tensor_ansatze_v = [
prep_circ.compose(circuit_u) for prep_circ in self._tensor_circuits_v
]
if self.fixed_hf_value is not None:
tensor_ansatze_v = tensor_ansatze_v[1:]
superposition_ansatze_v = [
prep_circ.compose(circuit_u)
for prep_circ in self._superposition_circuits_v
Expand Down Expand Up @@ -298,16 +303,15 @@ def __call__(
"be set when a job_id is present."
)
self._session_ids[i] = job_id

if self._fixed_hf_value is not None:
nans_u = np.empty(np.shape(tensor_expvals[0]))
num_paulis = len(forged_operator.tensor_paulis)
nans_u = np.empty(num_paulis)
nans_u[:] = np.nan
if not self._ansatz.bitstrings_are_symmetric:
# Should be equal number of expvals for each subsystem
assert len(tensor_expvals) % 2 == 0
num_tensor_terms = int(len(tensor_expvals) / 2)
nans_v = np.empty(np.shape(tensor_expvals[num_tensor_terms]))
nans_v[:] = np.nan
nans_v = copy.deepcopy(nans_u)
tensor_expvals.insert(num_tensor_terms, nans_v)
tensor_expvals.insert(0, nans_u)

Expand Down Expand Up @@ -633,14 +637,6 @@ def _estimate_expvals(
Returns:
The expectation values for the tensor circuits and superposition circuits
"""
# Ignore HF energy calculation. We will hard-code it later.
if fixed_hf_value is not None:
if not bitstrings_are_symmetric:
# Should have equal number of bitstrings for each subsystem
assert len(tensor_ansatze) % 2 == 0
del tensor_ansatze[int(len(tensor_ansatze) / 2)]
tensor_ansatze = tensor_ansatze[1:]

ansatz_t: list[QuantumCircuit] = []
observables_t: list[Pauli] = []
for i, circuit in enumerate(tensor_ansatze):
Expand Down Expand Up @@ -692,9 +688,6 @@ def _estimate_expvals(
# Post-process the results to get our expectation values in the right format
num_tensor_expvals = len(tensor_ansatze) * len(tensor_paulis)
estimator_results_t = results[:num_tensor_expvals]
if fixed_hf_value is not None:
for _ in tensor_paulis:
np.insert(estimator_results_t, 0, np.nan)
estimator_results_s = results[num_tensor_expvals:]

tensor_expval_list = list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def test_fixed_hf_h2o(self):
)

@pytest.mark.slow
@unittest.skipIf(not pyscf_available, "pyscf is not installed")
def test_fixed_hf_h2o_asymmetric(self):
"""Test for fixing the HF value in two separate subsystems."""
# Set up the ElectronicStructureProblem
Expand Down

0 comments on commit 36ad956

Please sign in to comment.