Repository: Qiskit/qiskit (python)

Issue:
`BackendSamplerV2` fails to run with empty classical register
### Environment

- **Qiskit version**: `main`
- **Python version**: 3.11
- **Operating system**: Linux


### What is happening?

The new `BackendSamplerV2` primitive fails to run if provided a circuit where at least one classical register contains zero bits.

### How can we reproduce the issue?

```python
from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.primitives import BackendSamplerV2
from qiskit_aer import AerSimulator

qreg = QuantumRegister(1)
creg0 = ClassicalRegister(0)
creg1 = ClassicalRegister(1)
qc = QuantumCircuit(qreg, creg0, creg1)
qc.h(0)
qc.measure(0, 0)

backend = AerSimulator()
sampler = BackendSamplerV2(backend=backend)
sampler.run([qc], shots=1024).result()
```

### What should happen?

Successful execution

### Any suggestions?

Don't assume it's safe to lookup index 0 of the `creg` at
https://github.com/Qiskit/qiskit/blob/194cc8fb9aedc24a5082eff351fcb7b70da59f17/qiskit/primitives/backend_sampler_v2.py#L189

Pull request description:
Fix `BackendSamplerV2` when used with an empty classical register
<!--
⚠️ If you do not respect this template, your pull request will be closed.
⚠️ Your pull request title should be short detailed and understandable for all.
⚠️ Also, please add a release note file using reno if the change needs to be
  documented in the release notes.
⚠️ If your pull request fixes an open issue, please link to the issue.

- [ ] I have added the tests to cover my changes.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the CONTRIBUTING document.
-->

### Summary

Fixes #12043


### Details and comments

Remaining action items:
- [x] ~~fix `get_counts` (currently throws an error in `reshape` because an array dimension is zero)~~ This is a problem with `BitArray`, not `BackendSamplerV2`, so I opened an issue instead: #12062

Patch:
diff --git a/qiskit/primitives/backend_sampler_v2.py b/qiskit/primitives/backend_sampler_v2.py
index ddd7b0b9b..23861aa34 100644
--- a/qiskit/primitives/backend_sampler_v2.py
+++ b/qiskit/primitives/backend_sampler_v2.py
@@ -186,7 +186,10 @@ def _analyze_circuit(circuit: QuantumCircuit) -> tuple[list[_MeasureInfo], int]:
     for creg in circuit.cregs:
         name = creg.name
         num_bits = creg.size
-        start = circuit.find_bit(creg[0]).index
+        if num_bits != 0:
+            start = circuit.find_bit(creg[0]).index
+        else:
+            start = 0
         meas_info.append(
             _MeasureInfo(
                 creg_name=name,


Test patch:
diff --git a/test/python/primitives/test_backend_sampler_v2.py b/test/python/primitives/test_backend_sampler_v2.py
index 6e1579c2f..dcddbf112 100644
--- a/test/python/primitives/test_backend_sampler_v2.py
+++ b/test/python/primitives/test_backend_sampler_v2.py
@@ -656,6 +656,21 @@ class TestBackendSamplerV2(QiskitTestCase):
         self.assertEqual(len(result), 1)
         self.assertEqual(len(result[0].data), 0)
 
+    @combine(backend=BACKENDS)
+    def test_empty_creg(self, backend):
+        """Test that the sampler works if provided a classical register with no bits."""
+        # Test case for issue #12043
+        q = QuantumRegister(1, "q")
+        c1 = ClassicalRegister(0, "c1")
+        c2 = ClassicalRegister(1, "c2")
+        qc = QuantumCircuit(q, c1, c2)
+        qc.h(0)
+        qc.measure(0, 0)
+
+        sampler = BackendSamplerV2(backend=backend, options=self._options)
+        result = sampler.run([qc], shots=self._shots).result()
+        self.assertEqual(result[0].data.c1.array.shape, (self._shots, 0))
+
 
 if __name__ == "__main__":
     unittest.main()