Closed kevinsung closed 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.
https://github.com/qiskit-community/ffsim/pull/119 implements this by adding Python methods to the class dynamically.
FermionOperator acts as a dictionary mapping keys to values. It should have
keys
,values
, anditems
methods.