Example

The sources of the demo programs are fairly complex. A very simple problem is given here to show the general techniques when programming with libquantum. This program implements a quantum random number generator, that returns either 0 or 1 with equal probability.

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <quantum.h>

int main ()
{
  quantum_reg reg;
  int result;

  srand(time(0));

  reg = quantum_new_qureg(0, 1);

  quantum_hadamard(0, &reg);

  result = quantum_bmeasure(0, &reg);

  printf("The Quantum RNG returned %i!\n", result);

  return 0;
}

In line 4 the header file for libquantum gets included. A quantum register named reg is declared in line 8. quantum_new_qureg initializes the quantum register with a state of $\vert\rangle$ and with one qubit in it. On this qubit, a Hadamard gate is applied, changing the state to $\frac{1}{\sqrt{2}}\vert\rangle + \frac{1}{\sqrt{2}}\vert 1\rangle$. A measurement destroys the superposition of the qubit, and the result of the measurement is printed.