Skip to content

Commit

Permalink
Fix BackendSamplerV2 when used with an empty classical register (#1558
Browse files Browse the repository at this point in the history
)

This is a port of Qiskit/qiskit#12055
to this repository, since this repository has its own fork of
`BackendSamplerV2` until Qiskit 1.1 is released (#1523).
  • Loading branch information
garrison authored Mar 25, 2024
1 parent ce86c0d commit 45a49bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion qiskit_ibm_runtime/qiskit/primitives/backend_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,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,
Expand Down
15 changes: 15 additions & 0 deletions test/unit/qiskit/test_backend_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,21 @@ def test_no_cregs(self, backend):
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()

0 comments on commit 45a49bc

Please sign in to comment.