ProjectQ-Framework / ProjectQ

ProjectQ: An open source software framework for quantum computing
https://projectq.ch
Apache License 2.0
888 stars 274 forks source link

Need flush() before StatePreparation? #303

Closed philiplunel closed 5 years ago

philiplunel commented 5 years ago

Hi,

I was using the StatePreparation() command on 4 qubits setting up a specific state. When I accessed the wave function afterwards I would get something different than what I prepared and when if flush before the StatePreparation() it seems to work fine. See the following programs

import numpy as np

from projectq import MainEngine
from projectq.ops import StatePreparation
eng = MainEngine()

q = eng.allocate_qureg(4)
b = [0, 0, 0.5, 0.5, 0, 0, 0, 0, 0.7071067811865476, 0, 0, 0, 0, 0, 0, 0]

#eng.flush()
StatePreparation(b) | q

print(b)
eng.flush()
c = eng.backend.cheat()
d = [ np.round(elem, 5) for elem in c[1] ]
print(d)

All(Measure) | q
del q

which outputs

[0, 0, 0.5, 0.5, 0, 0, 0, 0, 0.7071067811865476, 0, 0, 0, 0, 0, 0, 0]
[(-0+0j), (0.5+0j), 0j, (0.5+0j), 0j, 0j, 0j, 0j, (0.70711+0j), 0j, (-0+0j), (-0+0j), 0j, 0j, 0j, 0j]

which seemed weird since the |0010> state is now not prepared and instead the |0001> state is.

When I flush (uncomment it in the code above) before doing the StatePreparation() I get as output

[0, 0, 0.5, 0.5, 0, 0, 0, 0, 0.7071067811865476, 0, 0, 0, 0, 0, 0, 0]
[(-0+0j), 0j, (0.5+0j), (0.5+0j), 0j, 0j, 0j, 0j, (0.70711+0j), (-0+0j), 0j, (-0+0j), 0j, 0j, 0j, 0j]

as expected. What causes this and should one always flush before using the StatePreparation() command?

Edit: btw I know projectq views the binary representation reversed i.e. '1000' is the first state and '0001' the eighth, but I mean with |0010> the |2> state and with |0001> the |1> state and thus the third and second value respectivaly of the list the cheating algorithm.

damiansteiger commented 5 years ago

Both wavefunctions are equivalent. When using cheat, one needs to take into account the ordering of qubits (which is arbitrary and changed in our case because of the flush()) which is given in c[0], checkout the documentation of cheat

I would suggest to use instead the helper functions introduced in the simulator tutorial which allow you to specify the binary state and order of qubits for which you want to know an amplitude.

No need to call flush() before StatePreparation and hence not calling it can produce better optimized code and is the suggested approach. But there is nothing wrong with calling flush.

Besides:

philiplunel commented 5 years ago

Thanks, I missed that.

damiansteiger commented 5 years ago

No problem, I am happy to help out.