• Stars
    star
    719
  • Rank 62,516 (Top 2 %)
  • Language
    Python
  • License
    GNU Affero Genera...
  • Created about 8 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Multi-Qubit Ideal Quantum Computer Simulator

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

More Repositories

1

DevMyMac

โœจ โœจ A Simple Tool To Setup A Mac for Developmentโœจโœจ
Shell
365
star
2

NoGithubDarkMode

A Chrome Extension to Remove the dark menu bar from GitHub.
CSS
67
star
3

wbpck-bundler

A Little Toy Javascript Module Bundler ๐ŸŽ
JavaScript
40
star
4

medium-toc

Easily create a table of contents for your Medium articles in just one click. โœจ
JavaScript
33
star
5

console.md

Ever dreamed of rendering Markdown in the console? No. Neither had I.
JavaScript
26
star
6

poke

A simple tool to check your site for broken links, media, iframes, stylesheets, scripts, forms or metadata.
JavaScript
25
star
7

Unity3D-Hypersolids

โ—ป๏ธ A Way To Create, Show, and Rotate 4D Objects in Unity 3D
C#
24
star
8

qics

Javascript Idealistic Quantum Computer SImulation Library
JavaScript
6
star
9

how-sound-works

An explainer video built with 3blue1brown's manim
Python
6
star
10

boilerplate

A Modern, Gulp Based, Front End Build Setup ๐Ÿ˜Š
CSS
4
star
11

wasm-starter

WebAssembly Module Starter Code, Powered by Rust, wasm-pack, wasm-bindgen and Webpack ๐Ÿ”ฅ๐Ÿ˜
HTML
4
star
12

self-hosted-services

A collection of docker-compose files for the services that I self host
Shell
4
star
13

DublinBus

Get the Realtime and Stop Information from the Dublin Bus REST API
JavaScript
3
star
14

theory-test-questions

A List Of Sample Questions And Answers For The Irish Driver Theory Test
JavaScript
3
star
15

Overflowing

๐Ÿ“™ Find Out How Much You Really Use Stack Overflow ๐Ÿ“™
JavaScript
3
star
16

wasm-markov

Markov Chains Implemented in Rust, Powered By WebAssembly ๐Ÿ”ฅ
Rust
2
star
17

Excerpt

Hassle Free Excerpts/Extracts For ExpressionEngine 3 and 4 ๐ŸŽ‰๐Ÿ’ป
PHP
2
star
18

TuneBot

A Facebook Messenger bot that takes the name of an Irish music tune, and gives suggestions as to what you could play with it. Powered by the Session.org
JavaScript
2
star
19

node-static-site

My talk for the NodeJS Dublin Meetup, April 2018โšก๏ธ
CSS
2
star
20

norton-rating

Get the safety rating of a site from Norton Safe Web
JavaScript
1
star
21

Hackr-News

๐Ÿ“ฐ A Hacker News Front Page clone in React
JavaScript
1
star
22

glassdoor-question-scrape

A small tool to scrape past interview questions from Glassdoor
JavaScript
1
star
23

obj-value

Safely access javascript property values
JavaScript
1
star
24

context-api-demo

A Small App Demonstrating React 16.3's Context API ๐Ÿ˜Š
JavaScript
1
star
25

Imagine

A Toy Complex Number Library in Javascript
JavaScript
1
star
26

rustfest-demos

Rust
1
star
27

Menubar-Boilerplate

A Simple Boilerplate for Menubar Apps
JavaScript
1
star
28

Files

Random Files to Host
1
star
29

gulp-inline-imagesize

A Gulp Task To Add Image Size Comments To Your HTML
JavaScript
1
star
30

pingdom-widget

A CraftCMS Widget To Show Stats From Pingdom On The Dashboard โฑ๐Ÿ”‹๐Ÿ”Œ
PHP
1
star
31

markov-chains

Markov Chains Implemented in Rust
Rust
1
star
32

lucas-lehmer

A Javascript Implementation of the Lucas-Lehmer Primality Test for Mersenne Primes
JavaScript
1
star
33

hasty

Automatically Create Presentations From Any Article
JavaScript
1
star
34

toy-cfr-solver

A toy implementation of CFR
C++
1
star
35

Change-HTTP

An Expression Engine Extension to change 'HTTP' to 'HTTPS'
PHP
1
star
36

Minimo

A Responsive, Minimal App Landing Page Template
CSS
1
star
37

personal-site

My Personal Site
HTML
1
star
38

context-api-talk

Slides For My Lightning Talk About The Context API For ReactJS Dublin โšก๏ธ
JavaScript
1
star
39

mental-math

A Speed Arithmetic Game
JavaScript
1
star
40

notes

My School Notes, Managed With Git
TeX
1
star
41

BusPebble

Dublin Bus Realtime Information from your Pebble
JavaScript
1
star
42

polls-party

Anonymous Location Based Polls
JavaScript
1
star
43

alfred-workflows

๐ŸŽฉ My collection of Alfred Workflows
1
star
44

vonbot

A discord bot for interacting with my personal problem database
1
star
45

snell

A Two.js Visualization of Snell's Law
JavaScript
1
star
46

random-value

Javascript Random Value Generation
JavaScript
1
star
47

tim

A Minimal Hugo Theme Based On Tim Holman's Site, tholman.com
CSS
1
star
48

polymarket-research

Jupyter Notebook
1
star
49

higher-shapes

Visualize n-Dimensional Polytopes From Your Browser.
JavaScript
1
star