From 0ddfc917aa302957a0cb59d4f5ea384c62b65ead Mon Sep 17 00:00:00 2001 From: Naohito Nakazawa <117684192+Naohnakazawa@users.noreply.github.com> Date: Fri, 10 May 2024 11:46:44 +0900 Subject: [PATCH] Add minimal working code expample in experiment API docs (#1433) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Summary This change adds minimal working code examples to the API pages for each experiment,especially in characterizaion experiments. The minimal working code example is a code snippet which users can copy and paste to run the experiment. ### Details and comments - This change solves the `issue#1238` (See also the `issue#1221`). - In the API pages, the documentation for each experiment has been updated with its code example. Users are required to specify a backend in the code example. As a default, the backend is a simulator such as `GenericBackendV2(num_qubits=27)`. - In some experiments, `CorrelatedReadoutError` `ResonatorSpectroscopy` `CrossResonanceHamiltonian` and `EchoedCrossResonanceHamiltonian`, the backend is an IBM Quantum real device due to the `backend.defaults()` dependence of the code example. In these cases, the code example explicitly imports the module `qiskit-ibm-provider` in order to retrieve the job on IBM Quantum real devices. Therefore, `requirements-dev.txt` has been updated to add the dependency on `qiskit-ibm-provider` for running tests and building documentation. ### PR checklist (template) - [x] I have read the contributing guide `CONTRIBUTING.md`. - [ ] I have added the tests to cover my changes. - [x] I have updated the documentation accordingly. - [x] I have added a release note file using `reno` if this change needs to be documented in the release notes. ### PR checklist (additional in CONTRIBUTING.md) - [x] The code follows the code style of the project and successfully passes the tests. - [x] The API documentation has been updated accordingly. - [x] You have updated the relevant documentation or written new docs. In case the PR needs to be merged without delay (e.g. for a high priority fix), open an issue for updating or adding the documentation later. - [] You've added tests that cover the changes you've made, if relevant. - [] If your change has an end user facing impact (new feature, deprecation, removal, etc.), you've added or updated a reno release note for that change and tagged the PR for the changelog. - [x] If your code requires a change to dependencies, you've updated the corresponding requirements file: `requirements.txt` for core dependencies, `requirements-extras.txt` for dependencies for optional features, and `requirements-dev.txt` for dependencies required for running tests and building documentation. --------- Co-authored-by: 中澤 直仁 --- .../correlated_readout_error.py | 21 ++++++ .../library/characterization/drag.py | 28 ++++++++ .../characterization/ef_spectroscopy.py | 25 +++++++ .../characterization/fine_amplitude.py | 62 ++++++++++++++--- .../library/characterization/fine_drag.py | 69 ++++++++++++++++++- .../characterization/fine_frequency.py | 25 +++++++ .../library/characterization/half_angle.py | 18 +++++ .../characterization/local_readout_error.py | 22 ++++++ .../multi_state_discrimination.py | 18 +++++ .../characterization/qubit_spectroscopy.py | 27 ++++++++ .../library/characterization/rabi.py | 52 ++++++++++++++ .../library/characterization/ramsey_xy.py | 22 ++++++ .../library/characterization/readout_angle.py | 18 +++++ .../library/characterization/t1.py | 21 ++++++ .../library/characterization/t2hahn.py | 32 +++++++++ .../library/characterization/t2ramsey.py | 30 ++++++++ .../library/characterization/tphi.py | 37 ++++++++++ .../library/characterization/zz_ramsey.py | 27 ++++++++ ...rization-experiments-e77d4d26c6b49694.yaml | 8 +++ 19 files changed, 549 insertions(+), 13 deletions(-) create mode 100644 releasenotes/notes/add-examples-to-characterization-experiments-e77d4d26c6b49694.yaml diff --git a/qiskit_experiments/library/characterization/correlated_readout_error.py b/qiskit_experiments/library/characterization/correlated_readout_error.py index 4fd7742a62..617feb523f 100644 --- a/qiskit_experiments/library/characterization/correlated_readout_error.py +++ b/qiskit_experiments/library/characterization/correlated_readout_error.py @@ -73,6 +73,27 @@ class CorrelatedReadoutError(BaseExperiment): # section: analysis_ref :class:`CorrelatedReadoutErrorAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + from qiskit.providers.fake_provider import GenericBackendV2 + from qiskit_aer import AerSimulator + + num_qubits=5 + backend = AerSimulator.from_backend(GenericBackendV2(num_qubits=num_qubits, + calibrate_instructions=True)) + + .. jupyter-execute:: + + from qiskit_experiments.library import CorrelatedReadoutError + + exp = CorrelatedReadoutError(physical_qubits=(0,1,2), backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: manual :doc:`/manuals/measurement/readout_mitigation` diff --git a/qiskit_experiments/library/characterization/drag.py b/qiskit_experiments/library/characterization/drag.py index 6a338212e8..d7ede194ca 100644 --- a/qiskit_experiments/library/characterization/drag.py +++ b/qiskit_experiments/library/characterization/drag.py @@ -64,6 +64,34 @@ class RoughDrag(BaseExperiment, RestlessMixin): # section: analysis_ref :class:`DragCalAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=False, seed=101) + + .. jupyter-execute:: + + import numpy as np + from qiskit import pulse + from qiskit.circuit import Parameter + from qiskit_experiments.library import RoughDrag + + with pulse.build() as build_sched: + pulse.play(pulse.Drag(160, 0.50, 40, Parameter("beta")), pulse.DriveChannel(0)) + + exp = RoughDrag(physical_qubits=(0,), + schedule=build_sched, + betas = np.linspace(-4, 4, 51), + backend=backend,) + exp.set_experiment_options(reps=[3, 5, 7]) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: reference .. ref_arxiv:: 1 1011.1949 .. ref_arxiv:: 2 0901.0534 diff --git a/qiskit_experiments/library/characterization/ef_spectroscopy.py b/qiskit_experiments/library/characterization/ef_spectroscopy.py index 7e07d93cd6..0b28c6636f 100644 --- a/qiskit_experiments/library/characterization/ef_spectroscopy.py +++ b/qiskit_experiments/library/characterization/ef_spectroscopy.py @@ -34,7 +34,32 @@ class EFSpectroscopy(QubitSpectroscopy): └───┘└────────────┘ ░ └╥┘ measure: 1/═══════════════════════╩═ 0 + # section: example + .. jupyter-execute:: + :hide-code: + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, noise=False, seed=100) + + .. jupyter-execute:: + + import numpy as np + from qiskit_experiments.library.characterization import EFSpectroscopy + + qubit = 0 + freq01_estimate = backend.defaults().qubit_freq_est[qubit] + frequencies = np.linspace(freq01_estimate-15e6, freq01_estimate+15e6, 51) + + exp = EFSpectroscopy(physical_qubits = (0,), + frequencies = frequencies, + backend = backend, + ) + exp.set_experiment_options(amp=0.005) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ def __init__( diff --git a/qiskit_experiments/library/characterization/fine_amplitude.py b/qiskit_experiments/library/characterization/fine_amplitude.py index 69232e2e48..d5328f6a76 100644 --- a/qiskit_experiments/library/characterization/fine_amplitude.py +++ b/qiskit_experiments/library/characterization/fine_amplitude.py @@ -62,21 +62,25 @@ class FineAmplitude(BaseExperiment, RestlessMixin): in this case. # section: example + .. jupyter-execute:: + :hide-code: - The steps to run a fine amplitude experiment are + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e6, noise=True, seed=185) - .. code-block:: python + .. jupyter-execute:: + + import numpy as np + from qiskit.circuit.library import XGate + from qiskit_experiments.library import FineAmplitude - qubit = 3 - amp_cal = FineAmplitude([qubit], SXGate()) - amp_cal.set_experiment_options( - angle_per_gate=np.pi/2, - phase_offset=np.pi - ) - amp_cal.run(backend) + exp = FineAmplitude(physical_qubits=(0,), gate=XGate(), backend=backend) + exp.analysis.set_options(fixed_parameters={"angle_per_gate" : np.pi, "phase_offset" : np.pi}) - Note that there are subclasses of :class:`FineAmplitude` such as :class:`FineSXAmplitude` - that set the appropriate options for specific gates by default. + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) # section: analysis_ref :class:`FineAmplitudeAnalysis` @@ -251,6 +255,24 @@ class FineXAmplitude(FineAmplitude): :class:`FineXAmplitude` is a subclass of :class:`FineAmplitude` and is used to set the appropriate values for the default options. + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=True, seed=198) + + .. jupyter-execute:: + + from qiskit_experiments.library import FineXAmplitude + + exp = FineXAmplitude(physical_qubits=(0,), backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ def __init__(self, physical_qubits: Sequence[int], backend: Optional[Backend] = None): @@ -289,6 +311,24 @@ class FineSXAmplitude(FineAmplitude): :class:`FineSXAmplitude` is a subclass of :class:`FineAmplitude` and is used to set the appropriate values for the default options. + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=True, seed=198) + + .. jupyter-execute:: + + from qiskit_experiments.library import FineSXAmplitude + + exp = FineSXAmplitude(physical_qubits=(0,), backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ def __init__(self, physical_qubits: Sequence[int], backend: Optional[Backend] = None): diff --git a/qiskit_experiments/library/characterization/fine_drag.py b/qiskit_experiments/library/characterization/fine_drag.py index 24dafb060c..b9f9229074 100644 --- a/qiskit_experiments/library/characterization/fine_drag.py +++ b/qiskit_experiments/library/characterization/fine_drag.py @@ -127,6 +127,25 @@ class FineDrag(BaseExperiment, RestlessMixin): # section: analysis_ref :class:`.ErrorAmplificationAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=False, seed=199) + + .. jupyter-execute:: + + from qiskit.circuit.library import XGate + from qiskit_experiments.library.characterization import FineDrag + + exp = FineDrag(physical_qubits=(0,), gate=XGate(), backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: reference .. ref_arxiv:: 1 1612.00858 .. ref_arxiv:: 2 1011.1949 @@ -235,7 +254,26 @@ def _metadata(self): class FineXDrag(FineDrag): - """Class to fine characterize the DRAG parameter of an X gate.""" + """Class to fine characterize the DRAG parameter of an X gate. + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=False, seed=199) + + .. jupyter-execute:: + + from qiskit_experiments.library.characterization import FineXDrag + + exp = FineXDrag(physical_qubits=(0,), backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + """ def __init__(self, physical_qubits: Sequence[int], backend: Optional[Backend] = None): """Initialize the experiment.""" @@ -260,7 +298,34 @@ def _pre_circuit() -> QuantumCircuit: class FineSXDrag(FineDrag): - """Class to fine characterize the DRAG parameter of an :math:`SX` gate.""" + """Class to fine characterize the DRAG parameter of an :math:`SX` gate. + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=False, seed=199) + + .. jupyter-execute:: + + import numpy as np + from qiskit_experiments.library.characterization import FineSXDrag + + exp = FineSXDrag(physical_qubits=(0,), backend=backend) + exp.analysis.set_options(normalization= True, + fixed_parameters={ + "angle_per_gate" : 0.0, + "phase_offset" : np.pi/2, + "amp" : 0.6 + }, + ) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + """ def __init__(self, physical_qubits: Sequence[int], backend: Optional[Backend] = None): """Initialize the experiment.""" diff --git a/qiskit_experiments/library/characterization/fine_frequency.py b/qiskit_experiments/library/characterization/fine_frequency.py index a1a913e7f8..ce39417d67 100644 --- a/qiskit_experiments/library/characterization/fine_frequency.py +++ b/qiskit_experiments/library/characterization/fine_frequency.py @@ -48,6 +48,31 @@ class FineFrequency(BaseExperiment): 0 # section: analysis_ref :class:`~qiskit_experiments.curve_analysis.ErrorAmplificationAnalysis` + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_ibm_runtime.fake_provider import FakePerth + from qiskit_aer import AerSimulator + backend =AerSimulator.from_backend(FakePerth()) + + .. jupyter-execute:: + + from qiskit_experiments.library.characterization import FineFrequency + + repetitions = list(range(40)) + + exp = FineFrequency((0,), + delay_duration=320, + backend=backend, + repetitions=repetitions) + exp.set_transpile_options(optimization_level=0, basis_gates=['sx', 'rz', 'delay']) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ def __init__( diff --git a/qiskit_experiments/library/characterization/half_angle.py b/qiskit_experiments/library/characterization/half_angle.py index c41adb677d..ccd94cfe46 100644 --- a/qiskit_experiments/library/characterization/half_angle.py +++ b/qiskit_experiments/library/characterization/half_angle.py @@ -89,6 +89,24 @@ class HalfAngle(BaseExperiment): # section: analysis_ref :class:`.ErrorAmplificationAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=False, seed=199) + + .. jupyter-execute:: + + from qiskit_experiments.library.characterization import HalfAngle + + exp = HalfAngle((0,), backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: reference .. ref_arxiv:: 1 1504.06597 """ diff --git a/qiskit_experiments/library/characterization/local_readout_error.py b/qiskit_experiments/library/characterization/local_readout_error.py index dfc893e7ba..8830f2270f 100644 --- a/qiskit_experiments/library/characterization/local_readout_error.py +++ b/qiskit_experiments/library/characterization/local_readout_error.py @@ -62,6 +62,28 @@ class LocalReadoutError(BaseExperiment): # section: analysis_ref :class:`LocalReadoutErrorAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_aer import AerSimulator + from qiskit_ibm_runtime.fake_provider import FakePerth + backend = AerSimulator.from_backend(FakePerth()) + + .. jupyter-execute:: + + from qiskit_experiments.library import LocalReadoutError + + qubits = list(range(4)) + exp = LocalReadoutError(physical_qubits=qubits, backend=backend) + exp.analysis.set_options(plot=True) + exp.set_run_options(shots=10000) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: manual :doc:`/manuals/measurement/readout_mitigation` diff --git a/qiskit_experiments/library/characterization/multi_state_discrimination.py b/qiskit_experiments/library/characterization/multi_state_discrimination.py index ffea405a52..183d0707be 100644 --- a/qiskit_experiments/library/characterization/multi_state_discrimination.py +++ b/qiskit_experiments/library/characterization/multi_state_discrimination.py @@ -53,6 +53,24 @@ class MultiStateDiscrimination(BaseExperiment): # section: analysis_ref :class:`MultiStateDiscriminationAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=False, seed=199) + + .. jupyter-execute:: + + from qiskit_experiments.library.characterization import MultiStateDiscrimination + + exp=MultiStateDiscrimination((0,), backend=backend) + + exp_data=exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: reference `Qiskit Textbook\ `_ diff --git a/qiskit_experiments/library/characterization/qubit_spectroscopy.py b/qiskit_experiments/library/characterization/qubit_spectroscopy.py index 064e10ac6c..a915bb98f3 100644 --- a/qiskit_experiments/library/characterization/qubit_spectroscopy.py +++ b/qiskit_experiments/library/characterization/qubit_spectroscopy.py @@ -44,6 +44,33 @@ class QubitSpectroscopy(Spectroscopy): # section: analysis_ref :class:`~qiskit_experiments.curve_analysis.ResonanceAnalysis` + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=True, seed=199) + + .. jupyter-execute:: + + import numpy as np + from qiskit_experiments.library.characterization import QubitSpectroscopy + + qubit = 0 + freq01_estimate = backend.defaults().qubit_freq_est[qubit] + frequencies = np.linspace(freq01_estimate-15e6, freq01_estimate+15e6, 51) + + exp = QubitSpectroscopy(physical_qubits = (qubit,), + frequencies = frequencies, + backend = backend, + ) + exp.set_experiment_options(amp=0.005) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ __spec_gate_name__ = "Spec" diff --git a/qiskit_experiments/library/characterization/rabi.py b/qiskit_experiments/library/characterization/rabi.py index 3fb2a682d6..c8d997116c 100644 --- a/qiskit_experiments/library/characterization/rabi.py +++ b/qiskit_experiments/library/characterization/rabi.py @@ -56,6 +56,33 @@ class Rabi(BaseExperiment, RestlessMixin): # section: analysis_ref :class:`~qiskit_experiments.curve_analysis.OscillationAnalysis` + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-0.25e9, 1e9, 0.8e9, 1e4, noise=True, seed=199) + + .. jupyter-execute:: + + import numpy as np + from qiskit import pulse + from qiskit.circuit import Parameter + from qiskit_experiments.library import Rabi + + with pulse.build() as build_sched: + pulse.play(pulse.Gaussian(160, Parameter("amp"), 40), pulse.DriveChannel(0)) + + exp = Rabi(physical_qubits=(0,), + schedule=build_sched, + amplitudes=np.linspace(-0.1, 0.1, 21), + backend=backend,) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ __gate_name__ = "Rabi" @@ -199,7 +226,32 @@ class EFRabi(Rabi): └───┘└───────────┘ ░ └╥┘ measure: 1/══════════════════════╩═ 0 + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9, -0.25e9, 1e9, 0.8e9, 1e4, noise=True, seed=198) + + .. jupyter-execute:: + + import numpy as np + from qiskit import pulse + from qiskit.circuit import Parameter + from qiskit_experiments.library import EFRabi + + with pulse.build() as build_sched: + pulse.play(pulse.Gaussian(160, Parameter("amp"), sigma=40), pulse.DriveChannel(0)) + + exp = EFRabi(physical_qubits=(0,), + backend=backend, + schedule=build_sched, + amplitudes=np.linspace(-0.1, 0.1, 21),) + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ __outcome__ = "rabi_rate_12" diff --git a/qiskit_experiments/library/characterization/ramsey_xy.py b/qiskit_experiments/library/characterization/ramsey_xy.py index ccb1987481..40566580f9 100644 --- a/qiskit_experiments/library/characterization/ramsey_xy.py +++ b/qiskit_experiments/library/characterization/ramsey_xy.py @@ -80,6 +80,28 @@ class RamseyXY(BaseExperiment, RestlessMixin): # section: analysis_ref :class:`RamseyXYAnalysis` + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_aer import AerSimulator + from qiskit_ibm_runtime.fake_provider import FakePerth + + backend = AerSimulator.from_backend(FakePerth()) + + .. jupyter-execute:: + + import numpy as np + from qiskit_experiments.library.characterization import RamseyXY + + delays = np.linspace(0, 10.e-7, 101) + exp = RamseyXY((0,), backend=backend, delays=delays, osc_freq=2.0e6) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ @classmethod diff --git a/qiskit_experiments/library/characterization/readout_angle.py b/qiskit_experiments/library/characterization/readout_angle.py index b1c0c88a4f..3902c147e1 100644 --- a/qiskit_experiments/library/characterization/readout_angle.py +++ b/qiskit_experiments/library/characterization/readout_angle.py @@ -48,6 +48,24 @@ class ReadoutAngle(BaseExperiment): # section: analysis_ref :class:`ReadoutAngleAnalysis` + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.pulse_backend import SingleTransmonTestBackend + backend = SingleTransmonTestBackend(5.2e9,-.25e9, 1e9, 0.8e9, 1e4, noise=False, seed=199) + + .. jupyter-execute:: + + from qiskit_experiments.library.characterization import ReadoutAngle + + exp = ReadoutAngle((0,), backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ @classmethod diff --git a/qiskit_experiments/library/characterization/t1.py b/qiskit_experiments/library/characterization/t1.py index 0554599a25..6f3c02cfc1 100644 --- a/qiskit_experiments/library/characterization/t1.py +++ b/qiskit_experiments/library/characterization/t1.py @@ -37,6 +37,27 @@ class T1(BaseExperiment): # section: analysis_ref :class:`.T1Analysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_ibm_runtime.fake_provider import FakeManilaV2 + from qiskit_aer import AerSimulator + backend = AerSimulator.from_backend(FakeManilaV2()) + + .. jupyter-execute:: + + import numpy as np + from qiskit_experiments.library import T1 + + delays = np.arange(1.e-6, 300.e-6, 30.e-6) + exp = T1(physical_qubits=(0, ), delays=delays, backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: manual :doc:`/manuals/characterization/t1` diff --git a/qiskit_experiments/library/characterization/t2hahn.py b/qiskit_experiments/library/characterization/t2hahn.py index 57ce07d363..8add080bb9 100644 --- a/qiskit_experiments/library/characterization/t2hahn.py +++ b/qiskit_experiments/library/characterization/t2hahn.py @@ -59,6 +59,38 @@ class T2Hahn(BaseExperiment): # section: analysis_ref :class:`T2HahnAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_experiments.test.t2hahn_backend import T2HahnBackend + + conversion_factor = 1e-6 + estimated_t2hahn = 20*conversion_factor + backend = T2HahnBackend( + t2hahn=[estimated_t2hahn], + frequency=[100100], + readout0to1 = [0.02], + readout1to0 = [0.02], + ) + + .. jupyter-execute:: + + import numpy as np + from qiskit_experiments.library.characterization.t2hahn import T2Hahn + + delays = np.linspace(0, 50, 51)*1e-6 + + exp = T2Hahn(physical_qubits=(0, ), + delays=delays, + backend=backend) + exp.analysis.set_options(p0=None, plot=True) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: reference .. ref_arxiv:: 1 1904.06560 """ diff --git a/qiskit_experiments/library/characterization/t2ramsey.py b/qiskit_experiments/library/characterization/t2ramsey.py index 489839f0f1..b4b06be794 100644 --- a/qiskit_experiments/library/characterization/t2ramsey.py +++ b/qiskit_experiments/library/characterization/t2ramsey.py @@ -59,6 +59,36 @@ class T2Ramsey(BaseExperiment): # section: analysis_ref :class:`T2RamseyAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_ibm_runtime.fake_provider import FakeManilaV2 + from qiskit_aer import AerSimulator + from qiskit_aer.noise import NoiseModel + + noise_model = NoiseModel.from_backend(FakeManilaV2(), + thermal_relaxation=True, + gate_error=False, + readout_error=False, + ) + + backend = AerSimulator.from_backend(FakeManilaV2(), noise_model=noise_model) + + .. jupyter-execute:: + + import numpy as np + import qiskit + from qiskit_experiments.library import T2Ramsey + + delays = list(np.arange(1.00e-6, 50.0e-6, 2.00e-6)) + exp = T2Ramsey(physical_qubits=(0, ), delays=delays, backend=backend, osc_freq=1.0e5) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) + # section: reference .. ref_arxiv:: 1 1904.06560 """ diff --git a/qiskit_experiments/library/characterization/tphi.py b/qiskit_experiments/library/characterization/tphi.py index b19a805c84..5e1755326d 100644 --- a/qiskit_experiments/library/characterization/tphi.py +++ b/qiskit_experiments/library/characterization/tphi.py @@ -50,6 +50,43 @@ class Tphi(BatchExperiment): # section: analysis_ref :class:`.TPhiAnalysis` + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_ibm_runtime.fake_provider import FakeManilaV2 + from qiskit_aer import AerSimulator + from qiskit_aer.noise import NoiseModel + + noise_model = NoiseModel.from_backend(FakeManilaV2(), + thermal_relaxation=True, + gate_error=False, + readout_error=False, + ) + + backend = AerSimulator.from_backend(FakeManilaV2(), noise_model=noise_model) + + .. jupyter-execute:: + + import numpy as np + import qiskit + from qiskit_experiments.library.characterization import Tphi + + delays_t1 = np.arange(1e-6, 300e-6, 10e-6) + delays_t2 = np.arange(1e-6, 50e-6, 2e-6) + + exp = Tphi(physical_qubits=(0, ), + delays_t1=delays_t1, + delays_t2=delays_t2, + backend=backend + ) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + display(exp_data.figure(1)) + exp_data.analysis_results(dataframe=True) + # section: reference .. ref_arxiv:: 1 1904.06560 diff --git a/qiskit_experiments/library/characterization/zz_ramsey.py b/qiskit_experiments/library/characterization/zz_ramsey.py index daf546302b..69cc0a1306 100644 --- a/qiskit_experiments/library/characterization/zz_ramsey.py +++ b/qiskit_experiments/library/characterization/zz_ramsey.py @@ -124,6 +124,33 @@ class ZZRamsey(BaseExperiment): # section: analysis_ref :class:`ZZRamseyAnalysis` + + # section: example + .. jupyter-execute:: + :hide-code: + + # backend + from qiskit_ibm_runtime.fake_provider import FakePerth + from qiskit_aer import AerSimulator + from qiskit_aer.noise import NoiseModel + + noise_model = NoiseModel.from_backend(FakePerth(), + thermal_relaxation=True, + gate_error=False, + readout_error=False, + ) + + backend = AerSimulator.from_backend(FakePerth(), noise_model=noise_model) + + .. jupyter-execute:: + + from qiskit_experiments.library.characterization import ZZRamsey + + exp = ZZRamsey(physical_qubits=(0,1), backend=backend) + + exp_data = exp.run().block_for_results() + display(exp_data.figure(0)) + exp_data.analysis_results(dataframe=True) """ def __init__( diff --git a/releasenotes/notes/add-examples-to-characterization-experiments-e77d4d26c6b49694.yaml b/releasenotes/notes/add-examples-to-characterization-experiments-e77d4d26c6b49694.yaml new file mode 100644 index 0000000000..289d499884 --- /dev/null +++ b/releasenotes/notes/add-examples-to-characterization-experiments-e77d4d26c6b49694.yaml @@ -0,0 +1,8 @@ +--- +other: + - | + Added minimal working code examples to the API pages for each experiment, + especially in characterizaion experiments. The minimal working code example + is a code snippet which users can copy and paste to run the experiment. + Users are required to specify a backend to use the code example. By default, + the backend used in the examples is a simulator such as `FakeManilaV2`.