Skip to content

Commit

Permalink
Monkey-patching to get tests to pass for sampler update (#1473)
Browse files Browse the repository at this point in the history
These changes could be merged into #1470 (PR could be redirected to that
branch). For now I am just opening a PR to see how the tests run in CI.
  • Loading branch information
wshanks authored Oct 15, 2024
1 parent fcb43dc commit 325f99d
Show file tree
Hide file tree
Showing 31 changed files with 435 additions and 69 deletions.
7 changes: 7 additions & 0 deletions docs/howtos/artifacts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ Viewing artifacts
Here we run a parallel experiment consisting of two :class:`.T1` experiments in parallel and then view the output
artifacts as a list of :class:`.ArtifactData` objects accessed by :meth:`.ExperimentData.artifacts`:

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_ibm_runtime.fake_provider import FakePerth
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/characterization/t1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ for qubit 0.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

import numpy as np
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/characterization/t2ramsey.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ pure T1/T2 relaxation noise model.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

# A T1 simulator
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/characterization/tphi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ From the :math:`T_1` and :math:`T_2` estimates, we compute the results for
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

import numpy as np
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/measurement/readout_mitigation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ experiments to generate the corresponding mitigators.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

import numpy as np
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/measurement/restless_measurements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ they use always starts with the qubits in the ground state.
This tutorial requires the :external+qiskit_ibm_runtime:doc:`qiskit-ibm-runtime <index>` package to model a
backend. You can install it with ``python -m pip install qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_ibm_runtime.fake_provider import FakePerth
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/verification/quantum_volume.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ z_value = 2), and at least 100 trials have been ran.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_experiments.framework import BatchExperiment
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/verification/randomized_benchmarking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ explanation on the RB method, which is based on Refs. [1]_ [2]_.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

import numpy as np
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/verification/state_tomography.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ complete basis of measurement operators.

We first initialize a simulator to run the experiments on.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_aer import AerSimulator
Expand Down
7 changes: 7 additions & 0 deletions docs/tutorials/custom_experiment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,13 @@ To test our code, we first simulate a noisy backend with asymmetric readout erro
You can install it with ``python -m pip install qiskit-aer``.


.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_aer import AerSimulator, noise
Expand Down
7 changes: 7 additions & 0 deletions docs/tutorials/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ backend, real or simulated, that you can access through Qiskit.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_ibm_runtime.fake_provider import FakePerth
Expand Down
2 changes: 2 additions & 0 deletions qiskit_experiments/framework/base_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ def _run_jobs(
raise QiskitError("Only meas level 1 + 2 supported by sampler")
if "noise_model" in run_options:
sampler.options.simulator.noise_model = run_options["noise_model"]
if "seed_simulator" in run_options:
sampler.options.simulator.seed_simulator = run_options["seed_simulator"]

if run_options.get("shots") is not None:
sampler.options.default_shots = run_options.get("shots")
Expand Down
39 changes: 23 additions & 16 deletions qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,13 @@ def completion_times(self) -> Dict[str, datetime]:
if job is not None:
if hasattr(job, "time_per_step") and "COMPLETED" in job.time_per_step():
job_times[job_id] = job.time_per_step().get("COMPLETED")
elif (execution := job.result().metadata.get("execution")) and "execution_spans" in execution:
elif (
execution := job.result().metadata.get("execution")
) and "execution_spans" in execution:
job_times[job_id] = execution["execution_spans"].stop
elif (client := getattr(job, "_api_client", None)) and hasattr(client, "job_metadata"):
elif (client := getattr(job, "_api_client", None)) and hasattr(
client, "job_metadata"
):
metadata = client.job_metadata(job.job_id())
finished = metadata.get("timestamps", {}).get("finished", {})
if finished:
Expand Down Expand Up @@ -1041,11 +1045,20 @@ def _add_result_data(self, result: Result, job_id: Optional[str] = None) -> None
testres = SamplerPubResult(result[i].data, result[i].metadata)
data["job_id"] = job_id
if testres.data:
inner_data = testres.data[next(iter(testres.data))]
if not testres.data:
joined_data = testres.join_data()
outer_shape = testres.data.shape
if outer_shape:
raise QiskitError(
f"Outer PUB dimensions {outer_shape} found in result. "
"Only unparameterized PUBs are currently supported by "
"qiskit-experiments."
)
else:
joined_data = None
if joined_data is None:
# No data, usually this only happens in tests
pass
elif isinstance(inner_data, BitArray):
elif isinstance(joined_data, BitArray):
# bit results so has counts
data["meas_level"] = 2
# The sampler result always contains bitstrings. At
Expand All @@ -1059,15 +1072,9 @@ def _add_result_data(self, result: Result, job_id: Optional[str] = None) -> None
data["counts"] = testres.join_data(testres.data.keys()).get_counts()
data["memory"] = testres.join_data(testres.data.keys()).get_bitstrings()
# number of shots
data["shots"] = inner_data.num_shots
elif isinstance(inner_data, np.ndarray):
data["shots"] = joined_data.num_shots
elif isinstance(joined_data, np.ndarray):
data["meas_level"] = 1
joined_data = testres.join_data(testres.data.keys())
# Need to split off the pub dimension representing
# different parameter binds which is trivial because
# qiskit-experiments does not support parameter binding
# to pubs currently.
joined_data = joined_data[0]
if joined_data.ndim == 1:
data["meas_return"] = "avg"
# TODO: we either need to track shots in the
Expand All @@ -1085,15 +1092,15 @@ def _add_result_data(self, result: Result, job_id: Optional[str] = None) -> None
data["memory"][:, :, 0] = np.real(joined_data)
data["memory"][:, :, 1] = np.imag(joined_data)
else:
raise QiskitError(f"Unexpected result format: {type(inner_data)}")
raise QiskitError(f"Unexpected result format: {type(joined_data)}")

# Some Sampler implementations remove the circuit metadata
# which some experiment Analysis classes need. Here we try
# to put it back from the circuits themselves.
if "circuit_metadata" in testres.metadata:
data["metadata"] = testres.metadata["circuit_metadata"]
else:
corresponding_pub = job.inputs["pubs"][i]
elif self._jobs[job_id] is not None:
corresponding_pub = self._jobs[job_id].inputs["pubs"][i]
circuit = corresponding_pub[0]
data["metadata"] = circuit.metadata

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class CorrelatedReadoutError(BaseExperiment):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
from qiskit.providers.fake_provider import GenericBackendV2
from qiskit_aer import AerSimulator
Expand Down
4 changes: 4 additions & 0 deletions qiskit_experiments/library/characterization/fine_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class FineFrequency(BaseExperiment):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
# backend
from qiskit_ibm_runtime.fake_provider import FakePerth
from qiskit_aer import AerSimulator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class LocalReadoutError(BaseExperiment):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
# backend
from qiskit_aer import AerSimulator
from qiskit_ibm_runtime.fake_provider import FakePerth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class MultiStateDiscrimination(BaseExperiment):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
# backend
from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend
backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=False, seed=199)
Expand Down
4 changes: 4 additions & 0 deletions qiskit_experiments/library/characterization/ramsey_xy.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class RamseyXY(BaseExperiment, RestlessMixin):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
# backend
from qiskit_aer import AerSimulator
from qiskit_ibm_runtime.fake_provider import FakePerth
Expand Down
4 changes: 4 additions & 0 deletions qiskit_experiments/library/characterization/t1.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class T1(BaseExperiment):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
# backend
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
from qiskit_aer import AerSimulator
Expand Down
4 changes: 4 additions & 0 deletions qiskit_experiments/library/characterization/t2ramsey.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class T2Ramsey(BaseExperiment):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
# backend
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
from qiskit_aer import AerSimulator
Expand Down
4 changes: 4 additions & 0 deletions qiskit_experiments/library/characterization/tphi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class Tphi(BatchExperiment):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
# backend
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
from qiskit_aer import AerSimulator
Expand Down
4 changes: 4 additions & 0 deletions qiskit_experiments/library/characterization/zz_ramsey.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class ZZRamsey(BaseExperiment):
.. jupyter-execute::
:hide-code:
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()
# backend
from qiskit_ibm_runtime.fake_provider import FakePerth
from qiskit_aer import AerSimulator
Expand Down
3 changes: 2 additions & 1 deletion qiskit_experiments/test/fake_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def _default_options(cls):
def target(self) -> Target:
return self._target

def run(self, run_input, shots=100, **options):
def run(self, run_input, **options):
shots = options.get("shots", 100)
if not isinstance(run_input, list):
run_input = [run_input]
results = [
Expand Down
9 changes: 6 additions & 3 deletions qiskit_experiments/test/mock_iq_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def __init__(
backend_version: str = None,
**fields,
):
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support

patch_sampler_test_support()

super().__init__(provider, name, description, online_date, backend_version, **fields)

backend_v1 = FakeOpenPulse2Q()
Expand Down Expand Up @@ -459,9 +464,7 @@ def _generate_data(
run_result["counts"] = counts
if meas_return == "single" or self.options.get("memory"):
run_result["memory"] = [
format(result, "x")
for result, num in enumerate(results)
for _ in range(num)
format(result, "x") for result, num in enumerate(results) for _ in range(num)
]
else:
# Phase has meaning only for IQ shot, so we calculate it here
Expand Down
Loading

0 comments on commit 325f99d

Please sign in to comment.