-
Notifications
You must be signed in to change notification settings - Fork 55
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
Cast gates to dense matrices in optools
#414
Cast gates to dense matrices in optools
#414
Conversation
entanglement_fidelity
optools
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.
Will approve once the contextlib calls are swapped out for LinearOperator checks.
Thanks for your PR!
pygsti/tools/optools.py
Outdated
@@ -430,6 +433,13 @@ def entanglement_fidelity(a, b, mx_basis='pp', is_tp=None, is_unitary=None): | |||
------- | |||
float | |||
""" | |||
# Attempt to cast to dense array. If this is already an array, the AttributeError | |||
# will be suppressed. | |||
with _contextlib.suppress(AttributeError): |
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.
Can we change the contextlib.suppress calls to an isinstance(_, LinearOperator) instead?
It's more pyGSTi style to check for the base class that provides the used API than a blanket error suppression.
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.
Of course! I'm more used to EAFP-style programming, but happy to accomodate LBYL, too :) Just pushed the changes!
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 Timo. There's definitely arguments to be made that EAFP is a nicer way to go, but it's appreciated that you're willing to conform to current style :)
Looks good, thanks for the contribution!
Description of changes
Minor change: in
optools.entanglement_fidelity
, accept both arrays and gates for thea
andb
arguments. The arguments are cast to (dense) arrays within the function scope.Perform similar casting in other functions, that call
a.shape
.Motivation
See also #406.
Since some update (cannot pinpoint which exactly), this casting was not done anymore, neither within
optools.entanglement_fidelity
, nor in any of the functions calling this one.I've chosen to cast to dense array within
optools.entanglement_fidelity
, rather than before calling this function, for two reasons:optools
. Other functions (such asaverage_gate_fidelity
, which callsentanglement_fidelity
) accept arrays or gates. Hence, I'm of the opinion that casting to array withinentanglement_fidelity
is the more consistent choice. Alternatively, casting can be done before calling it, but (IMO) it's more consistent to then also do that for the other metrics functions defined inoptools
. I'm okay with either way, but this seems the simpler and less backwards-breaking solution, but I'm happy to implemetn otherwise if required.average_gate_fidelity
, which states in the docstring that it accepts arrays or gates fora
andb
, but directly passesa
andb
toentanglement_fidelity
, leading to an error.a.shape
on gates that do not have theshape
attribute (such asComposedOp
s)