Skip to content

Commit

Permalink
Check for more user errors (normalized state) and extend collapse_wav…
Browse files Browse the repository at this point in the history
…efunction input (#256)
  • Loading branch information
damiansteiger committed Aug 14, 2018
1 parent 11f4587 commit db8bee5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ install:
- pip$PY install -e .

# command to run tests
script: export OMP_NUM_THREADS=1 && pytest -W error projectq --cov projectq
script: export OMP_NUM_THREADS=1 && pytest projectq --cov projectq

after_success:
- coveralls
4 changes: 2 additions & 2 deletions projectq/backends/_sim/_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def collapse_wavefunction(self, qureg, values):
Args:
qureg (Qureg|list[Qubit]): Qubits to collapse.
values (list[bool]): Measurement outcome for each of the qubits
values (list[bool|int]|string[0|1]): Measurement outcome for each of the qubits
in `qureg`.
Raises:
Expand All @@ -321,7 +321,7 @@ def collapse_wavefunction(self, qureg, values):
"""
qureg = self._convert_logical_to_mapped_qureg(qureg)
return self._simulator.collapse_wavefunction([qb.id for qb in qureg],
[bool(v) for v in
[bool(int(v)) for v in
values])

def cheat(self):
Expand Down
5 changes: 5 additions & 0 deletions projectq/setups/decompositions/stateprep2cnot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def _decompose_state_preparation(cmd):
final_state = cmd.gate.final_state
if len(final_state) != 2**num_qubits:
raise ValueError("Length of final_state is invalid.")
norm = 0.
for amplitude in final_state:
norm += abs(amplitude)**2
if norm < 1 - 1e-10 or norm > 1 + 1e-10:
raise ValueError("final_state is not normalized.")
with Control(eng, cmd.control_qubits):
# As in the paper reference, we implement the inverse:
with Dagger(eng):
Expand Down
3 changes: 3 additions & 0 deletions projectq/setups/decompositions/stateprep2cnot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def test_wrong_final_state():
cmd = Command(None, StatePreparation([0, 1j]), qubits=([qb0, qb1],))
with pytest.raises(ValueError):
stateprep2cnot._decompose_state_preparation(cmd)
cmd2 = Command(None, StatePreparation([0, 0.999j]), qubits=([qb0, qb1],))
with pytest.raises(ValueError):
stateprep2cnot._decompose_state_preparation(cmd2)


@pytest.mark.parametrize("zeros", [True, False])
Expand Down
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[pytest]
testpaths = projectq

filterwarnings =
error
ignore:the matrix subclass is not the recommended way:PendingDeprecationWarning

0 comments on commit db8bee5

Please sign in to comment.