qml.from_qiskit_op¶
- from_qiskit_op(qiskit_op, params=None, wires=None)[source]¶
Converts a Qiskit SparsePauliOp into a PennyLane
Operator.Note
This function depends upon the PennyLane-Qiskit plugin. Follow the installation instructions to get up and running. You may need to restart your kernel if you are running in a notebook environment.
- Parameters:
qiskit_op (qiskit.quantum_info.SparsePauliOp) – a
SparsePauliOpcreated in Qiskitparams (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 originalSparsePauliOpacted on \(N\) qubits, then this must be a sequence of length \(N\)
- Returns:
The PennyLane operator, created based on the input Qiskit
SparsePauliOpobject.- Return type:
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
SparsePauliOpterm defined by the string"XYZ"appliesZon wire 0,Yon wire 1, andXon wire 2. For more details, see the String representation section of the Qiskit documentation for thePauliclass.Example
Consider the following script which creates a Qiskit
SparsePauliOp:from qiskit.quantum_info import SparsePauliOp qiskit_op = SparsePauliOp(["II", "XY"])
The
SparsePauliOpcontains two terms and acts over two qubits:>>> qiskit_op SparsePauliOp(['II', 'XY'], coeffs=[1.+0.j, 1.+0.j])
To convert the
SparsePauliOpinto a PennyLanepennylane.operation.Operator, use:>>> import pennylane as qml >>> qml.from_qiskit_op(qiskit_op) I(0) + X(1) @ Y(0)
Usage Details
You can convert a parametrized
SparsePauliOpinto 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
SparsePauliOpwith three coefficients (parameters):>>> param_qiskit_op SparsePauliOp(['II', 'XZ', 'YX'], coeffs=[ParameterExpression(1.0*a), ParameterExpression(1.0*b), ParameterExpression(1.0*c)])
The
SparsePauliOpcan be converted into a PennyLane operator by calling the conversion function and specifying the value of each parameter using theparamsargument:>>> qml.from_qiskit_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
SparsePauliOpas follows:>>> wired_qiskit_op = SparsePauliOp("XYZ") >>> wired_qiskit_op SparsePauliOp(['XYZ'], coeffs=[1.+0.j]) >>> qml.from_qiskit_op(wired_qiskit_op, wires=[3, 5, 7]) Y(5) @ Z(3) @ X(7)