map_fermion_action_generators

map_fermion_action_generators(operator, map_action, identity, compose=None)

Map a FermionOperator to another operator type.

This is a generic function to aid in implementing new mappers for FermionOperator instances. At its core, it simply iterates over the terms of the operator, mapping each encountered FermionAction with the user-provided map_action function. In combination with the user-provided identity generator, this allows mapping to arbitrary output types.

Note

The output type T must support multiplication by a scalar via __mul__. If compose=None it must also support composition of two instances via __and__.

>>> from qiskit_fermions.mappers import map_fermion_action_generators
>>> from qiskit_fermions.operators import FermionAction, FermionOperator, ann, cre
>>> from qiskit.quantum_info import SparsePauliOp
>>>
>>> def jordan_wigner(action: FermionAction) -> SparsePauliOp:
...     act, idx = action
...     qubits = list(range(idx + 1))
...     return SparsePauliOp.from_sparse_list(
...         [
...             ("Z" * idx + "X", qubits, 0.5),
...             ("Z" * idx + "Y", qubits, -0.5j if act else 0.5j),
...         ],
...         num_qubits=num_qubits,
...     )
>>>
>>> num_qubits = 2
>>> def identity() -> SparsePauliOp:
...     return SparsePauliOp.from_sparse_list([("", [], 1)], num_qubits)
>>>
>>> op = FermionOperator.from_dict({(cre(0), ann(1)): 2.0})
>>> qop = map_fermion_action_generators(op, jordan_wigner, identity)
>>> print([(label, complex(coeff)) for label, coeff in sorted(qop.label_iter())])
[('II', 0j), ('XX', (0.5+0j)), ('XY', -0.5j), ('YX', 0.5j), ('YY', (0.5+0j))]
Parameters:
  • operator (FermionOperator) – the operator to be mapped.

  • map_action (Callable[[FermionAction], T]) – the function to map a single FermionAction to the desired output type.

  • identity (Callable[[], T]) – the function to generate the multiplicative identity instance of the output type.

  • compose (Callable[[T, T], T] | None) – an optional function to implement the compositiion logic of two output type instances. If this is not provided, it will default to using operator.and_().

Returns:

The mapped operator.

Return type:

T