# What would a very simple quantum program look like?

After reading the "first programmable quantum photonic chip". I was wondering just what software for a computer that uses quantum entanglement would be like.

Is there any example of code for specific quantum programming? Like pseudocode or high-level language? Specifically, what's the shortest program that can be used to create a Bell state $$\left|\psi\right> = \frac{1}{\sqrt 2} \left(\left|00\right> + \left|11\right> \right)$$ starting from a state initialised to $\left|\psi_0\right> = \left|00\right>$ using both a simulation and one of IBM's Quantum Experience processors, such as the ibmqx4?

Making the conceptual jump from traditional programming to entanglement isn't that easy.

I've found C's libquantum too.

Assuming you are considering a gate-based quantum computer, the most easy way to produce an entagled state is to produce one of the Bell states. The following circuit shows the Bell state $\left| \Phi^+ \right>$.

By examining $\left| \psi_0 \right>$, $\left| \psi_1 \right>$ and $\left| \psi_2 \right>$ we can determine the entagled state after application of all gates:

1. $\left| \psi_0 \right>$:

Not much happens here since no gates were applied at this point. The state of the whole system is therefore just the tensorproduct of the single states which we write like this: $$\left| \psi_0 \right> = \left | 0 0 \right >$$

2. $\left| \psi_1 \right>$:

The Hadamard-Gate applies on the first qubit which results in the following:

$$\left| \psi_1 \right> =(H \otimes I)\left | 0 0 \right > = H\left | 0 \right > \otimes \left | 0 \right > = \frac{1}{\sqrt 2} \left (\left | 0 \right > + \left | 1 \right > \right ) \left | 0 \right > = \frac{1}{\sqrt 2} \left (\left | 0 0 \right > + \left | 1 0 \right > \right )$$

3. $\left| \psi_2 \right>$:

Now a CNOT gate is applied and flips the second qubit but only where the first one has the value 1. The result is

$$\left| \psi_2 \right> = \frac{1}{\sqrt 2} \left (\left | 0 0 \right > + \left | 1 1 \right > \right )$$

This last state $\left| \psi_2 \right>$ is an entagled state and usually the most natural way to come up with such a situation. Bell states occure in a lot of interesting quantum algorithms such as super dense coding or teleportation.

Although the approach above might not seem like programming to you in a usual sense, applying gates to states is basically how programming a gate-based quantum computer works. There exists abstraction layers that allow you to perform high-level programming but translate the commands to the application of gates. The IBM Quantum Experience interface provides such features.

In a language like Microsoft's Q# the above example could look similar to this:

operation BellTest () : ()
{
body
{
// Use two qubits
using (qubits = Qubit[2])
{
Set (One, qubits[0]);
Set (Zero, qubits[1]);

// Apply Hadamard gate to the first qubit
H(qubits[0]);

// Apply CNOT gate
CNOT(qubits[0],qubits[1]);
}
}
}


A more detailed version (including measurement) can be found here: Microsoft: Writing a Quantum Program.

One way of writing quantum programs is with QISKit. This can be used to run the programs on IBM's devices. The QISKit website suggests the following code snippet to get you going, which is an entangled circuit as you want. It is also the same process as in the answer by datell. I'll comment on it line-by-line.

# import and initialize the method used to store quantum programs
from qiskit import QuantumProgram
qp = QuantumProgram()
# initialize a quantum register of two qubits
qr = qp.create_quantum_register('qr',2)
# and a classical register of two bits
cr = qp.create_classical_register('cr',2)
# create a circuit with them which we call 'Bell'
qc = qp.create_circuit('Bell',[qr],[cr])
# apply a Hadamard to the first qubit
qc.h(qr[0])
# apply a controlled not with the first qubit as control
qc.cx(qr[0], qr[1])
# measure the first qubit and store its result on the first bit
qc.measure(qr[0], cr[0])
# the same for the second qubit and bit
qc.measure(qr[1], cr[1])
# run the circuit
result = qp.execute('Bell')
# extract the results
print(result.get_counts('Bell'))


Note that the 'execute' command here only specifies the program to run. All other settings, such as the device you want to use, the number of times you want to repeat it to get statistics, etc are set to their default values. To run on ibmqx4 for 1024 shots, you can instead use

results = qp.execute(['Bell'], backend='ibmqx4', shots=1024)


The simplest quantum program I can think of is a (1-bit) true random number generator. As a quantum circuit, it looks like this:

You first prepare a qubit in the state $$|0 \rangle$$, then apply a Hadamard gate to produce the superposition $$\frac{\sqrt{2}}{2} ( \left| 0 \right> + \left| 1 \right> )$$ which you then measure in the computational basis. The measurement's result is $$|0\rangle$$ or $$|1\rangle$$, each with a probability of 50%.