Introduction to Quantum Computer Programming
Programming quantum computers is fundamentally different from programming classical computers.
Today, quantum computers are programmed in the QASM language, an assembly-type language each qubit is manipuleted one line a time. This is similar to how traditional computers were programmed in the 1950s.
Quantum computers will not replace traditional computers. The way they work is quite different, meaning they can solve problems that were complicated for traditional computers. Quantum computers enables radically new ways of solving problems with computers. Quantum computers will touch Artificial intelligence, cryptography, phiysics and chemistry simulations, data organization, and more.
Bits versus Qubits
Traditional computers store bits data using transistors. These transistors can either be on or off, typically represented as 1 or 0.
Quantum computers use qubits - single atomic particles than can be in an "up" or "down" state - typically measured as 1 or 0. These particles can also be put into a superposition - a special state where they are both up and down at the same time.
Superposition
Superposition is special because it allows for controlled randomness. The qubit can be manipulated to have a known probablity of ending up in the up or down state.
The superposition state is unstable, and once measured will collapse back into a discrete up or down state, but the probablity of doing so is dependent on the properties of the qubit's superposition at that time.
An important point to note is that superposition results in true randomness where the probabilities can be controlled. This is considered impossible for a traditional computer and has interetesting implications for cryptography, wheere random numbers known as "nonces" are important.
A basic superposition can be accessed using the h
gate. When measured, the qubit will collapse into 0 or 1 with a 50% propability. This is similar to telling a traditional computer to preform round(rand(0, 1))
.
Entanglement
Two or more qubits can be entangled, meaning that what happens to one happens to the other. Once entangled, an action taken on one qubit affects the entangled qubits simultaneously. For example, if a qubit is put in superposition and entangled with another qubit, both qubits are then in the same superposition. When one is measured, it collapses randomly to 0 or 1. The entangled qubit simultaneously collapes to the same value.
Entanglement has interesting implications in encrypted communications, because the entangled qubits never touch or move, and measuring them breaks the entanglement. This means that a hacker would destroy a message by trying to read it.
An entanglement between two qubits can be performed with the cx
gate. This is similar to telling a traditional computer something like:
a = b;
By combining superposition and entanglement to create more interesting outcomes.
One or more entangled qubits can also be manipulated so that they have different superposition properties, yet affect each other. For instance if two qubits are entangled and one is set to be the Pauli-X (a type of not gate) of the other, than when one is mearused, the other qubit reports the opposite value.
In this example, one qubit is put into superposition with the h
gate, then entangled with another using the cx
gate, then the other qubit's superposition is inverted with the x
gate, like this:
h q[0]; cx q[0], q[1]; x q[1];
This is similar to telling a classical computer something like:
a = round(rand(0, 1)); b = !a;
Running Iterative Tests
On a quantum computer it is typically useful to run the same code many times, because often the code include randomness and the programmer typically wants to see how often the quantum computer settles on discrete outputs.
For this reason you may want to alter the "# iterations" field before running a program. The raw data table will show the output binary values each time the simulator ran the code, and the summary table will show the percent each unique binary value was reached.
About the Simulator
There are a few quantum computers today. They are amazing, yet challenging to use because there is often a queue or limited qubits. This simulator was designed to allow users to program and test simple quantum computer programs without needing to wait for their code to run on someone else's servers.
This simulator is what's known as an "ideal quantum computer," meaning that any and all qubits can be entagled and qubits don't collapse from superposition unexpectedly. A real quantum computer has physical limitations on which qubits can be entangled and for how long superposition states can be maintained. Code you write here may need to be altered slightly to work on a real quantum computer.
This simulator runs on your web browser in JavaScript, meaning that you don't need a web server or an account somewhere to run it. You do not share this simulator with other people. It is yours to enjoy. Be careful, Your computer will run out of memory if you try to create too many qubits.