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
582 stars 138 forks source link

Port to Python 3.0 #4

Closed rtvuser1 closed 6 years ago

rtvuser1 commented 7 years ago

The code is written for Python 2.7, and will not compile without a lot of changes in 3.4+ Has anyone created a version that runs on 3.4 or 3.5 ?

LSaldyt commented 7 years ago

I've tried to port the code to Python3+. This was done with the 2to3 and reindent built-in scripts. However, I've run across some errors.

In addition to the other issue I've opened, I get:

======================================================================
ERROR: test_apply_two_qubit_gate_CNOT_two_entangled_target (__main__.TestQuantumComputer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "QuantumComputer.py", line 1732, in test_apply_two_qubit_gate_CNOT_two_entangled_target
    self.assertTrue(self.qc.qubit_states_equal("q0,q1,q2",State.state_from_string('100')))
  File "QuantumComputer.py", line 550, in qubit_states_equal
    answer_state=self.get_requested_state_order(name)
  File "QuantumComputer.py", line 487, in get_requested_state_order
    if not set(qubit.get_entangled())<=set(get_states_for) and set(qubit.get_entangled()).intersection(set(get_states_for)):
TypeError: unhashable type: 'QuantumRegister'

======================================================================
ERROR: test_program_encoder_into_bitflip_code_parity_checks (__main__.TestQuantumComputer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "QuantumComputer.py", line 2136, in test_program_encoder_into_bitflip_code_parity_checks
    self.assertTrue(self.qc.probabilities_equal("q0,q1,q2,q3,q4",np.array(program.result_probability)))
  File "QuantumComputer.py", line 539, in probabilities_equal
    answer_state=self.get_requested_state_order(name)
  File "QuantumComputer.py", line 487, in get_requested_state_order
    if not set(qubit.get_entangled())<=set(get_states_for) and set(qubit.get_entangled()).intersection(set(get_states_for)):
TypeError: unhashable type: 'QuantumRegister'

----------------------------------------------------------------------

That being said, the version on my fork technically runs in both versions of python. I plan on making a pull request once all of the errors have been fixed.

LSaldyt commented 7 years ago

I've added a simple hash function that hashes by the name of the quantum register. I've also provided an inactive hash function that will hash by the more complex members of the register. Also, I found a fix to the other issue I had opened.

So, I now have a fully-functioning (all tests pass) Python3 version of the code. The hash function mentioned above could probably use some tweaking by someone more familiar with the codebase, but in the meantime, I'll submit a pull request.

marcus-mello commented 7 years ago

I needed to do a lot of tweaking to run QuantumComputer.py in Python 3. The file attached is a QuantumComputer.py abreviated to qc5.py (in .docx file...)

All modifications has a tag #MVCM in old code line, just above the new code.

I am not sure of its correct functioning! Depending on the sequence of commands, it has errors.

If you send:

y q [0]; cx q [3], q [0];

he says:

"File" C: \ Users \ User \ Dropbox \ QUBIT \ FONTES \ qc5.py ", line 432, in get_quantum_register_containing Raise ValueError ("qubit% s not found"% name) ValueError: qubit not found

If you send:

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

he says:

"File" C: \ Users \ User \ Dropbox \ QUBIT \ FONTES \ qc5.py ", line 363, in num_qubits If state.shape [1]! = 1 or num_qubits not in [1,2,3,4,5]: IndexError: tuple index out of range

If you send:

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

he says:

File "C:\Users\Usuario\Dropbox\QUBIT\IBM_lab\qc5.py", line 432, in get_quantum_register_containing raise ValueError("qubit %s not found" % name) ValueError: qubit q2 not found

May be the bug (in Py3 version) is in function CX... I do not know the logic rules enough... so it's being rather difficult to correct these problems.

The source of qc5.py (QuantumComputer converted to Py3) is attached.

p.s. I also added a '?' command to inspect the qubits after each operation, and some "print"s on source to track the results. (a fast, rough and ugly adaptation...)

qc5.docx

LSaldyt commented 7 years ago

Hi, Our qc5.docx file looks a lot different than my QuantumComputer.py file (even aside from the changes you've had to make). Are you sure you're using the latest commit of LSaldyt/QuantumComputing/master ? This is the only place where the changes I've mentioned have occurred. There is an open pull request #6 to merge this branch with the rest of the repository. If the problem is with the latest branch of my fork, I'm still glad to help.

Cheers,

Lucas

corbett commented 7 years ago

Hi All,

Thanks I just saw this PR for the main repoβ€”I was in an interview the week it was submitted and missed the notification. Sorry for the delay, but I’ll take a look in the next week or so after a round of travel I have scheduled. Thanks for making some contributions to make it accessibly for user of python 3!

C

Sent from the future

πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€ Dr. Christine Corbett Moran www.christinecorbettmoran.com

On Thu, Mar 30, 2017 at 6:45 PM, Lucas Saldyt notifications@github.com wrote:

Hi, Our qc5.docx file looks a lot different than (my) https://github.com/LSaldyt/QuantumComputing QuantumComputer.py file (even aside from the changes you've had to make). Are you sure you're using the latest commit of LSaldyt/QuantumComputing/master ? This is the only place where the changes I've mentioned have occurred. There is an open pull request #6 https://github.com/corbett/QuantumComputing/pull/6 to merge this branch with the rest of the repository. If the problem is with the latest branch of my fork, I'm still glad to help.

Cheers,

Lucas

β€” You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/corbett/QuantumComputing/issues/4#issuecomment-290470362, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJiFsXEO6_2Dv9O2BgdeZ9MdYEg63Dpks5rq9wWgaJpZM4LlMrA .

marcus-mello commented 7 years ago

Hello Lucas,

thank you very much for your attention with my doubt.

In fact, I found your version of QuantumComputer.py, updated to python3, and with the problem of array solved.

This new version is working fine and no longer presents the problems I reported.

I'm doing several tests and comparing her results with the results of the IBM Quantum Experience simulator.

It has been a very gratifying and enriching experience.

I just changed two lines of the font to remove a warning from python3, replacing "==" with "is".

MVCM if answer_state == None:

If answer_state is None:,

For almost 30 years I have been working as Systems Analyst, with several programming languages ​​and different technological platforms.

But today, my studies are focused on Philosophy, and in this case, in binary mode as we thought about 3000 years ago (truth / lie, right / wrong), and how this binary mode of thinking is incompatible with quantum computing.

Our studies will be part of a philosophical research that seeks to investigate a new way of thinking, a new human thinking.

I am between the two worlds, the exact sciences and the humanities, and so I was invited to participate in the research.

Once again, thank you for the attention and thank Dr. Christine by your work of dissemination of this knowledge.

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

(via Android 4.4.2 KitKat)

. . .

Em 30/03/2017 13:45, "Lucas Saldyt" notifications@github.com escreveu:

Hi, Our qc5.docx file looks a lot different than (my) https://github.com/LSaldyt/QuantumComputing QuantumComputer.py file (even aside from the changes you've had to make). Are you sure you're using the latest commit of LSaldyt/QuantumComputing/master ? This is the only place where the changes I've mentioned have occurred. There is an open pull request #6 https://github.com/corbett/QuantumComputing/pull/6 to merge this branch with the rest of the repository. If the problem is with the latest branch of my fork, I'm still glad to help.

Cheers,

Lucas

β€” You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/corbett/QuantumComputing/issues/4#issuecomment-290470362, or mute the thread https://github.com/notifications/unsubscribe-auth/AZdw64q5-JUnp38Nb9RfV_AWROYsS4Pbks5rq9wXgaJpZM4LlMrA .

marcus-mello commented 7 years ago

Hello Lucas,

Are the qubits in |psi>=|100> from Probability.pretty_print_probabilities 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())

LSaldyt commented 7 years ago

Hi Marcus,

I'll look into this. But for reference, the diff of the porting commit only shows the addition of parentheses to the print calls in pretty_print_probabilities.

So, it looks to me like the problem isn't coming from that function. I'll look into this further

Diff reference:

@staticmethod
def pretty_print_probabilities(state):
[...]
-    print am_desc[0:-1]
+        print(am_desc[0:-1])
[...]
-    print pr_desc
+        print(pr_desc)
[...]
-        print "<state>=%f" % float(probs[0]+probs[3]-probs[1]-probs[2])
+            print("<state>=%f" % float(probs[0]+probs[3]-probs[1]-probs[2]))

Running your example on the latest master branch of github.com/LSaldyt/QuantumComputing:

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

Running the same example on the latest master branch of github.com/corbett/QuantumComputing:

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

For me at least, the output is entirely identical in both the Python2 and Python3 versions of the software. If you're asking about the inner workings of QuantumComputer.py, you're probably better off asking @corbett, since she authored the software. All I did was port it, and I only have marginal knowledge of quantum circuitry and this particular software implementation.

Thanks,

Lucas

marcus-mello commented 7 years ago

Oh Lucas,
I did not think at all that his adjustments were responsible for the odd result in reverse order. Sorry if I expressed myself in a very unclear way. I'm not used to the GitHub platform, and I do not know exactly how, or who, to report on the improvement of the source code. I know Dr. Christine Corbett is the author, but I imagine she has very little time to deal with this apparently finished project. I really appreciate your attention, and I'll try to get in touch with Dr. Christine. Thanks!

LSaldyt commented 7 years ago

No worries, and good luck contacting Dr. Christine.

Tell me if I can help you further

Thanks,

Lucas

rtvuser1 commented 6 years ago

The original issue , regarding port to Python 3, appears to have been solved. Thank you