A Quantum Computing Library in 48 Lines of Python: Part 2
In Part 1 we wrote a quantum computing library in 48 lines of Python.
Now that we have this library, let’s use it to do some quantum computing!
Basic Usage
Let’s import the library and do some basic operations.
from quantum_library import QuantumState, QuantumOperation
A quantum state is created by calling QuantumState with a unit length complex vector.
state = QuantumState([1, 0])
A quantum operation is created by calling QuantumOperation with a unitary matrix.
operation = QuantumOperation([
[2**(-1/2), 2**(-1/2)],
[2**(-1/2), -2**(-1/2)],
])
We can apply the operation to the state to get a new state.
new_state = operation.apply(state)
We can measure states, which will collapse the state into one of its observable states, mutating it, and return the measurement outcome.
measurement = new_state.measure()
We can compose independent states together to form a larger system.
state1 = QuantumState([1, 0])
state2 = QuantumState([0, 1])
combined_state = state1.compose(state2)
We can do the same for operations. In this example we compose an operation with itself to represent applying the same operation to both systems in the larger system.
combined_operation = operation.compose(operation)
Here’s some example basic usage of the library.
CHSH Game
Consider the following game. Alice and Bob are each given one bit, and they each respond with one bit. If either of them gets a 0 they win by agreeing, if they both get 1 they win by disagreeing.
Let’s add a twist to this game. Alice and Bob take a brief space voyage before giving their answers. Alice will be on Venus when she gets her input, and Bob will be on Mars. They will not be able to coordinate their answers after getting their input because they need to send their response back to Earth within a short time period. It’s not just against the rules for them to communicate, it’s physically impossible due to the finite speed of light.
What would make a good strategy for Alice and Bob? One possible strategy is to always output 1, regardless of input. Then they will win whenever either one of them receives a 0. If the inputs are given at random, they’ll win 75% of the time. Another strategy would be for Bob to always output 1, and for Alice to output 1 if her input is 1, and 0 otherwise. This also has a 75% chance of winning.
It turns out that when the input is random, 75% is the best you can do classically.
Let’s now make the game more interesting. Alice and Bob can prepare and share an entangled quantum state which they’ll bring with them on their voyage. They still cannot communicate, the finite speed of light totally forbids communication. They can, however, coordinate their answers better and win with more than 75% probability.
If Alice an Bob share a maximally entangled pair of qubits in the state 1/√2 (up, up) + 1/√2 (down, down), and they keep it in this state until they learn their input, at which point they each apply a unitary as follows:
Then they measure their qubits and answer 0 if their qubit was up and 1 if their qubit was down. Then the claim is that they will do better than is physically possible classically. The following program simulates 10,000 iterations of the game.
Here’s the output of running this a few times.
They won this many times: 8558 (85.58%)
They won this many times: 8516 (85.16%)
They won this many times: 8544 (85.44%)
They won this many times: 8588 (85.88%)
They won this many times: 8586 (85.86%)
If there is interest, I will do a Part 3 with Shor’s Algorithm for factoring.