Skip to content

Commit

Permalink
QiskitDevice.probabilities() changed to QiskitDevice.probability() to… (
Browse files Browse the repository at this point in the history
#80)

* QiskitDevice.probabilities() changed to QiskitDevice.probability() to restore compatibility with pennylane python package

* updated changelog

Co-authored-by: antalszava <[email protected]>
Co-authored-by: Josh Izaac <[email protected]>
  • Loading branch information
3 people authored Apr 15, 2020
1 parent dfba326 commit 9b462af
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@

### Bug fixes

* Renamed `QiskitDevice.probabilities` to `QiskitDevice.probability` to overload `pennylane.Device.probability`. This fixes a bug that raises `NotImplementedError` when a QNode is used to compute probabilities on a IBMQ device.
[(#80)](https://github.com/XanaduAI/pennylane-qiskit/pull/80)

### Contributors

This release contains contributions from (in alphabetical order):

Rafael Haenel

---

# Release 0.8.2
Expand Down
10 changes: 5 additions & 5 deletions pennylane_qiskit/qiskit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def expval(self, observable, wires, par):
if self.backend_name in self._state_backends and self.analytic:
# exact expectation value
eigvals = self.eigvals(observable, wires, par)
prob = np.fromiter(self.probabilities(wires=wires).values(), dtype=np.float64)
prob = np.fromiter(self.probability(wires=wires).values(), dtype=np.float64)
return (eigvals @ prob).real

if self.analytic:
Expand All @@ -354,7 +354,7 @@ def var(self, observable, wires, par):
if self.backend_name in self._state_backends and self.analytic:
# exact variance value
eigvals = self.eigvals(observable, wires, par)
prob = np.fromiter(self.probabilities(wires=wires).values(), dtype=np.float64)
prob = np.fromiter(self.probability(wires=wires).values(), dtype=np.float64)
return (eigvals ** 2) @ prob - (eigvals @ prob).real ** 2

if self.analytic:
Expand All @@ -371,7 +371,7 @@ def sample(self, observable, wires, par):
if self.backend_name in self._state_backends:
# software simulator. Need to sample from probabilities.
eigvals = self.eigvals(observable, wires, par)
prob = np.fromiter(self.probabilities(wires=wires).values(), dtype=np.float64)
prob = np.fromiter(self.probability(wires=wires).values(), dtype=np.float64)
return np.random.choice(eigvals, self.shots, p=prob)

# a hardware simulator
Expand All @@ -385,7 +385,7 @@ def sample(self, observable, wires, par):
else:
# Need to convert counts into samples
samples = np.vstack(
[np.vstack([s] * int(self.shots * p)) for s, p in self.probabilities().items()]
[np.vstack([s] * int(self.shots * p)) for s, p in self.probability().items()]
)

if isinstance(observable, str) and observable in {"PauliX", "PauliY", "PauliZ", "Hadamard"}:
Expand All @@ -405,7 +405,7 @@ def sample(self, observable, wires, par):
def state(self):
return self._state

def probabilities(self, wires=None):
def probability(self, wires=None):
"""Return the (marginal) probability of each computational basis
state from the last run of the device.
Expand Down
14 changes: 7 additions & 7 deletions tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def test_qubit_state_vector(self, init_state, device, tol):
dev._obs_queue = []
dev.pre_measure()

res = np.fromiter(dev.probabilities().values(), dtype=np.float64)
res = np.fromiter(dev.probability().values(), dtype=np.float64)
expected = np.abs(state) ** 2
assert np.allclose(res, expected, **tol)

Expand All @@ -224,7 +224,7 @@ def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol):
dev._obs_queue = []
dev.pre_measure()

res = np.fromiter(dev.probabilities().values(), dtype=np.float64)
res = np.fromiter(dev.probability().values(), dtype=np.float64)
expected = np.abs(mat @ state) ** 2
assert np.allclose(res, expected, **tol)

Expand All @@ -240,7 +240,7 @@ def test_single_qubit_parameters(self, init_state, device, name, func, theta, to
dev._obs_queue = []
dev.pre_measure()

res = np.fromiter(dev.probabilities().values(), dtype=np.float64)
res = np.fromiter(dev.probability().values(), dtype=np.float64)
expected = np.abs(func(theta) @ state) ** 2
assert np.allclose(res, expected, **tol)

Expand All @@ -255,7 +255,7 @@ def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol):
dev._obs_queue = []
dev.pre_measure()

res = np.fromiter(dev.probabilities().values(), dtype=np.float64)
res = np.fromiter(dev.probability().values(), dtype=np.float64)
expected = np.abs(mat @ state) ** 2
assert np.allclose(res, expected, **tol)

Expand All @@ -270,7 +270,7 @@ def test_qubit_unitary(self, init_state, device, mat, tol):
dev._obs_queue = []
dev.pre_measure()

res = np.fromiter(dev.probabilities().values(), dtype=np.float64)
res = np.fromiter(dev.probability().values(), dtype=np.float64)
expected = np.abs(mat @ state) ** 2
assert np.allclose(res, expected, **tol)

Expand All @@ -293,7 +293,7 @@ def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol):
dev._obs_queue = []
dev.pre_measure()

res = np.fromiter(dev.probabilities().values(), dtype=np.float64)
res = np.fromiter(dev.probability().values(), dtype=np.float64)
expected = np.abs(mat @ state) ** 2
assert np.allclose(res, expected, **tol)

Expand All @@ -309,6 +309,6 @@ def test_single_qubit_parameters(self, init_state, device, name, func, theta, to
dev._obs_queue = []
dev.pre_measure()

res = np.fromiter(dev.probabilities().values(), dtype=np.float64)
res = np.fromiter(dev.probability().values(), dtype=np.float64)
expected = np.abs(func(theta) @ state) ** 2
assert np.allclose(res, expected, **tol)
2 changes: 1 addition & 1 deletion tests/test_qiskit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_probability_no_results(self):
"""Test that the probabilities function returns
None if no job has yet been run."""
dev = AerDevice(backend="statevector_simulator", wires=1, analytic=True)
assert dev.probabilities() is None
assert dev.probability() is None


class TestAnalyticWarningHWSimulator:
Expand Down

0 comments on commit 9b462af

Please sign in to comment.