Skip to content

Commit

Permalink
Fix StatePreparation division by 0 and update docs (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
damiansteiger committed Aug 12, 2018
1 parent 0d1a78f commit 11f4587
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/projectq.ops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ The operations collection consists of various default gates and is a work-in-pro
projectq.ops.CZ
projectq.ops.Toffoli
projectq.ops.TimeEvolution
projectq.ops.UniformlyControlledRy
projectq.ops.UniformlyControlledRz
projectq.ops.StatePreparation


Module contents
Expand Down
6 changes: 5 additions & 1 deletion projectq/setups/decompositions/stateprep2cnot.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def _decompose_state_preparation(cmd):
for block in range(2**(len(qureg)-target_qubit-1)):
a0 = abs_of_blocks[2*block]
a1 = abs_of_blocks[2*block+1]
angles.append(-2. * math.acos(a0/math.sqrt(a0**2 + a1**2)))
if a0 == 0 and a1 == 0:
angles.append(0)
else:
angles.append(
-2. * math.acos(a0 / math.sqrt(a0**2 + a1**2)))
abs_of_next_blocks.append(math.sqrt(a0**2 + a1**2))
UniformlyControlledRy(angles) | (qureg[(target_qubit+1):],
qureg[target_qubit])
Expand Down
6 changes: 5 additions & 1 deletion projectq/setups/decompositions/stateprep2cnot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ def test_wrong_final_state():
stateprep2cnot._decompose_state_preparation(cmd)


@pytest.mark.parametrize("zeros", [True, False])
@pytest.mark.parametrize("n_qubits", [1, 2, 3, 4])
def test_state_preparation(n_qubits):
def test_state_preparation(n_qubits, zeros):
engine_list = restrictedgateset.get_engine_list(
one_qubit_gates=(Ry, Rz, Ph))
eng = projectq.MainEngine(engine_list=engine_list)
qureg = eng.allocate_qureg(n_qubits)
eng.flush()

f_state = [0.2+0.1*x*cmath.exp(0.1j+0.2j*x) for x in range(2**n_qubits)]
if zeros:
for i in range(2**(n_qubits-1)):
f_state[i] = 0
norm = 0
for amplitude in f_state:
norm += abs(amplitude)**2
Expand Down

0 comments on commit 11f4587

Please sign in to comment.