Code Monkey home page Code Monkey logo

qusimpy's Introduction

QuSim.py

Build Status

Qusim.py is a toy multi-qubit quantum computer simulator, written in 150 lines of python

This code makes it easy for you to see how a quantum computer computes by following the linear algebra!

from QuSim import QuantumRegister

#############################################
#                 Introduction              #
#############################################
# Here Will Be A Few Example of Different
# Quantum States / Algorithms, So You Can
# Get A Feel For How The Module Works, and  
# Some Algorithmic Ideas


#############################################
#            Quantum Measurement            #
#############################################
# This experiment will prepare 2 states, of a
# Single qubit, and of 5 qubits, and will just
# Measure them

OneQubit = QuantumRegister(1)  # New Quantum Register of 1 Qubit
print('One Qubit: ' + OneQubit.measure())  # Should Print 'One Qubit: 0'

FiveQubits = QuantumRegister(5)  # New Quantum Register of 5 Qubits
# Should Print 'Five Qubits: 00000'
print('Five Qubits: ' + FiveQubits.measure())

#############################################
#                 Swap 2 Qubits             #
#############################################
# Here, We Will Apply a Pauli-X Gate / NOT Gate
# To the first qubit, and then after the algorithm,
# it will be swapped to the second qubit.

Swap = QuantumRegister(2)  # New Quantum Register of 2 qubits
Swap.applyGate('X', 1)  # Apply The NOT Gate. If Measured Now, it should be 10

# Start the swap algorithm
Swap.applyGate('CNOT', 1, 2)
Swap.applyGate('H', 1)
Swap.applyGate('H', 2)
Swap.applyGate('CNOT', 1, 2)
Swap.applyGate('H', 1)
Swap.applyGate('H', 2)
Swap.applyGate('CNOT', 1, 2)
# End the swap algorithm

print('SWAP: |' + Swap.measure() + '>')  # Measure the State, Should be 01

#############################################
#               Fair Coin Flip              #
#############################################
# Shown in this 'Experiment', is a so called 'Fair Coin Flip',
# Where a state will be prepared, that has an equal chance of
# Flipping to Each Possible State. to do this, the Hadamard
# Gate will be used.

# New Quantum Register of 1 Qubit (As a coin has only 2 states)
FairCoinFlip = QuantumRegister(1)
# If measured at this point, it should be |0>

# Apply the hadamard gate, now theres an even chance of measuring 0 or 1
FairCoinFlip.applyGate('H', 1)

# Now, the state will be measured, flipping the state to
# either 0 or 1. If its 0, we will say "Heads", or if its
# 1, we will say "Tails"
FairCoinFlipAnswer = FairCoinFlip.measure()  # Now its flipped, so we can test
if FairCoinFlipAnswer == '0':
    print('FairCoinFlip: Heads')
elif FairCoinFlipAnswer == '1':
    print('FairCoinFlip: Tails')

#############################################
#             CNOT Gate                     #
#############################################
# In this experiment, 4 states will be prepared, {00, 01, 10, 11}
# And then the same CNOT Gate will be run on them,
# To Show The Effects of the CNOT. The Target Qubit will be 2, and the control 1

# New Quantum Register of 2 Qubits, done 4 times.
# If any are measured at this time, the result will be 00
ZeroZero = QuantumRegister(2)
ZeroOne = QuantumRegister(2)
OneZero = QuantumRegister(2)
OneOne = QuantumRegister(2)

# Now prepare Each Into The State Based On Their Name
# ZeroZero Will be left, as thats the first state anyway
ZeroOne.applyGate('X', 2)
OneZero.applyGate('X', 1)
OneOne.applyGate('X', 1)
OneOne.applyGate('X', 2)

# Now, a CNOT Will Be Applied To Each.
ZeroZero.applyGate('CNOT', 1, 2)
ZeroOne.applyGate('CNOT', 1, 2)
OneZero.applyGate('CNOT', 1, 2)
OneOne.applyGate('CNOT', 1, 2)

# Print the results.
print('CNOT on 00: |' + ZeroZero.measure() + '>')
print('CNOT on 01: |' + ZeroOne.measure() + '>')
print('CNOT on 10: |' + OneZero.measure() + '>')
print('CNOT on 11: |' + OneOne.measure() + '>')

Largely based on the code from corbett/QuantumComputing.

If you are interested in a efficient, high performance, hardware accelerated quantum computer simulator written in Rust, please check out QCGPU

qusimpy's People

Contributors

adamisntdead 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

qusimpy's Issues

Question for the "C" matrix in *CONTROL NOT* condition

First, many thanks for the work, it is fantastic!

Could you please give me some explanation/source for why build the C matrix like this in CNOT situation?
I also tried for other combinations of the condition gates, like condition H or condition Y/Z, it all works good based on your idea.

Cheers

This Is Amazing, Probably

I'm sorry but when I try to run this code, an error comes up saying, " Traceback (most recent call last):
File "C:/Users/Omid/Desktop/shortcuts/Coding/Python Codes/Quantum Register By QuSimPy.py", line 1, in
from QuSim import QuantumRegister
ModuleNotFoundError: No module named 'QuSim' "
However, going over the rest of the code, this is amazing! Great job!

Extend QuSimPy with rotgates

The simulator works great, however, i would like to extend with rotation gates and U1, U2 and U3 gates. I have defined for U3

@staticmethod
def U3(theta,phi,lambd):
theta_mat=np.matrix([[np.cos(theta/2),np.sin(theta/2)],[-np.sin(theta/2),np.cos(theta/2)]])
phi_mat=np.matrix([[1,0],[0, e**(i phi)]])
lambd_mat=np.matrix([[1,0],[0,e
*(i lambd)]])
return phi_mat
theta_mat*lambd_mat

So now i can do the following
gateMatrix = gates.U3(pi/2,pi/3,0)
print(gateMatrix)

Result
[[ 0.70710678+0.j 0.70710678+0.j ]
[-0.35355339-0.61237244j 0.35355339+0.61237244j]]

How todo insert this in to the applygate ?

TypeError: only integer scalar arrays can be converted to a scalar index

Hi,

i'm trying to run example.py to play with your code but it crashes with the following stack trace:

$ python examples.py 
One Qubit: 0
Five Qubits: 00000
One Qubit (Correct Notation): |0>
Five Qubits (Correct Notation): |00000>
Traceback (most recent call last):
  File "examples.py", line 45, in <module>
    print('NOT Gate: |' + NOT.measure() + '>')
  File ".../QuSimPy/QuSim.py", line 147, in measure
    self.numQubits
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 2039, in binary_repr
    binary = bin(num)[2:]
TypeError: only integer scalar arrays can be converted to a scalar index

Is this a problem on my end?
I'm using Numpy 1.14.2, with either Python 2.7.12 or Python 3.5.2.

Thanks,
Daniele

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.