Grover's Search Snapshots error with Qiskit AerSimulator

Executing that with real noise simulator from IBM Quantum:

import pennylane as qml
import matplotlib.pyplot as plt
from pennylane import numpy as np
from qiskit_ibm_provider import IBMProvider
from qiskit_aer import noise, AerSimulator
provider = IBMProvider()
NUM_QUBITS = 2
wires = list(range(NUM_QUBITS))

try:
    simulator = AerSimulator.from_backend(provider.get_backend('ibm_osaka'))
    dev_ibm = qml.device("qiskit.aer", wire=NUM_QUBITS, simulator=simulator)
except Exception as e:
    print(e)

def equal_superposition(wires):
    for wire in wires:
        qml.Hadamard(wires=wire)


@qml.qnode(dev_ibm)
def circuit():
    qml.Snapshot("Initial state")
    equal_superposition(wires)
    qml.Snapshot("After applying the Hadamard gates")
    return qml.probs(wires=wires)  # Probability of finding a computational basis state on the wires

qml.snapshots(circuit)()

for k, result in results.items():
    print(f"{k}: {result}")

I’m getting that error message below:

---------------------------------------------------------------------------
DeviceError                               Traceback (most recent call last)
Cell In[16], line 13
     10     qml.Snapshot("After applying the Hadamard gates")
     11     return qml.probs(wires=wires)  # Probability of finding a computational basis state on the wires
---> 13 qml.snapshots(circuit)()
     15 for k, result in results.items():
     16     print(f"{k}: {result}")

File ~/.local/lib/python3.11/site-packages/pennylane/debugging.py:94, in snapshots.<locals>.get_snapshots(*args, **kwargs)
     91 if old_interface == "auto":
     92     qnode.interface = qml.math.get_interface(*args, *list(kwargs.values()))
---> 94 with _Debugger(qnode.device) as dbg:
     95     results = qnode(*args, **kwargs)
     96     # Reset interface

File ~/.local/lib/python3.11/site-packages/pennylane/debugging.py:34, in _Debugger.__init__(self, dev)
     31 def __init__(self, dev):
     32     # old device API: check if Snapshot is supported
     33     if isinstance(dev, qml.Device) and "Snapshot" not in dev.operations:
---> 34         raise DeviceError("Device does not support snapshots.")
     36     # new device API: check if it's the simulator device
     37     if isinstance(dev, qml.devices.Device) and not isinstance(dev, qml.devices.DefaultQubit):

DeviceError: Device does not support snapshots.

And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

Name: PennyLane
Version: 0.33.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/jose/.local/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info:           Linux-6.5.13-7-MANJARO-x86_64-with-glibc2.38
Python version:          3.11.6
Numpy version:           1.23.5
Scipy version:           1.11.4
Installed devices:
- default.gaussian (PennyLane-0.33.1)
- default.mixed (PennyLane-0.33.1)
- default.qubit (PennyLane-0.33.1)
- default.qubit.autograd (PennyLane-0.33.1)
- default.qubit.jax (PennyLane-0.33.1)
- default.qubit.legacy (PennyLane-0.33.1)
- default.qubit.tf (PennyLane-0.33.1)
- default.qubit.torch (PennyLane-0.33.1)
- default.qutrit (PennyLane-0.33.1)
- null.qubit (PennyLane-0.33.1)
- lightning.qubit (PennyLane-Lightning-0.33.1)
- qiskit.aer (PennyLane-qiskit-0.33.1)
- qiskit.basicaer (PennyLane-qiskit-0.33.1)
- qiskit.ibmq (PennyLane-qiskit-0.33.1)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.33.1)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.33.1)
- qiskit.remote (PennyLane-qiskit-0.33.1)

QISKIT VERSION:
{'qiskit': '0.45.1', 'qiskit-aer': '0.13.1', 'qiskit-ignis': None, 'qiskit-ibmq-provider': None, 'qiskit-nature': '0.7.1', 'qiskit-finance': '0.4.0', 'qiskit-optimization': '0.6.0', 'qiskit-machine-learning': '0.7.1'}

What I do to that work with AerSimulator?

Thanks,

Hello @Ferguson ! What you are doing is using the Qiskit device. That means that you are limited to the operations that said device has natively. Unfortunately Snapshot is not an operator that is included in that device. I propose the following alternatives:

  • If what you want is to run Grover’s algorithm, you can eliminate the Snapshots, these are used to see the intermediate state but do not affect the output.

  • If you don’t have any particular reason to use the qiskit device, you can use default.qubit and it will work.

  • If you want to use qiskit and get the state information, you can do a little trick. Instead of returning the probabilities, return qml.state(), and execute partial circuits to see the output it gives you. This may be a bit expensive and complicated to generalize but it’s the way I can think of.

If you have any questions, let me know :slight_smile:

Hello Guillermo.

Thanks for your comments but the main problem really is execute any circuit with noise applied on local simulator all time and if I use default.qubit I haven’t that. If I can use default.qubit with real noise about show me one sample please.

I had removed the Snapshot and after I get that working with noise model applied on local simulator via Qiskit Aer.

The final result it’s that:

import pennylane as qml
import matplotlib.pyplot as plt
from pennylane import numpy as np
from qiskit_ibm_provider import IBMProvider
from qiskit_aer import noise, AerSimulator
provider = IBMProvider()
NUM_QUBITS = 2
wires = list(range(NUM_QUBITS))

try:
    simulator = AerSimulator.from_backend(provider.get_backend('ibm_osaka'))
    dev_ibm = qml.device("qiskit.aer", wires=NUM_QUBITS, simulator=simulator)
except Exception as e:
    print(e)

def equal_superposition(wires):
    for wire in wires:
        qml.Hadamard(wires=wire)

@qml.qnode(dev_ibm)
def circuit():
        equal_superposition(wires)
        return qml.probs(wires=wires) # Probability of finding a computational basis state on the wires

results = circuit()

for k, result in enumerate(results):
    print(f"{k}: {result}")

y = np.real(results)
bit_strings = [f"{x:0{NUM_QUBITS}b}" for x in range(len(y))]

plt.bar(bit_strings, y, color = "#70CEFF")

plt.xticks(rotation="vertical")
plt.xlabel("State label")
plt.ylabel("Probability Amplitude")
plt.title("States probabilities amplitudes")
plt.show()

In this case, default.mixed exists in pennylane. This will allow you to add noise through different channels. An example can be found in this demo. I hope that helps!

Great! Thanks for your support.

1 Like