qiskit-community / ffsim

Faster simulations of fermionic quantum circuits.
https://qiskit-community.github.io/ffsim/
Apache License 2.0
25 stars 8 forks source link

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

Closed kevinsung closed 7 months ago

kevinsung commented 10 months ago

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

kevinsung commented 7 months ago

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 https://github.com/PyO3/pyo3/issues/991 is implemented, then we could simply inherit from the Python Mapping abstract base class.

kevinsung commented 7 months ago

https://github.com/qiskit-community/ffsim/pull/119 implements this by adding Python methods to the class dynamically.