Skip to content

Commit

Permalink
Merge pull request #337 from QunaSys/qiskit-backend-max_shots
Browse files Browse the repository at this point in the history
Bug fix: Qiskit backend max shots
  • Loading branch information
dchung0741 authored May 17, 2024
2 parents 4854de4 + 2ff49a2 commit b7f5218
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/qiskit/quri_parts/qiskit/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,18 @@ def _set_max_shot_to_default() -> int:
if not isinstance(backend, (BackendV1, BackendV2)):
raise BackendError("Backend not supported.")

if isinstance(backend, BackendV1):
max_shots = getattr(
backend.configuration(), "max_shots", _set_max_shot_to_default()
)
if max_shots > 0:
return 1, max_shots

if hasattr(backend, "max_shots"):
return 1, backend.max_shots
max_shots = backend.max_shots
elif hasattr(backend, "configuration"):
return 1, getattr(
max_shots = getattr(
backend.configuration(), "max_shots", _set_max_shot_to_default()
)
else:
return 1, _set_max_shot_to_default()
max_shots = _set_max_shot_to_default()

if max_shots > 0:
return 1, max_shots
return 1, _set_max_shot_to_default()


def get_job_mapper_and_circuit_transpiler(
Expand Down
107 changes: 107 additions & 0 deletions packages/qiskit/tests/qiskit/backend/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import Mock

import pytest
from qiskit.providers.backend import Backend, BackendV1, BackendV2
from qiskit.providers.models import QasmBackendConfiguration
from qiskit_ibm_runtime import IBMBackend

from quri_parts.backend import BackendError
from quri_parts.circuit import NonParametricQuantumCircuit, QuantumCircuit
from quri_parts.qiskit.backend import (
QiskitSavedDataSamplingJob,
QiskitSavedDataSamplingResult,
convert_qiskit_sampling_count_to_qp_sampling_count,
distribute_backend_shots,
get_backend_min_max_shot,
get_job_mapper_and_circuit_transpiler,
)
from quri_parts.qiskit.backend.utils import DEFAULT_MAX_SHOT


class TestDistributeBackendShots:
Expand Down Expand Up @@ -121,3 +131,100 @@ def test_convert_qiskit_sampling_count_to_qp_sampling_count() -> None:
{"00": 1, "01": 2, "10": 3, "11": 4}
)
assert converted_sampling_count == {0: 1, 1: 2, 2: 3, 3: 4}


def test_get_backend_min_max_shot() -> None:
backend = Mock(spec=Backend)
with pytest.raises(BackendError, match="Backend not supported"):
get_backend_min_max_shot(backend)

backend = Mock(spec=BackendV1)
conf = Mock(spec=QasmBackendConfiguration)
conf.max_shots = int(1e3)
backend.configuration.return_value = conf
min_shots, max_shots = get_backend_min_max_shot(backend)
assert min_shots == 1
assert max_shots == int(1e3)

backend = Mock(spec=BackendV1)
conf = Mock(spec=QasmBackendConfiguration)
backend.configuration.return_value = conf
with pytest.warns(
UserWarning,
match=(
"No max_shots setting is found. "
"The max shot is set to default value 1000000"
),
):
min_shots, max_shots = get_backend_min_max_shot(backend)
assert min_shots == 1
assert max_shots == DEFAULT_MAX_SHOT

backend = Mock(spec=BackendV1)
conf = Mock(spec=QasmBackendConfiguration)
conf.max_shots = 0
backend.configuration.return_value = conf
with pytest.warns(
UserWarning,
match=(
"No max_shots setting is found. "
"The max shot is set to default value 1000000"
),
):
min_shots, max_shots = get_backend_min_max_shot(backend)
assert min_shots == 1
assert max_shots == DEFAULT_MAX_SHOT

backend = Mock(spec=BackendV2)
backend.max_shots = 10
min_shots, max_shots = get_backend_min_max_shot(backend)
assert min_shots == 1
assert max_shots == 10

backend = Mock(spec=BackendV2)
with pytest.warns(
UserWarning,
match=(
"No max_shots setting is found. "
"The max shot is set to default value 1000000"
),
):
min_shots, max_shots = get_backend_min_max_shot(backend)
assert min_shots == 1
assert max_shots == DEFAULT_MAX_SHOT

backend = Mock(spec=BackendV2)
backend.max_shots = 0
with pytest.warns(
UserWarning,
match=(
"No max_shots setting is found. "
"The max shot is set to default value 1000000"
),
):
min_shots, max_shots = get_backend_min_max_shot(backend)
assert min_shots == 1
assert max_shots == DEFAULT_MAX_SHOT

backend = Mock(spec=IBMBackend)
conf = Mock(spec=QasmBackendConfiguration)
conf.max_shots = int(1e3)
backend.configuration.return_value = conf
min_shots, max_shots = get_backend_min_max_shot(backend)
assert min_shots == 1
assert max_shots == 1000

backend = Mock(spec=IBMBackend)
conf = Mock(spec=QasmBackendConfiguration)
conf.max_shots = 0
backend.configuration.return_value = conf
with pytest.warns(
UserWarning,
match=(
"No max_shots setting is found. "
"The max shot is set to default value 1000000"
),
):
min_shots, max_shots = get_backend_min_max_shot(backend)
assert min_shots == 1
assert max_shots == DEFAULT_MAX_SHOT

1 comment on commit b7f5218

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.