0
$\begingroup$

I am trying to run a small quantum circuit and generate the expectation value of an observable using the BackendEstimator primitive by incorporating the noise model from Fake20QV1 backend. Below is a snippet of the code.

qc = QuantumCircuit(3,3)
qc.x(0)
qc.x(1)
qc.h(2)
qc.x(2)

qc.measure(range(3), range(3))
observable = SparsePauliOp("ZZZ")
#qc.draw('mpl')
# Get the noise model of Fake20VQ1
backend_fake = Fake20QV1()
noise_model = noise.NoiseModel.from_backend(backend_fake)

# Get coupling map from backend
coupling_map = backend_fake.configuration().coupling_map

# Get basis gates from noise model
basis_gates = noise_model.basis_gates

# Perform a noisy simulation
backend = AerSimulator(noise_model=noise_model,
                       coupling_map=coupling_map,
                       basis_gates=basis_gates, shots=1024)
transpiled_circuit = transpile(qc, backend)
result = backend.run(transpiled_circuit).result()

Now, there are two ways in which I tried to run the circuit :

Alternative #1

counts = result.get_counts(0)
expectation_value = sampled_expectation_value(counts, observable)
plot_histogram(counts)
expectation_value

Runs successfully and gives the expected outputs.

Alternative #2 (using BackendSampler and BackendEstimator)

with Session(backend=backend_fake):
    sampler = BackendSampler()
    estimator = BackendEstimator()
    result1 = sampler.run(qc).result()
    print(f"Quasi-probability distribution: {result1.quasi_dists[0]}")

    result2 = estimator.run(qc, observable).result()
    print(f"EV : {result2.values[0]}")

Generates QiskitBackendNotFoundError: 'No backend matches the criteria.'

File ~/anaconda3/envs/qem/lib/python3.12/site-packages/qiskit_ibm_runtime/qiskit_runtime_service.py:779, in QiskitRuntimeService.backend(self, name, instance)
    763 """Return a single backend matching the specified filtering.
    764 
    765 Args:
   (...)
    776     QiskitBackendNotFoundError: if no backend could be found.
    777 """
    778 # pylint: disable=arguments-differ, line-too-long
--> 779 backends = self.backends(name, instance=instance)
    780 if not backends:
    781     cloud_msg_url = ""
...
--> 539         raise QiskitBackendNotFoundError("No backend matches the criteria.")
    540     if not self._backends[name] or instance_filter != self._backends[name]._instance:
    541         self._set_backend_config(name)

QiskitBackendNotFoundError: 'No backend matches the criteria.'
$\endgroup$

1 Answer 1

1
$\begingroup$

Since Qiskit has been updated, many things have been moved here and there, also the noise model fake backends that you are using. This is how you should run it in accordance with the recently updated Qiskit package.

Necessary Imports

from qiskit import QuantumCircuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_aer import AerSimulator
from qiskit_ibm_runtime.fake_provider import FakeVigo  # fake noisy backends are moved to ibm_runtime, do pip install qiskit-ibm-runtime before
from qiskit import transpile
from qiskit.result import sampled_expectation_value
from qiskit.visualization import plot_histogram
from qiskit.primitives import BackendEstimator, BackendSampler

Making the Quantum Circuit

qc = QuantumCircuit(3,3)
qc.x(0)
qc.x(1)
qc.h(2)
qc.x(2)

qc.measure(range(3), range(3))
observable = SparsePauliOp("ZZZ")
qc.draw('mpl',style="iqp")

Selecting the Noisy Backend and transpiling

device_backend = FakeVigo()
sim_vigo = AerSimulator.from_backend(device_backend)
transpiled_circuit = transpile(qc, sim_vigo)  # this step is necessary

Running a Noisy Simulation

result = sim_vigo.run(transpiled_circuit).result()
counts = result.get_counts(0)
expectation_value = sampled_expectation_value(counts, observable)

expectation_value

or getting the plot:

plot_histogram(counts)

Alternative Method

sampler = BackendSampler(backend=sim_vigo)
estimator = BackendEstimator(backend=sim_vigo)
result1 = sampler.run(qc).result()
print(f"Quasi-probability distribution: {result1.quasi_dists[0]}")

result2 = estimator.run(qc, observable).result()
print(f"EV : {result2.values[0]}")

You have to specify the backend in the argument of BackendSampler and BackendEstimator. This code will run.

$\endgroup$
4
  • $\begingroup$ I used the backend class Fake20QV1 from qiskit.provider.fake_provider module defined in qiskit 1.0.2 (from qiskit documentation). Does it mean that many modules/classes in the updated version are deprecated/unsupported? $\endgroup$ Mar 22 at 8:03
  • $\begingroup$ I think i missed the "Legacy Interface" part mentioned with V1 Fake backends in the qiskit.providers.fake_provider module's documentation. So it does seems unsupported now. $\endgroup$ Mar 22 at 8:13
  • $\begingroup$ They are not yet, maybe soon they will, but I can't import them from the provider class. It suggests me to use fake_backends from the qiskit_ibm_provider class. Yes, there are many changes from the previous qiskit 0.46.0 to the present qiskit 1.0.0 $\endgroup$ Mar 22 at 8:16
  • $\begingroup$ FYI Qiskit Runtime 0.22, just released, added a local testing mode for using fake backends, see docs.quantum.ibm.com/api/qiskit-ibm-runtime/… $\endgroup$
    – Steve Wood
    Mar 22 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.