Git Product home page Git Product logo

quantumcomputing's Introduction

Quintuple

This is an implementation of IBM's Quantum Experience in simulation; a 5-qubit quantum computer with a limited set of gates "the world’s first quantum computing platform delivered via the IBM Cloud". Their implementation is available at http://www.research.ibm.com/quantum/.

This code allows you to execute code printed from the Quantum Composer in the following syntax:

description usage
available qubit list q[0], q[1], q[2], q[3], q[4]
1-qubit gate list h,t,tdg,s,sdg,x,y,z,id
1-qubit gate action gate q[i];
2-qubit CNOT gate list cx
2-qubit CNOT gate action cx q[control], q[target];
measurement operation list measure, bloch
measurement operation action operation q[i];

It is much easier to dig into the internals of how the quantum computer computes by seeing and tracing the linear algebra representation of gates and states and their interactions as desired–for IBM's examples or for one's own code.

100% of the examples on the IBM tutorial are provided here, tested for and supported, and many addition tests and examples are provided. In fact, the implementation of the 5-qubit quantum computer simulator is only 675 lines, with approximately twice as many lines of test programs and examples provided.

Check out any of the test functions for example usage, and the Programs class contains many example programs in IBM's syntax all available in one place.

If you make use of this work, please cite my paper available on the physics arXiv as Quintuple: a Python 5-qubit quantum computer simulator to facilitate cloud quantum computing. For the making of story of this code, along with some pointers to resources on quantum computation, check out my blog post.

Example usage

from QuantumComputer import *
ghz_example_code="""h q[0];
		h q[1];
		x q[2];
		cx q[1], q[2];
		cx q[0], q[2];
		h q[0];
		h q[1];
		h q[2];"""
qc=QuantumComputer()
qc.execute(ghz_example_code)
Probability.pretty_print_probabilities(qc.qubits.get_quantum_register_containing("q0").get_state())

This will print

|psi>=0.70710678118654724|000>+-0.70710678118654724|111>
Pr(|000>)=0.500000; Pr(|111>)=0.500000; 

Or, using the swap Qubits example IBM tutorial Section IV, Page 2

swap_example_code="""x q[2];
		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];
		measure q[1];
		measure q[2];"""
qc.reset()
qc.execute(swap_example_code)
Probability.pretty_print_probabilities(qc.qubits.get_quantum_register_containing("q2").get_state())

will print

|psi>=|10>
Pr(|10>)=1.000000; 
<state>=-1.000000

We'll continue with this example in pure python below.

Note: using IBM's measurment code measure q[0]; will actually collapse the state, but for convenience the internal state before collapse is stored in qubit.get_noop(). Nature doesn't give this to us, but I can give it to you!

Pure python quantum computing machinery

Quantum computing operations can also be done in pure python, either with the QuantumComputer machinery or by directly manipulating gates.

QuantumComputer machinery

# Swap Qubits example IBM tutorial Section IV, Page 2
qc=QuantumComputer()
qc.apply_gate(Gate.X,"q2")
qc.apply_two_qubit_gate_CNOT("q1","q2")
qc.apply_gate(Gate.H,"q1")
qc.apply_gate(Gate.H,"q2")
qc.apply_two_qubit_gate_CNOT("q1","q2")
qc.apply_gate(Gate.H,"q1")
qc.apply_gate(Gate.H,"q2")
qc.apply_two_qubit_gate_CNOT("q1","q2")
qc.measure("q1")
qc.measure("q2")
Probability.pretty_print_probabilities(qc.qubits.get_quantum_register_containing("q1").get_state())

Will print

|psi>=|10>
Pr(|10>)=1.000000; 
<state>=-1.000000

Working with Individual States and gates

Note that states are combined by using the Kronecker product. Gates that operate on entangled states are composed from single qubit gates by the Kronecker product of the gate with the Identity. See the internals of qc.apply_gate or qc.apply_two_qubit_gate_CNOT for general examples, or feel free to use them instead.

# Swap Qubits example IBM tutorial Section IV, Page 2
q1=State.zero_state
q2=State.zero_state
q2=Gate.X*q2
new_state=Gate.CNOT2_01*np.kron(q1,q2)
H2_0=np.kron(Gate.H,Gate.eye)
H2_1=np.kron(Gate.eye,Gate.H)
new_state=H2_0*new_state
new_state=H2_1*new_state
new_state=Gate.CNOT2_01*new_state
new_state=H2_0*new_state
new_state=H2_1*new_state
new_state=Gate.CNOT2_01*new_state
Probability.pretty_print_probabilities(new_state)

Will print

|psi>=0.99999999999999967|10>
Pr(|10>)=1.000000;
<state>=-1.000000

This final manner of working with the library provides the most complete mathematical understanding of what's going on. Any individual state or gate can be printed, and it is clear how entanglement is represented as this is not done under the hood in this scenario.

quantumcomputing's People

Contributors

chandmer avatar corbett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

quantumcomputing's Issues

Seperate state test fails

Error message on my machine (Ubuntu 16, Python 2.7.12):

======================================================================
ERROR: test_separate_state (__main__.TestMultiQuantumRegisterStates)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "QuantumComputer.py", line 1614, in test_separate_state
    self.assertTrue(np.allclose(value_state,target_state))
  File "/home/lucas/.local/lib/python2.7/site-packages/numpy/core/numeric.py", line 2478, in allclose
    res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
  File "/home/lucas/.local/lib/python2.7/site-packages/numpy/core/numeric.py", line 2560, in isclose
    return within_tol(x, y, atol, rtol)
  File "/home/lucas/.local/lib/python2.7/site-packages/numpy/core/numeric.py", line 2543, in within_tol
    result = less_equal(abs(x-y), atol + rtol * abs(y))
TypeError: __array_prepare__ must return an ndarray or subclass thereof which is otherwise identical to its input

----------------------------------------------------------------------
Ran 45 tests in 6.081s

FAILED (errors=1)

pip list:

lucas@qed:~/Projects/QuantumComputing$ pip list
adium-theme-ubuntu (0.3.4)
Cython (0.23.4)
numpy (1.12.0)
pip (8.1.1)
Reindent (0.1.1)
setuptools (20.7.0)
unity-lens-photos (1.0)
wheel (0.29.0)

Full output of the command python2 QuantumComputer.py:
output.txt

Port to Python 3.0

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 ?

Measurement error in the README.md?

README.md says (example "Pure python quantum computing machinery"):

qc.measure("q1")
qc.measure("q2")
Probability.pretty_print_probabilities(qc.qubits.get_quantum_register_containing("q1").get_state())

If you do so, the last line always output:

|psi>=|00>
Pr(|00>)=1.000000; 
<state>=1.000000

Because measurement destroyed the state. IMHO, it should be get_noop instead of get_state.

Found it very hard to do simple computing programming

I can run it even in Pythonista. But given the large size of the program, it will be better if one can have separate files to run it. So far not successful and have to run my qm as test by cloning one of the test as source. And sometimes it does not work. I will give example whilst I continue he struggle. Js has an drag and drop n ipad. Hope python has one with Pythonista even if just text.

one major on missing some key operator like cz and one minor on readme

Minor: On the section about "Working with Individual States and gates", one minor issue is that the qc has to be either "qc.reset()" or redefine "qc = ...". I suspect the other issue can be handled.

Major: try to reproduce the circuit of teleport from quirk to composer. It can be done on the IBM openqasm 2.0 composer but not here. The issue is that there is no cz operator. I suspect one can amend these lines but not sure I am expert enough to try.

                ...
		cnot_re=re.compile('^cx (q\[[0-4]\]), (q\[[0-4]\])$')      # <-- need to change to (x|y|z)
		for l in lines:
			l=l.strip()
			if not l: continue
			# CNOT operates on two qubits so gets special processing
			cnot=cnot_re.match(l)
			if cnot:
				control_qubit=cnot.group(1)  #<-- group(2)
				target_qubit=cnot.group(2)   #<-- group(3)
                                # depends upon group(1) and feed group(1) into the function
				l='self.apply_two_qubit_gate_CNOT(%s,%s'%(control_qubit,target_qubit) #<-- major change needed
			for k,v in translation:
				l=l.replace(k,v)
			l=l+')'
			# Now running the code
			exec(l,globals(),locals())
                        ...

Somehow the cx in the first line need to increase to cover cy and cz and then apply the appropriate logic to it (the line l="..." for the "translation".

The regular expression might be easily fixed by changing into something like

cnot_re=re.compile('^c(x|y|z} (q\[[0-4]\]), (q\[[0-4]\])$')

and some change in logic of the "if cnot" so to use group 2 and 3 not 1 and 2 and use group 1 to handle cx/cy/cz

But the logic under self.apply_two_qubit_gate_CNOT is not that easy as it involved different way to handle cx/cy/cz gate change.

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

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())

Quantum Phase Estimation not working so well

Hello Dr. Christine, and here we are go... :)

Following the tests suggested in the Full User Guide from IBM (https://quantumexperience.ng.bluemix.net/qstage/#/user-guide), I'm in Section IV, Quantum Algorithms, specifically in Phase estimation algorithm.

The "Example circuit" demonstrates quantum phase estimation, setting qubit Q2 as target and qubit Q3 as a pointer.

score

ibm phase estimation

This is the result from IBM Simulator:
resutl

The result expected is |01000> (in 43210 order) or |00010> (in 01234 order) where the pointer qubit Q3 has always the value |1> that must refer to phase -1 of target qubit Q2.

But, this same test executed under the QuantumComputer.py, results from different way:

pos q3

The used source code below has a Toffoli sample just to demonstrate the position of qubit Q4, that it actually is in order 01234.

Phase.txt

I commented the "measure" to let me see the bloch values:

bloch point

And the result is this:

pos q3b

Now, the weird thing...

Phase is working if Pointer qubit is LESS THAN Target qubit ! . . . . . . . ( 01234 order )

tab pointer

This source code sample do four subsequent phase gates and works well, since I use the pattern POINTER < TARGET...

Phase_Pointer gt Target.txt

pos q3c

Well ... this issue is very away from my math knowledge, or Python, or about the construction of QuantumComputer.py... :(

I hope you, the Mrs. Matrix Architect :) , have some insight about how to address this and can match the results to those that IBM Simulator has.

Best Regards.

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

"Huston, CNOT have a situation..."

Actually in the last weeks I have spent many (almost all) of my free hours testing and trying to understand how QuantumComputer.py works, even to try to help in some way.

Maybe someone might ask, "But why? This app is just an experiment! ".

Exactly! Precisely for this reason, because it was designed and developed to study quantum logic.

QuantumComputer.py is a laboratory, and things do not always (almost never) go exactly as we planned in the lab experiments. In addition to the reasons for studying and researching, there is another: I love the idea of having a quantum computer in my pocket! :)

QuantumComputer.py is a complex and robust application, encoded in a just single file!
You can send it attached in a whatsapp message! Even though most people have no interest in it, or even understand what it is ... :)

But this study of Dr. Christine has the potential to be easily distributed, shared by students around the world, making quantum computing as popular in this early 21st century as the Abacus was 4,000 years ago.

Anyway, I have plenty of reasons to spend my free hours on this project. :)

Now, I found a weird result from a banal test.
May be this could be useful to solve some logic constraints of CNOT.

Follows the test done in the IBM Quantum Simulator:
Note that Q0 set to |1> with X gate.
x0_score

The QASM code:
x0_qasm

And the result from IBM Quantum Simulator:
x0_ibm_qe

IBM show it in binary order 43210 as “00001” (or “001”)
In reverse (natural) order 01234, the result will must be “10000” (or “100”)

But the QuantumComputer.py result is different... "010"
x0

This is the Python source code used in all tests:
CNOT Situation.txt

See the screen of source code used:
x0py

When the Q1 is set to |1>
x1_score

IBM show it in binary order 43210 as “00010” (or “010”)
In reverse (natural) order 01234, the result will must be “01000” (or “010”)
x1_ibm_qe

But the QuantumComputer.py result is different... "100"
x1

When the Q2 is set to |1>
x2_score

IBM show it in binary order 43210 as “00111” (or “111”)
In reverse (natural) order 01234, the result will must be “11100” (or “111”)
x2_ibm_qe

In this test QuantumComputer.py result is EQUAL to IBM ! "111"
x2

I hope these test cases can shed some light on the CNOT algorithm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.