Skip to content

Commit

Permalink
get_probabilities: Checks that all qubits are known.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomashaener committed Apr 20, 2017
1 parent 1f60033 commit d3587e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions projectq/backends/_ibm/_ibm.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ def get_probabilities(self, qureg):
probabilities.
Raises:
Exception: If no data is available (i.e., if the circuit has not
been executed).
RuntimeError: If no data is available (i.e., if the circuit has
not been executed). Or if a qubit was supplied which was not
present in the circuit (might have gotten optimized away).
"""
if len(self._probabilities) == 0:
raise RuntimeError("Please, run the circuit first!")
if any(qb.id not in self._mapping for qb in qureg):
raise RuntimeError("Unknown qubit. Please, make sure that all "
"provided qubits are present in the circuit.")

probability_dict = dict()

Expand Down
3 changes: 3 additions & 0 deletions projectq/backends/_ibm/_ibm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,6 @@ def mock_send(*args, **kwargs):
prob_dict = eng.backend.get_probabilities([qureg[0], qureg[2], qureg[1]])
assert prob_dict['111'] == pytest.approx(0.38671875)
assert prob_dict['101'] == pytest.approx(0.0263671875)

with pytest.raises(RuntimeError):
eng.backend.get_probabilities(eng.allocate_qubit())

0 comments on commit d3587e3

Please sign in to comment.