Skip to content

Commit

Permalink
Make figures loaded from experiment db viewable in notebook (qiskit-c…
Browse files Browse the repository at this point in the history
…ommunity#1321)

### Summary

Make figures loaded from experiment db viewable in jupyter notebook as
suggested in qiskit-community#436 .
### Details and comments

Some details that should be in this section include:

- Why this change was necessary
  To  make it more comfortable to use figures in jupyter notebook.
- What do users and developers need to know about this change
To display figures of experiment data that were loaded from experiment
db don't need to convert to SVG.

---------

Co-authored-by: Will Shanks <[email protected]>
  • Loading branch information
2 people authored and nkanazawa1989 committed Jan 17, 2024
1 parent e2c274f commit 9efb0e8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
6 changes: 2 additions & 4 deletions docs/howtos/cloud_service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ experiment <https://quantum.ibm.com/experiments/9640736e-d797-4321-b063-d503f8e9
service = ExperimentData.get_service_from_backend(backend)
load_expdata = ExperimentData.load("9640736e-d797-4321-b063-d503f8e98571", service)

To display the figure, which is serialized into a string, we need the
``SVG`` library:
Now we can display the figure from the loaded experiment data:

.. jupyter-input::

from IPython.display import SVG
SVG(load_expdata.figure(0).figure)
load_expdata.figure(0)

.. image:: ./experiment_cloud_service/t1_loaded.png

Expand Down
3 changes: 2 additions & 1 deletion qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
ExperimentDataSaveFailed,
)


if TYPE_CHECKING:
# There is a cyclical dependency here, but the name needs to exist for
# Sphinx on Python 3.9+ to link type hints correctly. The gating on
Expand Down Expand Up @@ -192,7 +193,7 @@ def _repr_svg_(self):
if isinstance(self.figure, str):
return self.figure
if isinstance(self.figure, bytes):
return str(self.figure)
return self.figure.decode("utf-8")
return None


Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/figure_return_SVG-4ad72fc8a3bee3cb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Figures loaded from the experiment service are now rendered correctly in Jupyter Notebook.

0 comments on commit 9efb0e8

Please sign in to comment.