Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add keys, values, and items methods to FermionOperator #90

Closed
kevinsung opened this issue Jan 8, 2024 · 2 comments · Fixed by #119
Closed

Add keys, values, and items methods to FermionOperator #90

kevinsung opened this issue Jan 8, 2024 · 2 comments · Fixed by #119
Labels
rust Involves writing Rust

Comments

@kevinsung
Copy link
Collaborator

kevinsung commented Jan 8, 2024

FermionOperator acts as a dictionary mapping keys to values. It should have keys, values, and items methods.

@kevinsung kevinsung added rust Involves writing Rust good first issue Good for newcomers labels Jan 8, 2024
@kevinsung kevinsung changed the title Add values and items methods to FermionOperator Add keys, values, and items methods to FermionOperator Jan 13, 2024
@kevinsung kevinsung removed the good first issue Good for newcomers label Apr 1, 2024
@kevinsung
Copy link
Collaborator Author

I would like this test to pass:

def test_mapping_methods():
    op = FermionOperator(
        {
            (ffsim.cre_a(1), ffsim.des_a(2)): 1,
            (ffsim.cre_a(2), ffsim.des_a(1)): 0.5,
            (ffsim.cre_b(1), ffsim.des_b(2)): -0.5j,
            (ffsim.cre_b(2), ffsim.des_b(1)): 1 - 0.5j,
        }
    )
    assert op.keys() == {
        (ffsim.cre_a(1), ffsim.des_a(2)),
        (ffsim.cre_a(2), ffsim.des_a(1)),
        (ffsim.cre_b(1), ffsim.des_b(2)),
        (ffsim.cre_b(2), ffsim.des_b(1)),
    }

It turns out to be more complicated than I expected, since the keys method would need to return a Rust object that, for example, knows how to compare correctly to Python objects.

If PyO3/pyo3#991 is implemented, then we could simply inherit from the Python Mapping abstract base class.

@kevinsung
Copy link
Collaborator Author

#119 implements this by adding Python methods to the class dynamically.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
rust Involves writing Rust
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant