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

Expose inline_sum #330

Merged
merged 1 commit into from May 3, 2018
Merged

Expose inline_sum #330

merged 1 commit into from May 3, 2018

Conversation

kevinsung
Copy link
Collaborator

Using this function is much more efficient than adding a list of SymbolicOperators using the built-in sum. Previously it was hidden in bravyi_kitaev.py. Changed the argument order to match up with the built-in sum.

Also changed the argument order of inline_product but did not expose it.

@kevinsung kevinsung requested a review from babbush May 3, 2018 16:27
Copy link
Collaborator

@idk3 idk3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, the old argument order is definitely weird. Two nits but lgtm!

@@ -53,7 +53,7 @@ def bravyi_kitaev(operator, n_qubits=None):
n_qubits=n_qubits)
for term in operator.terms
)
return inline_sum(seed=QubitOperator(), summands=transformed_terms)
return inline_sum(summands=transformed_terms, seed=QubitOperator())
Copy link
Collaborator

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() and identity() class methods? I find it a little hard to distinguish () and (()) with a quick look

Copy link
Collaborator Author

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.

Copy link
Collaborator

@idk3 idk3 May 3, 2018

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, vs zero which is impossible to mistake for one (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?

Copy link
Collaborator Author

@kevinsung kevinsung May 3, 2018

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,

    def __init__(self, term=None, coefficient=1.):

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.

@@ -32,6 +32,19 @@ class OperatorUtilsError(Exception):
pass


def inline_sum(summands, seed):
Copy link
Collaborator

Choose a reason for hiding this comment

The 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!

Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, oh well, I'll approve

@kevinsung kevinsung merged commit eb8ef6d into quantumlib:master May 3, 2018
@kevinsung kevinsung deleted the inline_sum branch May 3, 2018 18:57
philipp-q pushed a commit to philipp-q/OpenFermion that referenced this pull request Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants