Skip to content

Commit

Permalink
[Bug] Normalize probabilities (#53)
Browse files Browse the repository at this point in the history
* normalize probabilities

* black

* changelog

* Update CHANGELOG.md

* Update CHANGELOG.md
  • Loading branch information
albi3ro authored Apr 21, 2022
1 parent 8cd59b6 commit 7580077
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
`SimulatorDevice` class for general IonQ devices.
[(#50)](https://github.com/PennyLaneAI/PennyLane-IonQ/pull/50)

### Bug Fixes

* Since the histogram of probabilities returned from the remote simulator does not always sum exactly to one,
the PennyLane device normalizes them to higher precision.
[(#53)](https://github.com/PennyLaneAI/PennyLane-IonQ/pull/53)

### Contributors

This release contains contributions from (in alphabetical order):

Jon Donovan, Antal Száva
Jon Donovan, Christina Lee, Antal Száva

---

Expand Down
7 changes: 5 additions & 2 deletions pennylane_ionq/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ def apply(self, operations, **kwargs):
for operation in rotations:
self._apply_operation(operation)

# print(self.job)
self._submit_job()

def _apply_operation(self, operation):
Expand Down Expand Up @@ -194,7 +193,11 @@ def prob(self):

# convert the sparse probs into a probability array
self._prob_array = np.zeros([2**self.num_wires])
self._prob_array[idx] = np.fromiter(self.histogram.values(), float)

# histogram values don't always perfectly sum to exactly one
histogram_values = self.histogram.values()
norm = sum(histogram_values)
self._prob_array[idx] = np.fromiter(histogram_values, float) / norm

return self._prob_array

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def init_state(scope="session"):

def _init_state(n):
state = np.random.randint(0, 2, [n])
ket = np.zeros([2 ** n])
ket = np.zeros([2**n])
ket[np.ravel_multi_index(state, [2] * n)] = 1
return state, ket

Expand Down

0 comments on commit 7580077

Please sign in to comment.