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

Added human-readable file IO for SymbolicOperators #311

Merged
merged 4 commits into from
Apr 17, 2018

Conversation

quid256
Copy link
Contributor

@quid256 quid256 commented Apr 14, 2018

Proposed resolution to #122

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@quid256
Copy link
Contributor Author

quid256 commented Apr 14, 2018

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

@babbush babbush requested a review from kevinsung April 14, 2018 23:49
@@ -396,13 +396,15 @@ def inverse_fourier_transform(hamiltonian, grid, spinless):
vec_func_2=momentum_vector)


def load_operator(file_name=None, data_directory=None):
def load_operator(file_name=None, data_directory=None, human_readable=False):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of human_readable let's call this plain_text.

"""Load FermionOperator or QubitOperator from file.

Args:
file_name: The name of the saved file.
data_directory: Optional data directory to change from default data
directory specified in config file.
human_readable: Whether the operator should be loaded from a
human-readable format
Copy link
Collaborator

Choose a reason for hiding this comment

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

"Whether the input file is plain text."

loaded_fermion_operator = load_operator(self.file_name,
human_readable=True)
self.assertEqual(self.fermion_operator.terms,
loaded_fermion_operator.terms)
Copy link
Collaborator

Choose a reason for hiding this comment

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

self.assertEqual(self.fermion_operator, loaded_fermion_operator)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should the rest of the test cases be comparing the original operator and the loaded operator in this way? Including the cases that have already been written?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That would be nice but I won't require it. Those tests were actually written before this was possible; we didn't always implement __eq__ in SymbolicOperator.

map(complex, tm.values())))), f)
if human_readable:
with open(file_path, 'w') as f:
f.write(operator_type + ": " + str(operator))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we'll want to do :\n instead of : . Can you add a test with small operator that explicitly includes what the saved format looks like? I.e., the test should compare the saved file with an explicit string.

for term in operator_terms:
operator += QubitOperator(term, operator_terms[term])
else:
raise TypeError('Operator of invalid type.')
Copy link
Collaborator

Choose a reason for hiding this comment

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

It should not be necessary to add terms individually. It should be possible to simply do something like operator = FermionOperator(some_string) where some_string is probably going to be straight from the file, minus the initial heading which says what type it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this code is constructing the operators as you suggest. It uses the split function only to separate the type of the operator from the remaining data, which is stored as a string when the data is loaded from plain text

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I was looking at the wrong part of the code. Yes, you did do it that way. Great!

kevinsung
kevinsung previously approved these changes Apr 17, 2018
@kevinsung
Copy link
Collaborator

Thanks for the contribution! Feel free to add your name and affiliation to the NOTICE and README, if you like.

@babbush
Copy link
Contributor

babbush commented Apr 17, 2018

... in the alphabetical section, please!

@kevinsung kevinsung merged commit e42fd2e into quantumlib:master Apr 17, 2018
philipp-q pushed a commit to philipp-q/OpenFermion that referenced this pull request Sep 2, 2020
* Added human-readable file IO for SymbolicOperators

* Renamed human_readable to plain_text and fixed tests

* Changed operator equality tests to use __eq__ instead of directly comparing terms
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

4 participants