map_edge_vertex_generators¶
- map_edge_vertex_generators(operator, map_action, identity, compose=None)¶
Map a
EdgeVertexOperatorto another operator type.This is a generic function to aid in implementing new mappers for
EdgeVertexOperatorinstances. At its core, it simply iterates over the terms of the operator, mapping each encounteredEdgeActionwith 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__.SparseObservabledoes not, which is why the example below namescompose()explicitly; a type with an__and__(such asSparsePauliOp) can rely on the default.Note
The mapping written out below is a deliberately minimal illustration of this function, not a replacement for
edge_vertex_jordan_wigner(): it covers only nearest-neighbor interactions, and it is neither parallelized nor memory-bounded. Reach for the library function rather than copying this one.>>> from qiskit_fermions.mappers import map_edge_vertex_generators >>> from qiskit_fermions.operators import EdgeAction, EdgeVertexOperator >>> from qiskit.quantum_info import SparseObservable >>> >>> def jordan_wigner_nearest_neighbor(mode: EdgeAction) -> SparseObservable: ... left, right = mode ... if left == right: ... return SparseObservable.from_sparse_list( ... [("Z", [left], 1.0)], num_qubits=num_qubits ... ) ... if abs(left - right) != 1: ... raise NotImplementedError( ... "This mapping only handles nearest neighbor interactions" ... ) ... # The orientation must be compared rather than differenced: an edge operator is ... # antisymmetric, so the sign depends on the index order while the string does not. ... lo, hi = min(left, right), max(left, right) ... coeff = -1.0 if left < right else 1.0 ... return SparseObservable.from_sparse_list( ... [("YX", [lo, hi], coeff)], num_qubits=num_qubits ... ) >>> >>> num_qubits = 4 >>> def identity() -> SparseObservable: ... return SparseObservable.identity(num_qubits) >>> >>> op = EdgeVertexOperator.from_dict({ ... ((0, 0),): 2.0, ... ((0, 1),): 0.5, ... ((1, 1), (1, 2)): 1.0, ... }) >>> qop = map_edge_vertex_generators( ... op, jordan_wigner_nearest_neighbor, identity, compose=SparseObservable.compose ... ) >>> print(sorted(qop.simplify().to_sparse_list())) [('XX', [1, 2], 1j), ('YX', [0, 1], (-0.5+0j)), ('Z', [0], (2+0j))]
- Parameters:
operator (EdgeVertexOperator) – the operator to be mapped.
map_action (Callable[[tuple[int, int]], T]) – the function to map a single
EdgeActionto 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