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

spin indexing #219 #224

Merged
merged 84 commits into from
Mar 1, 2018
Merged

spin indexing #219 #224

merged 84 commits into from
Mar 1, 2018

Conversation

conta877
Copy link
Contributor

@conta877 conta877 commented Feb 27, 2018

what do we think of something like this? #219

no docstrings or descriptions yet - I'll add them once everyones happy :)

created the order functions such that they take in the mode_idx and number of modes as arguments.
there is a lonely order_function (for up-then-down) under transforms/order_functions

here is a sample output:

from openfermion.transforms import get_fermion_operator
from openfermion.transforms import up_then_down
from openfermion.ops import normal_ordered


geometry = [("H", (0., 0., 0.)), ("H", (0., 0., 0.6))]
molecule = MolecularData(geometry, "sto-3g", 1,
                         description="0.6")
molecule.load()
molecular_hamiltonian = molecule.get_molecular_hamiltonian()
hamiltonian = normal_ordered(get_fermion_operator(molecular_hamiltonian))

print hamiltonian
print "\n----------------------\n"
print hamiltonian.reorder(order_function=up_then_down)
print "\n----------------------\n"
print hamiltonian.reorder(num_modes = 8, order_function=up_then_down)


0.17373064388 [3^ 0^ 2 1] +
-0.365770571815 [2^ 2] +
0.17373064388 [2^ 1^ 3 0] +
-0.515062453042 [3^ 1^ 3 1] +
-0.17373064388 [1^ 0^ 3 2] +
-0.365770571815 [3^ 3] +
-0.701337728655 [1^ 0^ 1 0] +
-0.688793096922 [2^ 1^ 2 1] +
-0.17373064388 [3^ 2^ 1 0] +
0.881962014317 [] +
-0.515062453042 [2^ 0^ 2 0] +
-0.724506023965 [3^ 2^ 3 2] +
-1.34221399305 [0^ 0] +
-1.34221399305 [1^ 1] +
-0.688793096922 [3^ 0^ 3 0]

----------------------

-0.515062453042 [1^ 0^ 1 0] +
-1.34221399305 [2^ 2] +
-0.17373064388 [2^ 0^ 3 1] +
0.17373064388 [3^ 0^ 1 2] +
-0.724506023965 [3^ 1^ 3 1] +
-0.365770571815 [3^ 3] +
-0.17373064388 [3^ 1^ 2 0] +
-0.688793096922 [1^ 2^ 1 2] +
0.881962014317 [] +
-0.701337728655 [2^ 0^ 2 0] +
-0.515062453042 [3^ 2^ 3 2] +
-1.34221399305 [0^ 0] +
-0.365770571815 [1^ 1] +
-0.688793096922 [3^ 0^ 3 0] +
0.17373064388 [1^ 2^ 3 0]

----------------------

0.17373064388 [5^ 0^ 1 4] +
-1.34221399305 [4^ 4] +
-0.688793096922 [1^ 4^ 1 4] +
-0.365770571815 [1^ 1] +
-0.17373064388 [5^ 1^ 4 0] +
-0.515062453042 [5^ 4^ 5 4] +
-0.17373064388 [4^ 0^ 5 1] +
-0.724506023965 [5^ 1^ 5 1] +
-0.701337728655 [4^ 0^ 4 0] +
0.17373064388 [1^ 4^ 5 0] +
-0.515062453042 [1^ 0^ 1 0] +
0.881962014317 [] +
-0.365770571815 [5^ 5] +
-1.34221399305 [0^ 0] +
-0.688793096922 [5^ 0^ 5 0]

this was quick prototyping haven't really tested it yet, please let me know if you spot bugs!

conta877 and others added 30 commits January 18, 2018 09:36
…ing mathematica code. it was giving wrong operators before, now the operators are fine but signs are wonky
…to count qubit/fermion labels from 1 instead of 0
…ht) as a tool to append the same code intance several times
…sts for symbolicBinary. minor mods for python3 comp.
…ode, added an error if qubit index in a code is out of bounds
@babbush
Copy link
Contributor

babbush commented Feb 28, 2018

@conta877 let me know when you are ready for review.

@conta877
Copy link
Contributor Author

conta877 commented Feb 28, 2018 via email

@babbush
Copy link
Contributor

babbush commented Feb 28, 2018

I think so. It would be good hear from at least two of either:
@ncrubin @kevinsung @jarrodmcc to make sure they agree since all three seemed to have opinions...

@conta877
Copy link
Contributor Author

conta877 commented Feb 28, 2018 via email

@kevinsung
Copy link
Collaborator

This looks fine to me.

@jarrodmcc
Copy link
Contributor

jarrodmcc commented Feb 28, 2018 via email

@conta877
Copy link
Contributor Author

conta877 commented Feb 28, 2018 via email

@conta877
Copy link
Contributor Author

conta877 commented Feb 28, 2018

is it worth having a separate place for the order functions or should they be put in somewhere else? up_then_down looks very lonely there right now.

@kevinsung
Copy link
Collaborator

kevinsung commented Feb 28, 2018

I suggest that for now, we keep both the functions reorder and up_then_down in the same file, either utils/_operator_utils.py or transforms/_conversion.py. I don't feel very strongly though.

@jarrodmcc
Copy link
Contributor

jarrodmcc commented Mar 1, 2018 via email

@conta877
Copy link
Contributor Author

conta877 commented Mar 1, 2018

ready for review

@kevinsung
Copy link
Collaborator

Thank you @conta877! In addition to the inline comments I have two general things:

  • If you haven't done so, please run the PEP8 linter on your code and fix the style violations. You can install it with pip install pycodestyle.
  • So the reverse flag should have the property that if I reorder a FermionOperator twice in a row with the same parameters, except once with reverse=False and once with reverse=True, then I should get the same thing back? Can you add a test for this for the up_then_down_map?

@@ -290,3 +290,61 @@ def save_operator(operator, file_name=None, data_directory=None,
with open(file_path, 'wb') as f:
marshal.dump((operator_type, dict(zip(tm.keys(),
map(complex, tm.values())))), f)


def reorder(Operator, order_function,num_modes=None, reverse=False):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here I would use lowercase to conform with our general style (operator instead of Operator). We generally use lowercase for variables.

@@ -21,7 +21,8 @@

from ._operator_utils import (count_qubits, eigenspectrum, fourier_transform,
get_file_path, inverse_fourier_transform,
is_identity, load_operator, save_operator)
is_identity, load_operator, save_operator,
reorder,up_then_down)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missed a space. Please run the PEP 8 linter.

@conta877
Copy link
Contributor Author

conta877 commented Mar 1, 2018

@kevinsung thanks for the review! - I caught a bug putting in the reverse test.

Copy link
Collaborator

@kevinsung kevinsung left a comment

Choose a reason for hiding this comment

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

Just one last thing I noticed.

@@ -15,8 +15,6 @@

import marshal
import numpy
import os
import time

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for removing time, but I think os is in fact used in the saving operation; apparently we don't have a test for that line right now...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops! I got tricked by pycharm. will add it back

@kevinsung
Copy link
Collaborator

Thanks @conta877 ! It turns out we did have that test (your pycharm is not crazy) but it passed due to namespace pollution from the * imports, which will be fixed in another PR. Anyway I think this is ready to be merged.

active_indices=None,
spin_indexing="even-odd"):
def get_molecular_hamiltonian(self,occupied_indices=None,
active_indices=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

this spacing seems to be inconsistent with pep8. No?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indeed. I'm failing at pep8ing with this commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

I figured we had given you enough trouble. Perhaps clean it up on the next PR if you're feeling charitable ;-)

@babbush babbush merged commit a0a43a5 into quantumlib:master Mar 1, 2018
philipp-q pushed a commit to philipp-q/OpenFermion that referenced this pull request Sep 2, 2020
* fixed bohr/angstrom confusion in function description

* translations from mathematica to python arxiv1712.07067

* work on those notimplementeds

* WIP: decoder class adittion

* modified decoder class. extractor implemented

* no need for pauliaction function

* introduction of symbolic binary class - no more decoder class

* minor bugs fixed, applies binary rules to input now

* started programming the BinaryCode class, work in progress

* added count qubit function in binary operator, modified code.py

* fixed morning brain bugs

* implemented concatination and appending, not yet documented and debugged

* added _shift function to symbolicBinary and integer addition. modified the decoder shifter accordinly

* radd imul, NOT TESTED! - just copy paste should work?

* fixed small bugs, added default codes, created transform

* fixed the multiplication error and modified the transform code following mathematica code. it was giving wrong operators before, now the operators are fine but signs are wonky

* fixed the transform and all the places where we accidentally started to count qubit/fermion labels from 1 instead of 0

* bug fixes: ordering matters to detect which terms should cancel

* fixed code appending, introduced the integer multiplication (left+right) as a tool to append the same code intance several times

* modified the multiply by 0 and 1 behavior in SymbolicBinary. added tests for symbolicBinary. minor mods for python3 comp.

* fixed the condition for code concatenation, fixed a bug in checksum_code, added an error if qubit index in a code is out of bounds

* added comments to binary_operator, more test, evaluate function

* started writing documentations and clipping lines, writing out numpy

* merging with the merger

* updating binary operator based on comments

* updating the binary_ops

* updates based on comments

* init update

* moved binaryop to first import

* added names into notice and readme

* started documentation in  _code_operator.py, added parity code / (K=1) and (K=2) segment code / K=1 binary addressing code

* encoders are sparse matrices now

* added tests for code_operator

* doc strings for the transform, fixed bug in the initialization of SymbolicBinary, made changes suggested by review, carried some of them over to files to be pulled later

* updates based on pull request comments

* merging changes with merger

* fixing possible integer checking errors

* cleaned version of transform function, needs testing and timing - seems to work so far

* fixed BK transforms. indexing errors

* added tests, pep8 and Ryan compliance mods

* added some style to the functions file

* renaming, re-structuring

* renaming again

* minor changes

* pep8

* changed the symbolicBInary class

* docstring cleanup

* bug docstring fixes

* bug fix in the evaluate method, pep8 the _binary_operator file

* merge with upstream and additional tests to SymbolicBinary class

* added weight-two segment code test, fixed bug with in the code appending by integer multiplication

* added half-up code

* bugs fixed in half-up code

* corrected weight_one_segment_code and the interleaved_code, former half-up. Added tests for the codes.

* get rid of heads

* reordering function for the fermionOperator class. takes 0,1,2,3,4,.. -> 0,2,4,..,1,3,...

* spin indexing within moleculardata

* proper master reset

* reverting spin_indexing to option 1 with arbitrary functions

* moving reorder under operator_utils and making it symbolicOperator general

* +1 bug fix

* docstrings, variable changes, tests

* coverage

* put up_then_down with reorder

* bug fixes, pep8

* adding os back
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

6 participants