-
Notifications
You must be signed in to change notification settings - Fork 376
Consistent behaviour of StateFn.eval with OperatorBase.eval #1210
Conversation
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.
Allow calling eval on state function objects with no argument, which returns the VectorStateFn representation of the state function.
As the eval here inherits its docstring from OperatorBase can that be updated to describe this change. It talks about return values when self is a StateFn
This is consistent behaviour with OperatorBase.eval, which returns the MatrixOp representation, if no argument is passed.
OperatorBase eval is abstract and I do not see any such definition in OperatorBase eval docstring that this should be the behavior of subclasses when front is None. Is this the case elsewhere, maybe the docstring should be improved in this regard too
@@ -277,6 +277,11 @@ def assign_parameters(self, param_dict: dict) -> OperatorBase: | |||
def eval(self, | |||
front: Union[str, dict, np.ndarray, |
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.
I am not sure why, unlike OperatorBase, this has never been typehinted as Optional. Clearly it defaulted to None so should have been. Can we change this in all these state fn signatures especially now since it seem it will work in all cases whereas before it looks like things may well have failed in some of these should None have actually been passed.
Also I note the signature in OperatorBase for eval is a bit more specific around the Dict type on the eval signature than was done in the typehints in these state fns.
Now circuit_state_fn, if I am reading the code correctly would already drop through and do what is coded here anyway as the default last line. Maybe you want this explicit code here so its consistent with the other state fns.
Good point 👍 I added the optional typehint and put |
…ommunity#1210) * eval on a statefn returns the sampling probs * add tests, rm print * add reno * return VectorStateFn instead of Statevector * Change test case to use ddt instead of subtest loop Co-authored-by: Manoel Marques <[email protected]> Co-authored-by: Steve Wood <[email protected]> Co-authored-by: woodsp <[email protected]>
…ommunity#1210) * eval on a statefn returns the sampling probs * add tests, rm print * add reno * return VectorStateFn instead of Statevector * Change test case to use ddt instead of subtest loop Co-authored-by: Manoel Marques <[email protected]> Co-authored-by: Steve Wood <[email protected]> Co-authored-by: woodsp <[email protected]>
…ommunity/qiskit-aqua#1210) * eval on a statefn returns the sampling probs * add tests, rm print * add reno * return VectorStateFn instead of Statevector * Change test case to use ddt instead of subtest loop Co-authored-by: Manoel Marques <[email protected]> Co-authored-by: Steve Wood <[email protected]> Co-authored-by: woodsp <[email protected]>
…ommunity/qiskit-aqua#1210) * eval on a statefn returns the sampling probs * add tests, rm print * add reno * return VectorStateFn instead of Statevector * Change test case to use ddt instead of subtest loop Co-authored-by: Manoel Marques <[email protected]> Co-authored-by: Steve Wood <[email protected]> Co-authored-by: woodsp <[email protected]>
…ommunity/qiskit-aqua#1210) * eval on a statefn returns the sampling probs * add tests, rm print * add reno * return VectorStateFn instead of Statevector * Change test case to use ddt instead of subtest loop Co-authored-by: Manoel Marques <[email protected]> Co-authored-by: Steve Wood <[email protected]> Co-authored-by: woodsp <[email protected]>
Summary
Allow calling
eval
on state function objects with no argument, which returns theVectorStateFn
representation of the state function. This is consistent behaviour withOperatorBase.eval
, which returns theMatrixOp
representation, if no argument is passed.Details and comments
All state functions are converted to the
VectorStateFn
with the existing methodto_matrix_op
. For theOperatorStateFn
s it is assumed that the operator acts on the zero state (as is assumed in theCircuitStateFn
if a circuit is passed), therefore the first row is returned as vector.