rigetti / pyquil

A Python library for quantum programming using Quil.
http://docs.rigetti.com
Apache License 2.0
1.4k stars 342 forks source link

Measuring and Storing #126

Closed rasa97 closed 7 years ago

rasa97 commented 7 years ago

For the code:

p = pq.Program()
p.inst(X(0)).measure(0,1)
print qvm.run(p, [1,0,2])

I would think that the program takes 1 (X[0]), invert it, and print the result as [[0,0,0]]. But the result is [[1,0,0]]. How is this happening?

willzeng commented 7 years ago

Hi @rasa97 I can tell you a little more about what is happening. This program:

p = pq.Program()
p.inst(X(0)).measure(0,1)

Applies an X gate to the zeroth qubit, placing that qubit in the 1 state. It then measures the state of the zeroth qubit and deposits that bit result in the first classical register. Since the classical registers are initialized in zero, the state of the classical memory at the end of the computation is: [0,1,0] where the first classical register contains the result of the measurement. You'll get this outcome if you run:

print qvm.run(p, range(3))

to look at the first three bits of classical memory.

When you run:

print qvm.run(p, [1,0,2])

You are asking for the classical memory to be reordered so in the returned list, so that you get the contents of the first bit, the zeroth bit, and the second bit in that order. Thus instead of [0, 1, 0] you get [1, 0, 0] since the classical bit registers have been swapped.

Does that help?

rasa97 commented 7 years ago

Thanks for the clarification! One more thing, it appears that I can only create qubits which are initialised as qubit 0. How can I create qubits initialised to qubit 1 so that I can apply Hadamard gate on it?

willzeng commented 7 years ago

In Quil all qubits start in the zero state. To change a qubit into the 1 state you can just apply an X gate. For example:

def init_qubits_in_one(qubit_list)
    p = Program()
    for q_index in qubit_list:
        p.inst(X(q_index))
    return p

Will give you a program where all qubits in qubit_list are now in the one state.

rasa97 commented 7 years ago

A question on the ordering of multiple qubits... why is it in the reverse direction than in the conventional way? I would think pq.Program().inst(I(0), X(1)) will produce |01>, but it produces |10>.

Also, how do we get the wavefunction of nth qubit in multiple qubits? Like after teleportation, how can we display the wavefunction of qubit[2] ?

willzeng commented 7 years ago

The zeroth qubit is the least significant digit. This makes it easier to convert to binary strings. It just a convention choice.

Do you mean to trace out the other qubits? We don't yet have a function trace out the others, but that would be useful!