corbett / QuantumComputing

This is an implementation of IBM's Quantum Experience in simulation; a 5-qubit quantum computer with a limited set of gates. Please cite me if you end up using this academically.
GNU General Public License v3.0
587 stars 141 forks source link

Is |psi>=|100> in reverse order 01234? #7

Closed marcus-mello closed 6 years ago

marcus-mello commented 7 years ago

Dear Dr. Christine,

I am following up on my research in the philosophical field of "quantum thinking" (not binary... false/true, good/evil, right/wrong ...) and, meanwhile, I try to deepen my technical knowledge on the functioning of the QASM language .

I'm new to Python3, just a month old, but I'm old in several other languages, including C++. Unfortunately I still do not fully understand the functioning of your application, so I apologize for taking your time.

Despite the online availability of the IBM Quantum-e platform, it naturally needs an internet connection and ends up being slow for many tests, as well as the limitations on the size of the QASM code, and the track restrictions available for CNOT gates.

For the huge amount of logical testing I need to do, your application is 100% effective. It is fast and flexible, precisely because it is not limited to the constraints of the real quantum computer.

Well ... after this long introduction, I explain below the reason for my post:

Are the qubits in |psi>=|100> from Probability.pretty_print_probabilities function in "reverse" order 01234 ?

I was testing the Toffoli gate, and I expected the following results in 43210 order: 00 --> |psi>=|000> 01 --> |psi>=|001> 10 --> |psi>=|010> 11 --> |psi>=|111>

But, I always get the following result: 00 --> |psi>=|000> 01 --> |psi>=|100> <-------Look at this! In reverse order 01234 ! 10 --> |psi>=|010> 11 --> |psi>=|111>

I thought I had typed something wrong in the qasm code, but I realized that the sequence is in reverse order 01234.

Can we modify this?

p.s Later I extended the qasm code to do more tests, adding the result of q2 with q3 and putting the final result in q4. I initialized only q0 and q3 with 1 to actually do nothing, and I got the following print:
|psi>|10010> in 01234 order...

Many thanks,

Marcus Mello UNIRIO Universidade Federal do Estado do Rio de Janeiro Departamento de Filosofia

=====================================

from QuantumComputer import *

qasm="""

Toffoli - q0+q1=q2 +q3=q4;

entries;

q0;

x q[0];

q1;

x q[1];

q3;

x q[3];

first step: q0+q1=q2;

id q[2]; h q[2]; cx q[1], q[2]; tdg q[2]; cx q[0], q[2]; tdg q[2]; cx q[1], q[2]; tdg q[2]; cx q[0], q[2]; tdg q[1]; tdg q[2]; h q[2];

ibm flip;

cx q[1], q[2]; h q[1]; h q[2]; cx q[1], q[2]; h q[1]; h q[2]; cx q[1], q[2];

cx q[0], q[2]; tdg q[2]; tdg q[0]; cx q[0], q[2];

my flip;

cx q[1], q[2]; h q[1]; h q[2]; cx q[1], q[2]; h q[1]; h q[2]; cx q[1], q[2];

next step: q2+q3=4;

id q[4]; h q[4]; cx q[3], q[4]; tdg q[4]; cx q[2], q[4]; tdg q[4]; cx q[3], q[4]; tdg q[4]; cx q[2], q[4]; tdg q[3]; tdg q[4]; h q[4];

ibm flip;

cx q[3], q[4]; h q[3]; h q[4]; cx q[3], q[4]; h q[3]; h q[4]; cx q[3], q[4];

cx q[2], q[4]; tdg q[4]; tdg q[2]; cx q[2], q[4];

my flip;

cx q[3], q[4]; h q[3]; h q[4]; cx q[3], q[4]; h q[3]; h q[4]; cx q[3], q[4];

measure q[0]; measure q[1]; measure q[2]; measure q[3]; measure q[4];

"""

qc=QuantumComputer() qc.reset() qc.execute(qasm)

print () print ("========== QUANTUM REGISTER ==========")

for n in range (0,5): arg="q" + str(n) print ("QUBIT",str(n)) Probability.pretty_print_probabilities (qc.qubits.get_quantum_register_containing(arg).get_state())

marcus-mello commented 7 years ago

Well... I'm working hard to assimilate the logic of QuantumComputer.py

Looking at the internal array Python structures, we can see that always start in 0, then is natural to work with qubits in that order: 012434

Then, it is more simple adjust the sequence of qubits at end of process.

The field 'state_desc' in 'pretty_print_probabilities' function own the solution. The field 'QuantumRegister.num_qubits(state)' give us the number of 'state_desc' digits to be inverted.

This is the "new" pretty_print_probabilities: (Note the #MVCM remarks in the modified lines)

def pretty_print_probabilities(state):
    probs=Probability.get_probabilities(state)
    am_desc='|psi>='
    pr_desc=''
    #MVCM get num qubits to print: 1 to 5
    nqb = QuantumRegister.num_qubits(state)
    for am,pr,state_desc in zip(state.flat,probs,State.all_state_strings( QuantumRegister.num_qubits(state))):
           #MVCM invert state_desc binary sequence from 01234 to 43210 for |psi> print
           state_desc_inverted = ''
           offset = nqb - 1
           for n in range(0,nqb):
                  occ = ((n+offset)-(2*n))
                  state_desc_inverted += str(state_desc[occ:occ+1])
          if am!=0:
               if am!=1:
                       #MVCM am_desc+='%r|%s>+'%(am,state_desc)
                       am_desc+='%r|%s>+'%(am,state_desc_inverted)
              else:
                      #MVCM am_desc+='|%s>+'%(state_desc)
                      am_desc+='|%s>+'%(state_desc_inverted)
          if pr!=0:
               #MVCM pr_desc+='Pr(|%s>)=%f; '%(state_desc,pr)
               pr_desc+='Pr(|%s>)=%f; '%(state_desc_inverted,pr)
    print(am_desc[0:-1])
    print(pr_desc)
    if state.shape==(4,1):
        print("<state>=%f" % float(probs[0]+probs[3]-probs[1]-probs[2]))

The sample attached adjust the qubit0 to |1> using a X gate to give us a 00001 at end, in 43210 order.

Because the boring warning from module at start, I did change also the "if state==None:" clause to "if state is None:" in the lines 201 and 567, and, you see below the print result 'before' and 'after' the changes:

Before changes...

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.

RESTART: C:\Users\Usuario\Dropbox\IBM\QUBIT\QUINTUPLE\FONTES\Toffoli_0+1=2+3=4.py

Warning (from warnings module): File "C:\Users\Usuario\Dropbox\IBM\QUBIT\QUINTUPLE\FONTES\QuantumComputer.py", line 200 if state==None: FutureWarning: comparison to None will result in an elementwise object comparison in the future.

========== QUANTUM REGISTER ========== QUBIT 0 |psi>=|10000> Pr(|10000>)=1.000000; QUBIT 1 |psi>=|10000> Pr(|10000>)=1.000000; QUBIT 2 |psi>=|10000> Pr(|10000>)=1.000000; QUBIT 3 |psi>=|10000> Pr(|10000>)=1.000000; QUBIT 4 |psi>=|10000> Pr(|10000>)=1.000000;

After changes...

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.

RESTART: C:\Users\Usuario\Dropbox\IBM\QUBIT\QUINTUPLE\FONTES\Toffoli_0+1=2+3=4.py

========== QUANTUM REGISTER ========== QUBIT 0 |psi>=|00001> Pr(|00001>)=1.000000; QUBIT 1 |psi>=|00001> Pr(|00001>)=1.000000; QUBIT 2 |psi>=|00001> Pr(|00001>)=1.000000; QUBIT 3 |psi>=|00001> Pr(|00001>)=1.000000; QUBIT 4 |psi>=|00001> Pr(|00001>)=1.000000;

Solved! :)

Marcus Mello UNIRIO Universidade Federal do Estado do Rio de Janeiro Departamento de Filosofia

Toffoli_0+1=2+3=4.txt

QuantumComputer.txt