Skip to content

Commit

Permalink
pnr fix
Browse files Browse the repository at this point in the history
  • Loading branch information
apchytr committed Aug 29, 2024
1 parent 8e91b1f commit eb22728
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion mrmustard/lab_dev/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def probabilities(self, state: State | None = None) -> list[float] | None:
state.probability if isinstance(state, State) else math.real(state)
for state in states
]
return probs
return probs / sum(probs)
return self.prob_dist

def sample(self, state: State | None = None, n_samples: int = 1000) -> list[any]:
Expand Down Expand Up @@ -134,6 +134,10 @@ class PNRSampler(Sampler):
def __init__(self, modes: Sequence[int], cutoff: int) -> None:
super().__init__(list(range(cutoff)), [Number(modes, n) for n in range(cutoff)])

def probabilities(self, state: State | None = None) -> list[float] | None:
fock_state = state.to_fock() if state else state
return super().probabilities(fock_state)


class HomodyneSampler(Sampler):
r"""
Expand Down
6 changes: 3 additions & 3 deletions tests/test_lab_dev/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_probabilities(self):
assert sampler2.probabilities() is None

state = Vacuum([0])
assert sampler2.probabilities(state) == [1, 0, 0]
assert all(sampler2.probabilities(state) == [1, 0, 0])

with pytest.raises(ValueError, match="incompatible"):
sampler_two_mode = Sampler(
Expand Down Expand Up @@ -88,8 +88,8 @@ def test_probabilities(self):
sampler = PNRSampler([0, 1], cutoff=10)
vac_prob = [1.0] + [0.0] * 9
assert sampler.probabilities() is None
assert sampler.probabilities(Vacuum([0, 1])) == vac_prob
assert sampler.probabilities(Vacuum([0, 1, 2])) == vac_prob
assert all(sampler.probabilities(Vacuum([0, 1])) == vac_prob)
assert all(sampler.probabilities(Vacuum([0, 1, 2])) == vac_prob)


class TestHomodyneSampler:
Expand Down

0 comments on commit eb22728

Please sign in to comment.