Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix of eval_observables() when quantum_circuit is a StateFn. #7863

Merged
merged 5 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions qiskit/algorithms/aux_ops_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ def _prepare_list_op(
if isinstance(observables, dict):
observables = list(observables.values())

state = StateFn(quantum_state)
return ListOp([StateFn(obs, is_measurement=True).compose(state) for obs in observables])
if not isinstance(quantum_state, StateFn):
quantum_state = StateFn(quantum_state)

return ListOp([StateFn(obs, is_measurement=True).compose(quantum_state) for obs in observables])


def _prepare_result(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fixes:
- |
The :func:`~qiskit.algorithms.eval_observables` function raised an error when the
``quantum_state`` argument is a :class:`~qiskit.opflow.StateFn`.
This error was fixed and ``eval_observables`` now correctly supports all input types
specifying in the type hints.
22 changes: 21 additions & 1 deletion test/python/algorithms/test_aux_ops_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@
from qiskit.algorithms import eval_observables
from qiskit import BasicAer, QuantumCircuit
from qiskit.circuit.library import EfficientSU2
from qiskit.opflow import PauliSumOp, X, Z, I, ExpectationFactory, OperatorBase, ExpectationBase
from qiskit.opflow import (
PauliSumOp,
X,
Z,
I,
ExpectationFactory,
OperatorBase,
ExpectationBase,
StateFn,
)
from qiskit.utils import QuantumInstance, algorithm_globals


Expand Down Expand Up @@ -155,6 +164,17 @@ def test_eval_observables(self, observables: ListOrDict[OperatorBase]):
quantum_instance,
)

with self.subTest(msg="Test StateFn."):
statefn = StateFn(bound_ansatz)
self._run_test(
expected_result,
statefn,
decimal,
expectation,
observables,
quantum_instance,
)


if __name__ == "__main__":
unittest.main()