Skip to content

Commit

Permalink
relax a check of measurements for Sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
t-imamichi committed Sep 8, 2022
1 parent e9913e8 commit 195aefd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
9 changes: 0 additions & 9 deletions qiskit/primitives/base_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
from qiskit.utils.deprecation import deprecate_arguments, deprecate_function

from .sampler_result import SamplerResult
from .utils import final_measurement_mapping


class BaseSampler(ABC):
Expand Down Expand Up @@ -393,14 +392,6 @@ def run(
"on the desired qubits."
)

mapping = final_measurement_mapping(circuit)
if set(range(circuit.num_clbits)) != set(mapping.values()):
raise QiskitError(
f"Some classical bits of the {i}-th circuit are not used for measurements."
f" the number of classical bits ({circuit.num_clbits}),"
f" the used classical bits ({set(mapping.values())})."
)

run_opts = copy(self.run_options)
run_opts.update_options(**run_options)

Expand Down
6 changes: 6 additions & 0 deletions qiskit/primitives/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ def _run(
def _preprocess_circuit(circuit: QuantumCircuit):
circuit = init_circuit(circuit)
q_c_mapping = final_measurement_mapping(circuit)
if set(range(circuit.num_clbits)) != set(q_c_mapping.values()):
raise QiskitError(
"Some classical bits are not used for measurements."
f" the number of classical bits ({circuit.num_clbits}),"
f" the used classical bits ({set(q_c_mapping.values())})."
)
c_q_mapping = sorted((c, q) for q, c in q_c_mapping.items())
qargs = [q for _, q in c_q_mapping]
circuit = circuit.remove_final_measurements(inplace=False)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
upgrade:
- |
Added some error checks to :meth:`~qiskit.primitives.BaseSampler.run`.
It raises an error if there is no classical bit or some classical bits
are not used for measurements.
Added a validation check to :meth:`~qiskit.primitives.BaseSampler.run`.
It raises an error if there is no classical bit.

0 comments on commit 195aefd

Please sign in to comment.