3

I have the following code to run an Estimator primitive by using Qiskit Runtime. It computes the expectation value $\langle O \rangle = \langle \psi | O | \psi \rangle$ (in this example $\langle 0 | Z | 0 \rangle = 1$ with zero variance):

from qiskit import QuantumCircuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_ibm_runtime import QiskitRuntimeService, Options, Session, Estimator

prepare the state |Ψ>

qc = QuantumCircuit(1)

define the operator/observable O

O = SparsePauliOp(['Z'])

service = QiskitRuntimeService() backend = 'ibmq_qasm_simulator' options = Options(resilience_level=0)

with Session(service=service, backend=backend) as session: estimator = Estimator(session=session, options=options) job = estimator.run(circuits=[qc], observables=[O])

print(job.result().values)

I know that Qiskit Runtime primitives can run on real IBM quantum devices but what I'm wondering whether is possible run them by using fake backends (or, more in general, custom noise models). For example, how can I run the code above on the FakeManila backend or passing a NoiseModel instance? Is there any other way to do that?

SimoneGasperini
  • 1,634
  • 1
  • 3
  • 18

1 Answers1

7

Using primitives with fake backends

You can use BackendEstimator to work with fake backends. As primitives implementation, BackendEstimator and BackendSampler are designed to be generic that can work with almost any backend.

from qiskit.primitives import BackendEstimator

estimator = BackendEstimator(backend = FakeNairobi())

job = estimator.run(qc, O) result = job.result() print(result.values)

Note that, if you are dealing with a provider that has native primitive implementations, like Qiskit Runtime or Qiskit Aer, you should use that native implementation. Most probably, it will be much more efficient.


Using primitives with Qiskit runtime noisy simulators

If what you want is to use primitives with Qiskit runtime noisy simulators, you can easily do that by creating the NoiseModel and passing it to the estimator options as follows:

from qiskit_aer.noise import NoiseModel

noisy_backend = service.get_backend('ibm_nairobi') backend_noise_model = NoiseModel.from_backend(noisy_backend)

simulator = service.get_backend('ibmq_qasm_simulator')

options = Options() options.resilience_level = 0 options.optimization_level = 0 options.simulator = { "noise_model": backend_noise_model }

with Session(service=service, backend=simulator) as session: estimator = Estimator(session=session, options=options) job = estimator.run(circuits=qc, observables=O) print(job.result().values)

Egretta.Thula
  • 11,986
  • 1
  • 13
  • 34