Skip to content

Commit

Permalink
Merge branch 'main' into runtime-job-usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kt474 authored Sep 16, 2024
2 parents 395035b + f3b6abe commit cbb7cb8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
12 changes: 11 additions & 1 deletion qiskit_ibm_runtime/base_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ def _get_mode_service_backend(
elif get_cm_session():
mode = get_cm_session()
service = mode.service # type: ignore
backend = service.backend(name=mode.backend(), instance=mode._instance) # type: ignore
try:
backend = service.backend(
name=mode.backend(), # type: ignore
instance=mode._instance, # type: ignore
use_fractional_gates=mode._backend.options.use_fractional_gates, # type: ignore
)
except (AttributeError, TypeError):
backend = service.backend(
name=mode.backend(), # type: ignore
instance=mode._instance, # type: ignore
)
return mode, service, backend # type: ignore
else:
raise ValueError("A backend or session must be specified.")
Expand Down
2 changes: 2 additions & 0 deletions qiskit_ibm_runtime/fake_provider/local_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def _run(
"Passing a backend name is not supported in local testing mode. "
"Please pass a backend instance."
)
if "resilience_level" in inputs:
warnings.warn("The resilience_level option has no effect in local testing mode.")

inputs = copy.deepcopy(inputs)

Expand Down
1 change: 1 addition & 0 deletions release-notes/unreleased/1922.but.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug that we cannot run primitives in the session context with fractional gates.
12 changes: 10 additions & 2 deletions test/integration/test_noise_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ def test_learner_plus_estimator(self, service): # pylint: disable=unused-argume
options.resilience.zne.amplifier = "pea"
options.resilience.layer_noise_learning.layer_pair_depths = [0, 1]

pubs = [(c, "Z" * c.num_qubits) for c in self.circuits]
circuit = QuantumCircuit(3)
circuit.ecr(0, 1)
circuit.ecr(1, 2)
circuit.ecr(1, 2)
circuit.ecr(0, 1)
circuit.ecr(0, 1)
circuit.ecr(0, 1)

pubs = [(circuit, "Z" * circuit.num_qubits)]

with Session(service, backend) as session:
learner = NoiseLearner(mode=session, options=options)
Expand All @@ -120,7 +128,7 @@ def test_learner_plus_estimator(self, service): # pylint: disable=unused-argume
self.assertEqual(len(noise_model), 3)

estimator = EstimatorV2(mode=session, options=options)
estimator.options.resilience.layer_noise_model = noise_model
estimator.options.resilience.layer_noise_model = noise_model.data

estimator_job = estimator.run(pubs)
result = estimator_job.result()
Expand Down
15 changes: 14 additions & 1 deletion test/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
from unittest.mock import MagicMock, patch

from qiskit_ibm_runtime.fake_provider import FakeManila
from qiskit_ibm_runtime import Session
from qiskit_ibm_runtime import Session, SamplerV2
from qiskit_ibm_runtime.ibm_backend import IBMBackend
from qiskit_ibm_runtime.exceptions import IBMRuntimeError
from qiskit_ibm_runtime.utils.default_session import _DEFAULT_SESSION

from .mock.fake_runtime_service import FakeRuntimeService
from .mock.fake_api_backend import FakeApiBackendSpecs
from ..ibm_test_case import IBMTestCase
from ..utils import get_mocked_backend

Expand Down Expand Up @@ -160,3 +161,15 @@ def test_correct_execution_mode(self):
_ = FakeRuntimeService(channel="ibm_quantum", token="abc")
session = Session(backend="common_backend")
self.assertEqual(session.details()["mode"], "dedicated")

def test_cm_session_fractional(self):
"""Test instantiating primitive inside session context manager with the fractional optin."""
service = FakeRuntimeService(
channel="ibm_quantum",
token="abc",
backend_specs=[FakeApiBackendSpecs(backend_name="FakeFractionalBackend")],
)
backend = service.backend("fake_fractional", use_fractional_gates=True)
with Session(backend=backend) as _:
primitive = SamplerV2()
self.assertTrue(primitive._backend.options.use_fractional_gates)

0 comments on commit cbb7cb8

Please sign in to comment.