map_majorana_action_generators
map_majorana_action_generators(operator, map_action, identity, compose=None)
Map a MajoranaOperator to another operator type.
This is a generic function to aid in implementing new mappers for MajoranaOperator instances. At its core, it simply iterates over the terms of the operator, mapping each encountered MajoranaAction with the user-provided map_action function. In combination with the user-provided identity generator, this allows mapping to arbitrary output types.
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__. SparseObservable does not, which is why the example below names compose() explicitly; a type with an __and__ (such as SparsePauliOp) can rely on the default.
The mapping written out below is a minimal illustration of this function rather than a replacement for majorana_jordan_wigner(), which is parallelized and bounds the memory it uses while assembling the result.
>>> from qiskit_fermions.mappers import map_majorana_action_generators
>>> from qiskit_fermions.operators import MajoranaAction, MajoranaOperator, gamma
>>> from qiskit.quantum_info import SparseObservable
>>>
>>> def jordan_wigner(mode: MajoranaAction) -> SparseObservable:
... idx = mode // 2
... qubits = list(range(idx + 1))
... pauli = "Y" if mode % 2 else "X"
... return SparseObservable.from_sparse_list(
... [("Z" * idx + pauli, qubits, 1.0)],
... num_qubits=num_qubits,
... )
>>>
>>> num_qubits = 2
>>> def identity() -> SparseObservable:
... return SparseObservable.identity(num_qubits)
>>>
>>> op = MajoranaOperator.from_dict({
... (0, 2): 0.5,
... (1, 3): 0.5,
... (0, 3): 0.5j,
... (1, 2): -0.5j,
... })
>>> qop = map_majorana_action_generators(
... op, jordan_wigner, identity, compose=SparseObservable.compose
... )
>>> print(sorted(qop.simplify().to_sparse_list()))
[('XX', [0, 1], (0.5-0j)), ('XY', [0, 1], 0.5j), ('YX', [0, 1], -0.5j), ('YY', [0, 1], (0.5+0j))]
Parameters
- operator (MajoranaOperator) – the operator to be mapped.
- map_action (Callable[[int], T]) – the function to map a single
MajoranaActionto 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