-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Support Dict[str, OperatorBase] for aux_operators (fix #6772) #6870
Changes from 8 commits
ef51d8b
23d0b31
11ef00e
7eb96e1
9be9006
4e09082
ccf1dc5
6824351
23cfa63
98a127f
e5dfae3
68895c9
808a193
fed1a7d
2b3a6d2
c4b4679
32e608b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ | |
from qiskit.utils import QuantumInstance, algorithm_globals | ||
from ..optimizers import Optimizer, SLSQP | ||
from ..variational_algorithm import VariationalAlgorithm, VariationalResult | ||
from .minimum_eigen_solver import MinimumEigensolver, MinimumEigensolverResult | ||
from .minimum_eigen_solver import MinimumEigensolver, MinimumEigensolverResult, ListOrDict | ||
from ..exceptions import AlgorithmError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -411,32 +411,42 @@ def supports_aux_operators(cls) -> bool: | |
def _eval_aux_ops( | ||
self, | ||
parameters: np.ndarray, | ||
aux_operators: List[OperatorBase], | ||
aux_operators: ListOrDict[OperatorBase], | ||
expectation: ExpectationBase, | ||
threshold: float = 1e-12, | ||
) -> np.ndarray: | ||
) -> ListOrDict[complex]: | ||
# Create new CircuitSampler to avoid breaking existing one's caches. | ||
sampler = CircuitSampler(self.quantum_instance) | ||
|
||
aux_op_meas = expectation.convert(StateFn(ListOp(aux_operators), is_measurement=True)) | ||
if isinstance(aux_operators, dict): | ||
list_op = ListOp(list(aux_operators.values())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to use an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Starting from 3.6 a standard Python There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, that's very good to know! 👍 |
||
else: | ||
list_op = ListOp(aux_operators) | ||
|
||
aux_op_meas = expectation.convert(StateFn(list_op, is_measurement=True)) | ||
aux_op_expect = aux_op_meas.compose(CircuitStateFn(self.ansatz.bind_parameters(parameters))) | ||
values = np.real(sampler.convert(aux_op_expect).eval()) | ||
|
||
# Discard values below threshold | ||
aux_op_results = values * (np.abs(values) > threshold) | ||
# Deal with the aux_op behavior where there can be Nones or Zero qubit Paulis in the list | ||
_aux_op_nones = [op is None for op in aux_operators] | ||
aux_operator_eigenvalues = [ | ||
None if is_none else [result] | ||
for (is_none, result) in zip(_aux_op_nones, aux_op_results) | ||
] | ||
# As this has mixed types, since it can included None, it needs to explicitly pass object | ||
# data type to avoid numpy 1.19 warning message about implicit conversion being deprecated | ||
aux_operator_eigenvalues = np.array([aux_operator_eigenvalues], dtype=object) | ||
|
||
# Return None eigenvalues for None operators if aux_operators is a list. | ||
# None operators are already dropped in compute_minimum_eigenvalue if aux_operators is a dict. | ||
if isinstance(aux_operators, list): | ||
aux_operator_eigenvalues = [None] * len(aux_operators) | ||
key_value_iterator = enumerate(aux_op_results) | ||
else: | ||
aux_operator_eigenvalues = {} | ||
key_value_iterator = zip(aux_operators.keys(), aux_op_results) | ||
|
||
for key, value in key_value_iterator: | ||
if aux_operators[key] is not None: | ||
aux_operator_eigenvalues[key] = value | ||
|
||
return aux_operator_eigenvalues | ||
|
||
def compute_minimum_eigenvalue( | ||
self, operator: OperatorBase, aux_operators: Optional[List[Optional[OperatorBase]]] = None | ||
self, operator: OperatorBase, aux_operators: Optional[ListOrDict[OperatorBase]] = None | ||
) -> MinimumEigensolverResult: | ||
super().compute_minimum_eigenvalue(operator, aux_operators) | ||
|
||
|
@@ -454,19 +464,24 @@ def compute_minimum_eigenvalue( | |
initial_point = _validate_initial_point(self.initial_point, self.ansatz) | ||
|
||
bounds = _validate_bounds(self.ansatz) | ||
|
||
# We need to handle the array entries being Optional i.e. having value None | ||
# We need to handle the array entries being zero or Optional i.e. having value None | ||
if aux_operators: | ||
zero_op = I.tensorpower(operator.num_qubits) * 0.0 | ||
converted = [] | ||
for op in aux_operators: | ||
if op is None: | ||
converted.append(zero_op) | ||
else: | ||
converted.append(op) | ||
|
||
# For some reason Chemistry passes aux_ops with 0 qubits and paulis sometimes. | ||
aux_operators = [zero_op if op == 0 else op for op in converted] | ||
# Convert the None and zero values when aux_operators is a list. | ||
# Drop None and convert zero values when aux_operators is a dict. | ||
if isinstance(aux_operators, list): | ||
key_op_iterator = enumerate(aux_operators) | ||
converted = [zero_op] * len(aux_operators) | ||
else: | ||
key_op_iterator = aux_operators.items() | ||
converted = {} | ||
for key, op in key_op_iterator: | ||
if op is not None: | ||
converted[key] = zero_op if op == 0 else op | ||
|
||
aux_operators = converted | ||
|
||
else: | ||
aux_operators = None | ||
|
||
|
@@ -517,7 +532,7 @@ def compute_minimum_eigenvalue( | |
|
||
if aux_operators is not None: | ||
aux_values = self._eval_aux_ops(opt_params, aux_operators, expectation=expectation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@woodsp-ibm The conflict is the following
where I've locally changed the old Any suggestions on what should be done next? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you specify in a bit more detail what breaks? I just tried merging aux_values = self._eval_aux_ops(opt_result.x, aux_operators, expectation=expectation)
result.aux_operator_eigenvalues = aux_values The unittests in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is good to hear, thank you for testing. It is probably an issue with my local repository then since the tests also fail when I'm simply on the main branch after updating it to the upstream main branch. Just to be safe I'll merge via github instead of my local git repo.
|
||
result.aux_operator_eigenvalues = aux_values[0] | ||
result.aux_operator_eigenvalues = aux_values | ||
Comment on lines
-535
to
+550
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not exactly sure why this was only storing the first entry.. @woodsp-ibm do you know this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In looking it seems to be the weird way it was built out. Starts out with a List which it then nests, when it does an np.array as below, and then in the above it unnested it |
||
|
||
return result | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be valid to restrict the dictionary key to a
str
, or does it have to beAny
? If users want to use the VQE runtime and pass the aux ops as dictionary, it'll have to be serializable which is not guaranteed if the key can be anything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speaking for the purposes of the Qiskit Nature applications I believe
str
should be sufficient 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was discussed earlier #6870 (comment) but the topic of serialization did not arise. It seemed more flexible not to have to constrain key type; of course for serialization in the runtime one could come up with a set of uniques strings and map them on input/output to whatever keys were defined locally by the user - more work of course. I guess it would be easier to go from str to Any down the road if people object to the limitation so if for now str facilitates the runtime I guess we can go with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I had forgotten about our previous discussion...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CisterMoke could you please address this suggestion and change
Any
tostr
for the typehint inListOrDict
everywhere?After that I believe we should be able to proceed with merging this 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, sorry for the late update! I had kind of forgotten about this pull request at this point 😅