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
Expose inline_sum #330
Expose inline_sum #330
Changes from all commits
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 |
---|---|---|
|
@@ -32,6 +32,19 @@ class OperatorUtilsError(Exception): | |
pass | ||
|
||
|
||
def inline_sum(summands, seed): | ||
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. This argument order is way better. It would be even nicer if the seeds defaulted to additive and multiplicative identities IMO! 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. Actually wait, is there a nice way to do this including both Fermion and QubitOperators? 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. You could get one of the summands and use the class method. That wouldn't be nice, though, because summands is an iterable and not a sequence. 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. Yeah, oh well, I'll approve |
||
"""Computes a sum, using the __iadd__ operator. | ||
Args: | ||
seed (T): The starting total. The zero value. | ||
summands (iterable[T]): Values to add (with +=) into the total. | ||
Returns: | ||
T: The result of adding all the factors into the zero value. | ||
""" | ||
for r in summands: | ||
seed += r | ||
return seed | ||
|
||
|
||
def freeze_orbitals(fermion_operator, occupied, unoccupied=None, prune=True): | ||
"""Fix some orbitals to be occupied and others unoccupied. | ||
|
||
|
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.
Could you use the
zero()
andidentity()
class methods? I find it a little hard to distinguish()
and(())
with a quick lookThere 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.
Sorry, I disagree with you. I strongly believe that
QubitOperator()
is the nicest way to initialize the zero operator in all situations where it's possible.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.
Here it appears as
QubitOperator())
, so it's hard to read, vszero
which is impossible to mistake forone
(e:identity
).I try to think of this from the perspective of a new user - they shouldn't have to think about what the default coefficient / initialization is. identity and zero make the difference 100% obvious. In the past (()) and () used to do the opposite thing as well so this is plainly not an intuitive difference. Why are you against it?
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 wasn't around when (()) and () meant the opposite, but you guys must have changed it because you realized it is totally inconsistent with the definition of the initialization function,
The identity is a term that has a coefficient, and there is no reasonable choice of tuple to represent it other than the empty tuple
()
. On the other hand, the zero operator represents the lack of any terms. Since the zero operator has no terms but the identity does have a term, initialization with no arguments must mean the zero operator. This is consistent with default initializers in general, which generally mean "do the bare minimum to instantiate a class". In this case, the bare minimum is to create a terms dictionary and leave it empty.My way is more "pythonic" and I expect new users to pick up on the distinction very quickly.