Need help regarding qiskit.aer

Hi Catalina

I am using qiskit.aer to model system of my own. I know that Qiskit.aer is noisy simulator backend. But when I am using backend = “statevector_simulator“ the output should be deterministic but the solution isn’t. I am using the same example present in the pennylane website.

import pennylane as qml
dev = qml.device('qiskit.aer', wires=2,backend='statevector_simulator')

@qml.qnode(dev)
def circuit(x, y, z):
    qml.RZ(z, wires=[0])
    qml.RY(y, wires=[0])
    qml.RX(x, wires=[0])
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(wires=1))

circuit(0.2, 0.1, 0.3)

Please find the qml.about() result and pennylane-qiskit version.

Name: pennylane
Version: 0.42.3
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: 
Author: 
Author-email: 
License-Expression: Apache-2.0
Location: /Users/s.poyyapakkam/miniconda3/envs/qml/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, diastatic-malt, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, tomlkit, typing_extensions
Required-by: PennyLane-qiskit, pennylane_lightning

Platform info:           macOS-15.4-arm64-arm-64bit
Python version:          3.11.8
Numpy version:           2.3.2
Scipy version:           1.16.1
Installed devices:
- qiskit.aer (PennyLane-qiskit-0.42.0)
- qiskit.basicaer (PennyLane-qiskit-0.42.0)
- qiskit.basicsim (PennyLane-qiskit-0.42.0)
- qiskit.remote (PennyLane-qiskit-0.42.0)
- lightning.qubit (pennylane_lightning-0.42.0)
- default.clifford (pennylane-0.42.3)
- default.gaussian (pennylane-0.42.3)
- default.mixed (pennylane-0.42.3)
- default.qubit (pennylane-0.42.3)
- default.qutrit (pennylane-0.42.3)
- default.qutrit.mixed (pennylane-0.42.3)
- default.tensor (pennylane-0.42.3)
- null.qubit (pennylane-0.42.3)
- reference.qubit (pennylane-0.42.3)

Name: PennyLane-qiskit
Version: 0.42.0
Summary: PennyLane plugin for Qiskit
Home-page: https://github.com/XanaduAI/pennylane-qiskit
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/s.poyyapakkam/miniconda3/envs/qml/lib/python3.11/site-packages
Requires: networkx, numpy, pennylane, qiskit, qiskit-aer, qiskit-ibm-runtime, sympy
Required-by: 
Note: you may need to restart the kernel to use updated packages.

Please let me know if I am missing something.

Hi @Srivathsan_Sundar ,

I think qiskit.aer uses some random function to generate the results. I used a seed and it partially worked. For some strange reason the first time you run the code it gives one result and then it gives the same result thereafter. Is this good enough for what you need? I don’t know how qiskit aer is coded up so I think the issue comes from there.

import pennylane as qml
import numpy as np

np.random.seed(1234)
dev = qml.device('qiskit.aer', wires=2,backend='statevector_simulator')


@qml.qnode(dev)
def circuit(x, y, z):
    qml.RZ(z, wires=[0])
    qml.RY(y, wires=[0])
    qml.RX(x, wires=[0])
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(wires=1))

print(circuit(0.2, 0.1, 0.3))
print(circuit(0.2, 0.1, 0.3))
print(circuit(0.2, 0.1, 0.3))
print(circuit(0.2, 0.1, 0.3))
print(circuit(0.2, 0.1, 0.3))

Results

0.966796875
0.98046875
0.98046875
0.98046875
0.98046875

I hope this helps!

Hi @CatalinaAlbornoz

Thanks for your message. If I set the seed to be 14. The same code generates different results. Then I am thinking what’s the use of adding the term “statevector_simulator“. I know it’s due to qiskit.aer. If you get an update regarding this let me know.

import pennylane as qml

import numpy as np

np.random.seed(14)

dev = qml.device('qiskit.aer', wires=2,backend='statevector_simulator')

@qml.qnode(dev)

def circuit(x, y, z):

    qml.RZ(z, wires=[0])

    qml.RY(y, wires=[0])

    qml.RX(x, wires=[0])

    qml.CNOT(wires=[0, 1])

    return qml.expval(qml.PauliZ(wires=1))

print(circuit(0.2, 0.1, 0.3))

print(circuit(0.2, 0.1, 0.3))

print(circuit(0.2, 0.1, 0.3))

print(circuit(0.2, 0.1, 0.3))

print(circuit(0.2, 0.1, 0.3))

Results

0.951171875

0.970703125

0.974609375

0.97265625

0.96484375

Hi @Srivathsan_Sundar ,

I found the cause for this, and it was actually in the plugin.

At the moment the plugin uses 1024 shots by default. So when using shots you’re subject to statistical variation due to the nature of having a finite number of shots.

If you set shots=None you stop seeing this variation, e.g.

dev = qml.device('qiskit.aer', wires=2,backend='statevector_simulator', shots=None)

Note that this will change in the next release of the plugin (on or around Oct 15th). Starting with PennyLane-Qiskit v0.43 shots=None will be the default, so you won’t need to worry about this.

In addition to this, PennyLane will change how shots are added to programs since the shots will be added to the qnode instead of being added to the device. We’ll add examples in the documentation once it’s released so for now just make sure to use shots=None and let us know if you have any more questions!

Hi @CatalinaAlbornoz

Thanks a lot. When I initialize the shots = None. I am getting deterministic results.