Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an error check for Sampler #8678

Merged
merged 6 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions qiskit/primitives/base_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ def run(
f"the number of parameters ({circuit.num_parameters}) for the {i}-th circuit."
)

for i, circuit in enumerate(circuits):
if circuit.num_clbits == 0:
raise QiskitError(
f"The {i}-th circuit does not have any classical bit. "
"Sampler requires classical bits."
t-imamichi marked this conversation as resolved.
Show resolved Hide resolved
)

return self._run(circuits, parameter_values, parameter_views, **run_options)

@abstractmethod
Expand Down
9 changes: 6 additions & 3 deletions test/python/primitives/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,17 @@ def test_run_errors(self):
qc1.measure_all()
qc2 = RealAmplitudes(num_qubits=1, reps=1)
qc2.measure_all()
qc3 = QuantumCircuit(1)

sampler = Sampler()
with self.assertRaises(QiskitError):
sampler.run([qc1], [[1e2]]).result()
sampler.run([qc1], [[1e2]])
with self.assertRaises(QiskitError):
sampler.run([qc2], [[]]).result()
sampler.run([qc2], [[]])
with self.assertRaises(QiskitError):
sampler.run([qc2], [[1e2]]).result()
sampler.run([qc2], [[1e2]])
with self.assertRaises(QiskitError):
result = sampler.run([qc3], [[]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, but it would be nice if these were wrapped in self.subTest so one failure doesn't stop the others from running, like

with self.subTest(msg="Circuit  has no parameters, but values passed"):
    with self.assertRaises(...):
      ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I updated it to use subTest.


def test_run_empty_parameter(self):
"""Test for empty parameter"""
Expand Down