Quantum Error Mitigation

Hi, how do I put error mitigation schemes into my Pennylane code?

So my code is:

# QML
import pennylane as qml

wires=4
dev4 = qml.device('qiskit.ibmq', wires=wires, backend='ibm_nairobi',
                  ibmqx_token='XXX', hub='ibm-q', group='open',
                  project='main', shots=100)
coupling_map = dev4.backend.configuration().to_dict()["coupling_map"]

dev4.set_transpile_args(
    **{
        "optimization_level": 1,
        "coupling_map": coupling_map,
        "layout_method": "sabre",
        "routing_method": "sabre",
    }
)

@qml.qnode(dev4)

def CONVCircuit(phi, wires, i=0):
    """
    quantum convolution Node
    """
    # parameter
    theta = np.pi / 2
    
    qml.RX(phi[0] * np.pi, wires=0)
    qml.RX(phi[1] * np.pi, wires=1)
    qml.RX(phi[2] * np.pi, wires=2)
    qml.RX(phi[3] * np.pi, wires=3)

    qml.CRZ(theta, wires=[1, 0])
    qml.CRZ(theta, wires=[3, 2])
    qml.CRX(theta, wires=[1, 0])
    qml.CRX(theta, wires=[3, 2])
    qml.CRZ(theta, wires=[2, 0])
    qml.CRX(theta, wires=[2, 0])
    
    # Expectation value
    measurement = qml.expval(qml.PauliZ(wires=0))

    return measurement

In the above code, where should I put the error mitigation techniques like M3 error mitigation , Digital ZNE and others?

In Qiskit, we use:

options = Options(resilience_level = 1)
with Session(service=service, backend=backend):
    # M3 error mitigation
    sampler = Sampler(options=options)
    job = sampler.run(circuits=qc, shots=4000)

How do I define such error mitigation schemes in Pennylane so that I can run it on IBM Quantum Hardware?

Hi @mass_of_15 ,

You can use PennyLane’s error mitigation transforms. In the documentation for the zero-noise extrapolation transform you will find some examples.

The key here is adding the transform right above your qnode. You can check this in the example below. Note: I ran it on qiskit.aer because the queues for the hardware backends are super long.

We also have a demo on differentiating quantum error mitigation transforms and another demo on error mitigation via zero noise extrapolation based on a recent IBM paper. They can both be great resources to get a deeper understanding on how these transforms work and to use them in PennyLane.

import pennylane as qml
from pennylane import numpy as np
import qiskit_ibm_provider
from pennylane.transforms import fold_global, richardson_extrapolate

# Save your API token
IBM_token = 'Your IBM Token'# Insert your token here
try:
    qiskit_ibm_provider.IBMProvider()
except:
    qiskit_ibm_provider.IBMProvider.save_account(token=IBM_token, overwrite=True)

# Define the backend that you will run on
b = "ibmq_qasm_simulator"
#b = "ibm_perth"

# Create your device
"""dev = qml.device(
    "qiskit.ibmq",
    wires=4,
    backend=b,
    shots = 10
)"""

dev = qml.device(
    "qiskit.aer",
    wires=4,
    shots = 10
)

# Create a QNode
@qml.transforms.mitigate_with_zne([1., 2., 3.], fold_global, richardson_extrapolate)
@qml.qnode(dev)
def circuit(phi):
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[1, 3])
    qml.CNOT(wires=[1, 2])
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[0, 3])
    qml.RX(phi,wires=0)
    return qml.probs(wires=[0, 1, 2, 3])

phi=np.array(0.1,requires_grad=True)

# Print your circuit
print(qml.draw(circuit)(phi))
circuit(phi)

Let me know if you have any questions about this!