Skip to content

Commit

Permalink
fix the ipython_display side effect
Browse files Browse the repository at this point in the history
  • Loading branch information
timmysilv committed Dec 5, 2024
1 parent 4c603d0 commit bf5d99a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mrmustard/lab_dev/circuit_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import numpy as np
from numpy.typing import ArrayLike
import ipywidgets as widgets
from IPython import get_ipython, InteractiveShell
from IPython.display import display

from mrmustard import settings, math, widgets as mmwidgets
Expand Down Expand Up @@ -724,6 +725,9 @@ def __truediv__(self, other: Scalar) -> CircuitComponent:
return self._from_attributes(Representation(self.ansatz / other, self.wires), self.name)

def _ipython_display_(self):
if isinstance(get_ipython(), InteractiveShell):
print(self)
return

Check warning on line 730 in mrmustard/lab_dev/circuit_components.py

View check run for this annotation

Codecov / codecov/patch

mrmustard/lab_dev/circuit_components.py#L729-L730

Added lines #L729 - L730 were not covered by tests
# both reps might return None
rep_fn = mmwidgets.fock if isinstance(self.ansatz, ArrayAnsatz) else mmwidgets.bargmann
rep_widget = rep_fn(self.ansatz)
Expand Down
4 changes: 4 additions & 0 deletions mrmustard/lab_dev/states/dm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from itertools import product
import warnings
import numpy as np
from IPython import get_ipython, InteractiveShell
from IPython.display import display

from mrmustard import math, settings, widgets
Expand Down Expand Up @@ -375,6 +376,9 @@ def quadrature_distribution(self, quad: RealVector, phi: float = 0.0) -> Complex
return self.quadrature(quad, phi)

def _ipython_display_(self): # pragma: no cover
if isinstance(get_ipython(), InteractiveShell):
print(self)
return
is_fock = isinstance(self.ansatz, ArrayAnsatz)
display(widgets.state(self, is_ket=False, is_fock=is_fock))

Expand Down
4 changes: 4 additions & 0 deletions mrmustard/lab_dev/states/ket.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from itertools import product
import warnings
import numpy as np
from IPython import get_ipython, InteractiveShell
from IPython.display import display

from mrmustard import math, settings, widgets
Expand Down Expand Up @@ -335,6 +336,9 @@ def quadrature_distribution(self, quad: RealVector, phi: float = 0.0) -> Complex
return math.abs(self.quadrature(quad, phi)) ** 2

def _ipython_display_(self): # pragma: no cover
if isinstance(get_ipython(), InteractiveShell):
print(self)
return
is_fock = isinstance(self.ansatz, ArrayAnsatz)
display(widgets.state(self, is_ket=True, is_fock=is_fock))

Expand Down
4 changes: 4 additions & 0 deletions mrmustard/physics/ansatz/array_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import numpy as np
from numpy.typing import ArrayLike

from IPython import get_ipython, InteractiveShell
from IPython.display import display

from mrmustard import math, widgets
Expand Down Expand Up @@ -207,6 +208,9 @@ def _generate_ansatz(self):
self.array = [self._fn(**self._kwargs)]

def _ipython_display_(self):
if isinstance(get_ipython(), InteractiveShell):
print(self)
return

Check warning on line 213 in mrmustard/physics/ansatz/array_ansatz.py

View check run for this annotation

Codecov / codecov/patch

mrmustard/physics/ansatz/array_ansatz.py#L212-L213

Added lines #L212 - L213 were not covered by tests
w = widgets.fock(self)
if w is None:
print(repr(self))
Expand Down
4 changes: 4 additions & 0 deletions mrmustard/physics/ansatz/polyexp_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from matplotlib import colors
import matplotlib.pyplot as plt

from IPython import get_ipython, InteractiveShell
from IPython.display import display

from mrmustard.utils.typing import (
Expand Down Expand Up @@ -568,6 +569,9 @@ def _generate_ansatz(self):
self.c = c

def _ipython_display_(self):
if isinstance(get_ipython(), InteractiveShell):
print(self)
return

Check warning on line 574 in mrmustard/physics/ansatz/polyexp_ansatz.py

View check run for this annotation

Codecov / codecov/patch

mrmustard/physics/ansatz/polyexp_ansatz.py#L573-L574

Added lines #L573 - L574 were not covered by tests
display(widgets.bargmann(self))

def _order_batch(self):
Expand Down
4 changes: 4 additions & 0 deletions mrmustard/physics/wires.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from functools import cached_property
import numpy as np

from IPython import get_ipython, InteractiveShell
from IPython.display import display

from mrmustard import widgets
Expand Down Expand Up @@ -546,4 +547,7 @@ def __repr__(self) -> str:
return f"Wires{self.args}"

def _ipython_display_(self):
if isinstance(get_ipython(), InteractiveShell):
print(self)
return

Check warning on line 552 in mrmustard/physics/wires.py

View check run for this annotation

Codecov / codecov/patch

mrmustard/physics/wires.py#L551-L552

Added lines #L551 - L552 were not covered by tests
display(widgets.wires(self))

0 comments on commit bf5d99a

Please sign in to comment.