Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StatePreparation division by 0 and update docs #255

Merged
merged 13 commits into from Aug 12, 2018
3 changes: 3 additions & 0 deletions docs/projectq.ops.rst
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
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
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