load_pauli_op¶
- pennylane_qiskit.load_pauli_op(pauli_op: SparsePauliOp, params: Any | None = None, wires: Sequence | None = None) Operator [source]¶
Loads a PennyLane
Operator
from a Qiskit SparsePauliOp.- Parameters:
pauli_op (qiskit.quantum_info.SparsePauliOp) – the
SparsePauliOp
to be convertedparams (Any) – optional assignment of coefficient values for the
SparsePauliOp
; see the Qiskit documentation to learn more about the expected format of these parameterswires (Sequence | None) – optional assignment of wires for the converted
SparsePauliOp
; if the originalSparsePauliOp
acted on \(N\) qubits, then this must be a sequence of length \(N\)
- Returns:
The equivalent PennyLane operator.
- Return type:
pennylane.operation.Operator
Note
The wire ordering convention differs between PennyLane and Qiskit: PennyLane wires are enumerated from left to right, while the Qiskit convention is to enumerate from right to left. This means a
SparsePauliOp
term defined by the string"XYZ"
appliesZ
on wire 0,Y
on wire 1, andX
on wire 2. For more details, see the String representation section of the Qiskit documentation for thePauli
class.Example
Consider the following script which creates a Qiskit
SparsePauliOp
:from qiskit.quantum_info import SparsePauliOp qiskit_op = SparsePauliOp(["II", "XY"])
The
SparsePauliOp
contains two terms and acts over two qubits:>>> qiskit_op SparsePauliOp(['II', 'XY'], coeffs=[1.+0.j, 1.+0.j])
To convert the
SparsePauliOp
into a PennyLaneOperator
, use:>>> from pennylane_qiskit import load_pauli_op >>> load_pauli_op(qiskit_op) I(0) + X(1) @ Y(0)
Usage Details
You can convert a parameterized
SparsePauliOp
into a PennyLane operator by assigning literal values to each coefficient parameter. For example, the scriptimport numpy as np from qiskit.circuit import Parameter a, b, c = [Parameter(var) for var in "abc"] param_qiskit_op = SparsePauliOp(["II", "XZ", "YX"], coeffs=np.array([a, b, c]))
defines a
SparsePauliOp
with three coefficients (parameters):>>> param_qiskit_op SparsePauliOp(['II', 'XZ', 'YX'], coeffs=[ParameterExpression(1.0*a), ParameterExpression(1.0*b), ParameterExpression(1.0*c)])
The
SparsePauliOp
can be converted into a PennyLane operator by calling the conversion function and specifying the value of each parameter using theparams
argument:>>> load_pauli_op(param_qiskit_op, params={a: 2, b: 3, c: 4}) ( (2+0j) * I(0) + (3+0j) * (X(1) @ Z(0)) + (4+0j) * (Y(1) @ X(0)) )
Similarly, a custom wire mapping can be applied to a
SparsePauliOp
as follows:>>> wired_qiskit_op = SparsePauliOp("XYZ") >>> wired_qiskit_op SparsePauliOp(['XYZ'], coeffs=[1.+0.j]) >>> load_pauli_op(wired_qiskit_op, wires=[3, 5, 7]) Y(5) @ Z(3) @ X(7)