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 SparsePauliOp created in Qiskit

  • params (Any) – optional assignment of coefficient values for the SparsePauliOp; see the Qiskit documentation to learn more about the expected format of these parameters

  • wires (Sequence | None) – optional assignment of wires for the converted SparsePauliOp; if the original SparsePauliOp acted on \(N\) qubits, then this must be a sequence of length \(N\)

Returns

The PennyLane operator, created based on the input Qiskit SparsePauliOp object.

Return type

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" applies Z on wire 0, Y on wire 1, and X on wire 2. For more details, see the String representation section of the Qiskit documentation for the Pauli 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 PennyLane pennylane.operation.Operator, use:

>>> import pennylane as qml
>>> qml.from_qiskit_op(qiskit_op)
I(0) + X(1) @ Y(0)

You can convert a parameterized SparsePauliOp into a PennyLane operator by assigning literal values to each coefficient parameter. For example, the script

import 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 the params argument:

>>> 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 SparsePauliOp as 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)

Contents

Using PennyLane

Release news

Development

API

Internals