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.'