|
1 | 1 | # QuSim.py |
2 | 2 |
|
3 | | -A Multi-Qubit Quantum Computer Simulator. |
| 3 | +Qusim.py is a toy multi-qubit quantum computer simulator, written in 150 lines of python |
4 | 4 |
|
5 | | -TODO: Write This |
| 5 | +```python |
| 6 | +from QuSim import QuantumRegister |
| 7 | + |
| 8 | +############################################# |
| 9 | +# Introduction # |
| 10 | +############################################# |
| 11 | +# Here Will Be A Few Example of Different # |
| 12 | +# Quantum States / Algorithms, So You Can # |
| 13 | +# Get A Feel For How The Module Works, and # |
| 14 | +# Some Algorithmic Ideas # |
| 15 | +############################################# |
| 16 | + |
| 17 | +############################################# |
| 18 | +# Quantum Measurement # |
| 19 | +############################################# |
| 20 | +# This experiment will prepare 2 states, of a |
| 21 | +# Single qubit, and of 5 qubits, and will just |
| 22 | +# Measure them |
| 23 | + |
| 24 | +OneQubit = QuantumRegister(1) # New Quantum Register of 1 Qubit |
| 25 | +print('One Qubit: ' + OneQubit.measure()) # Should Print 'One Qubit: 0' |
| 26 | + |
| 27 | +FiveQubits = QuantumRegister(5) # New Quantum Register of 5 Qubits |
| 28 | +# Should Print 'Five Qubits: 00000' |
| 29 | +print('Five Qubits: ' + FiveQubits.measure()) |
| 30 | + |
| 31 | +############################################# |
| 32 | +# Swap 2 Qubits # |
| 33 | +############################################# |
| 34 | +# Here, We Will Apply a Pauli-X Gate / NOT Gate |
| 35 | +# To the first qubit, and then after the algorithm, |
| 36 | +# it will be swapped to the second qubit. |
| 37 | + |
| 38 | +Swap = QuantumRegister(2) # New Quantum Register of 2 qubits |
| 39 | +Swap.applyGate('X', 1) # Apply The NOT Gate. If Measured Now, it should be 10 |
| 40 | + |
| 41 | +# Start the swap algorithm |
| 42 | +Swap.applyGate('CNOT', 1, 2) |
| 43 | +Swap.applyGate('H', 1) |
| 44 | +Swap.applyGate('H', 2) |
| 45 | +Swap.applyGate('CNOT', 1, 2) |
| 46 | +Swap.applyGate('H', 1) |
| 47 | +Swap.applyGate('H', 2) |
| 48 | +Swap.applyGate('CNOT', 1, 2) |
| 49 | +# End the swap algorithm |
| 50 | + |
| 51 | +print('SWAP: |' + Swap.measure() + '>') # Measure the State, Should be 01 |
| 52 | + |
| 53 | +############################################# |
| 54 | +# Fair Coin Flip # |
| 55 | +############################################# |
| 56 | +# Shown in this 'Experiment', is a so called 'Fair Coin Flip', |
| 57 | +# Where a state will be prepared, that has an equal chance of |
| 58 | +# Flipping to Each Possible State. to do this, the Hadamard |
| 59 | +# Gate will be used. |
| 60 | + |
| 61 | +# New Quantum Register of 1 Qubit (As a coin has only 2 states) |
| 62 | +FairCoinFlip = QuantumRegister(1) |
| 63 | +# If measured at this point, it should be |0> |
| 64 | + |
| 65 | +# Apply the hadamard gate, now theres an even chance of measuring 0 or 1 |
| 66 | +FairCoinFlip.applyGate('H', 1) |
| 67 | + |
| 68 | +# Now, the state will be measured, flipping the state to |
| 69 | +# either 0 or 1. If its 0, we will say "Heads", or if its |
| 70 | +# 1, we will say "Tails" |
| 71 | +FairCoinFlipAnswer = FairCoinFlip.measure() # Now its flipped, so we can test |
| 72 | +if FairCoinFlipAnswer == '0': |
| 73 | + print('FairCoinFlip: Heads') |
| 74 | +elif FairCoinFlipAnswer == '1': |
| 75 | + print('FairCoinFlip: Tails') |
| 76 | + |
| 77 | +############################################# |
| 78 | +# CNOT Gate # |
| 79 | +############################################# |
| 80 | +# In this experiment, 4 states will be prepared, {00, 01, 10, 11} |
| 81 | +# And then the same CNOT Gate will be run on them, |
| 82 | +# To Show The Effects of the CNOT. The Target Qubit will be 2, and the control 1 |
| 83 | + |
| 84 | +# New Quantum Register of 2 Qubits, done 4 times. |
| 85 | +# If any are measured at this time, the result will be 00 |
| 86 | +ZeroZero = QuantumRegister(2) |
| 87 | +ZeroOne = QuantumRegister(2) |
| 88 | +OneZero = QuantumRegister(2) |
| 89 | +OneOne = QuantumRegister(2) |
| 90 | + |
| 91 | +# Now prepare Each Into The State Based On Their Name |
| 92 | +# ZeroZero Will be left, as thats the first state anyway |
| 93 | +ZeroOne.applyGate('X', 2) |
| 94 | +OneZero.applyGate('X', 1) |
| 95 | +OneOne.applyGate('X', 1) |
| 96 | +OneOne.applyGate('X', 2) |
| 97 | + |
| 98 | +# Now, a CNOT Will Be Applied To Each. |
| 99 | +ZeroZero.applyGate('CNOT', 1, 2) |
| 100 | +ZeroOne.applyGate('CNOT', 1, 2) |
| 101 | +OneZero.applyGate('CNOT', 1, 2) |
| 102 | +OneOne.applyGate('CNOT', 1, 2) |
| 103 | + |
| 104 | +# Print the results. |
| 105 | +print('CNOT on 00: |' + ZeroZero.measure() + '>') |
| 106 | +print('CNOT on 01: |' + ZeroOne.measure() + '>') |
| 107 | +print('CNOT on 10: |' + OneZero.measure() + '>') |
| 108 | +print('CNOT on 11: |' + OneOne.measure() + '>') |
| 109 | +``` |
| 110 | + |
| 111 | +Largely based on the code from [corbett/QuantumComputing](https://github.com/corbett/QuantumComputing). |
| 112 | + |
| 113 | +If you are interested in a efficient, high performance, hardware accelerated |
| 114 | +quantum computer simulator written in Rust, please check out [QCGPU](https://github.com/qcgpu/qcgpu-rust) |