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

test_resource: cover setting shots and memory options #92

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
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
112 changes: 112 additions & 0 deletions test/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import json
import math
import uuid
from contextlib import nullcontext
from typing import Any, ContextManager
from unittest import mock

import httpx
Expand Down Expand Up @@ -364,3 +366,113 @@ def test_offline_simulator_detects_invalid_circuits(

with pytest.raises(ValueError, match="^Operation 'h' not in basis gate set"):
offline_simulator_no_noise.run(qc)


def test_offline_simulator_propagate_shots_option(
offline_simulator_no_noise: MockSimulator,
) -> None:
"""Check various ways of configuring the number of repetitions."""
qc = qiskit.transpile(random_circuit(2), offline_simulator_no_noise)

default_shots = sum(offline_simulator_no_noise.run(qc).result().get_counts().values())
assert default_shots == AQTOptions().shots

# TODO: use annotated-types to get unified access to upper bound
shots = min(default_shots + 40, AQTOptions.__fields__["shots"].field_info.le)
assert shots != default_shots

# configure shots in AQTResource.run
shots_run = sum(offline_simulator_no_noise.run(qc, shots=shots).result().get_counts().values())
assert shots_run == shots

# configure shots in qiskit.execute
shots_execute = sum(
qiskit.execute(qc, offline_simulator_no_noise, shots=shots).result().get_counts().values()
)
assert shots_execute == shots

# configure shots in resource options
offline_simulator_no_noise.options.shots = shots
shots_options = sum(offline_simulator_no_noise.run(qc).result().get_counts().values())
assert shots_options == shots

# qiskit.execute overrides resource options
shots_override = min(shots + 40, AQTOptions.__fields__["shots"].field_info.le)
assert shots_override != shots
assert shots_override != default_shots
assert offline_simulator_no_noise.options.shots != shots_override
shots = sum(
qiskit.execute(qc, offline_simulator_no_noise, shots=shots_override)
.result()
.get_counts()
.values()
)
assert shots == shots_override


@pytest.mark.parametrize(
("memory", "context"),
[(True, nullcontext()), (False, pytest.raises(qiskit.QiskitError, match="No memory"))],
)
def test_offline_simulator_run_propagate_memory_option(
memory: bool,
context: ContextManager[Any],
offline_simulator_no_noise: MockSimulator,
) -> None:
"""Check that the memory option can be set on `AQTResource.run`."""
qc = qiskit.transpile(random_circuit(2), offline_simulator_no_noise)
default_shots = AQTOptions().shots

result = offline_simulator_no_noise.run(qc, memory=memory).result()
with context:
assert len(result.get_memory()) == default_shots


@pytest.mark.parametrize(
("memory", "context"),
[(True, nullcontext()), (False, pytest.raises(qiskit.QiskitError, match="No memory"))],
)
def test_offline_simulator_execute_propagate_memory_option(
memory: bool, context: ContextManager[Any], offline_simulator_no_noise: MockSimulator
) -> None:
"""Check that the memory option can be set in `qiskit.execute`."""
qc = random_circuit(2)
default_shots = AQTOptions().shots

result = qiskit.execute(qc, offline_simulator_no_noise, memory=memory).result()
with context:
assert len(result.get_memory()) == default_shots


@pytest.mark.parametrize(
("memory", "context"),
[(True, nullcontext()), (False, pytest.raises(qiskit.QiskitError, match="No memory"))],
)
def test_offline_simulator_resource_propagate_memory_option(
memory: bool, context: ContextManager[Any], offline_simulator_no_noise: MockSimulator
) -> None:
"""Check that the memory option can be set as resource option."""
qc = qiskit.transpile(random_circuit(2), offline_simulator_no_noise)
default_shots = AQTOptions().shots

offline_simulator_no_noise.options.memory = memory
result = offline_simulator_no_noise.run(qc).result()
with context:
assert len(result.get_memory()) == default_shots


@pytest.mark.parametrize(
("memory", "context"),
[(True, nullcontext()), (False, pytest.raises(qiskit.QiskitError, match="No memory"))],
)
def test_offline_simulator_execute_override_memory_option(
memory: bool, context: ContextManager[Any], offline_simulator_no_noise: MockSimulator
) -> None:
"""Check that setting `memory` through `qiskit.execute` overrides the resource options."""
qc = random_circuit(2)
default_shots = AQTOptions().shots

offline_simulator_no_noise.options.memory = not memory
result = qiskit.execute(qc, offline_simulator_no_noise, memory=memory).result()
with context:
assert len(result.get_memory()) == default_shots