map_transfer_vertex_generators¶
- map_transfer_vertex_generators(operator, map_action, identity, compose=None)¶
Map a
TransferVertexOperatorto another operator type.This is a generic function to aid in implementing new mappers for
TransferVertexOperatorinstances. At its core, it simply iterates over the terms of the operator, mapping each encounteredTransferActionwith the user-providedmap_actionfunction. In combination with the user-providedidentitygenerator, this allows mapping to arbitrary output types.Note
The output type
Tmust support multiplication by a scalar via__mul__. Ifcompose=Noneit must also support composition of two instances via__and__.>>> from qiskit_fermions.mappers import map_transfer_vertex_generators >>> from qiskit_fermions.operators import TransferAction, TransferVertexOperator >>> from qiskit.quantum_info import SparsePauliOp >>> >>> def jordan_wigner_nearest_neighbor(mode: TransferAction) -> SparsePauliOp: ... match abs(mode[0] - mode[1]): ... case 0: ... pauli = "Z" ... qubits = [mode[0]] ... coeff = 1.0 ... case 1: ... pauli = "YY" ... qubits = [mode[0], mode[1]] ... coeff = -0.5 ... case _: ... raise NotImplementedError( ... "This mapping only handles nearest neighbor interactions" ... ) ... ... return SparsePauliOp.from_sparse_list([(pauli, qubits, coeff)], num_qubits=num_qubits) >>> >>> num_qubits = 4 >>> def identity() -> SparsePauliOp: ... return SparsePauliOp.from_sparse_list([("", [], 1)], num_qubits) >>> >>> op = TransferVertexOperator.from_dict({ ... ((0, 0),): 2.0, ... ((0, 1),): 0.5, ... ((1, 1), (1, 2)): 1.0, ... }) >>> qop = map_transfer_vertex_generators(op, jordan_wigner_nearest_neighbor, identity) >>> print([(label, complex(coeff)) for label, coeff in sorted(qop.label_iter())]) [('IIII', 0j), ('IIIZ', (2+0j)), ('IIYY', (-0.25+0j)), ('IYXI', 0.5j)]
- Parameters:
operator (TransferVertexOperator) – the operator to be mapped.
map_action (Callable[[TransferAction], T]) – the function to map a single
TransferActionto 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