## Getting started with qibo

Let's start importing qibo and the modules required for building our first circuit. We set the default numpy backend for this tutorial.

import qibo
from qibo.models import Circuit
from qibo import gates

qibo.set_backend("numpy")

After this, we can start to do quantum circuit simulation using qibo.

### Simulating the superposition

Now we can build up a simple circuit composed of one qubit and an Hadamard gate. This gate is such that, if applied to a $| 0 \rangle$ state, it returns the following superposition of the fundamental states:

$|\psi\rangle = \frac{1}{\sqrt{2}}\bigl(|0\rangle + |1\rangle \bigr).$

At the end of the circuit we put a measurement gate, in order to be able to project the final state on the $z$ direction and to perform a simple measurement operation.

# initialize the circuit
c = Circuit(1)

# draw circuit
print(c.draw())

q0: ─H─M─

Now we can execute the circuit $N_{shots}=1000$ times, and use the simulation tool for getting the results.

simulated_final_state = c(nshots=1000)

print(simulated_final_state)

(0.70711+0j)|0> + (0.70711+0j)|1>

With this execution we simulate the final state obtained applying an Hadamard gate on an initial $| 0 \rangle$ state.

#### Printing frequencies

Now we can use the frequencies method for visualizing the obtained results. One way to do that is to activate the binary=False option; thanks to this we get a counter object which shows us how many times the two fundamental states are registered after one thousand of executions.

simulated_final_state.frequencies(binary=False)

Counter({0: 510, 1: 490})

#### Printing probabilities

Alternatively, we can use the probabilities method.

# calculating probabilities
probabilities = simulated_final_state.probabilities

# showing the two probabilities
print(probabilities(qubits=[0]))

[0.5 0.5]

As expected, the probability of obtaining each of the two fundamental states is equal to 0.5.

you can repeat this experiment many times, but you will always get these probabilities as results.

for _ in range(10):
final_state = c(nshots=1000)
probabilities = simulated_final_state.probabilities
print(probabilities(qubits=[0]))

[0.5 0.5]
[0.5 0.5]
[0.5 0.5]
[0.5 0.5]
[0.5 0.5]
[0.5 0.5]
[0.5 0.5]
[0.5 0.5]
[0.5 0.5]
[0.5 0.5]

This is good, because we are dealing with a simulation and, in this particular case, we are simulating perfect measurements. There is a way in qibo to make the measurement more realistic. To verify this, we need to define the circuit again and this time we will enable the collapse=True option inside the port representing the measurement.

c_real = Circuit(1)

results = c_real(nshots=5)
results

[array([0.+0.j, 1.+0.j]),
array([1.+0.j, 0.+0.j]),
array([1.+0.j, 0.+0.j]),
array([1.+0.j, 0.+0.j]),
array([1.+0.j, 0.+0.j])]

This procedure provide us with a list of collapsed vectors. If we want to verify the difference between this method and the previous one, we need to perform five times the collapsing of one thousand states.

import numpy as np

for experiment in range(10):
prob = 0
for shot in range(1000):
# summing the real part of the first component of the final quantum state
prob += np.real(c_real()[0][0])
prob /= 1000
print(prob)

0.494
0.54
0.537
0.488
0.486
0.498
0.477
0.489
0.529
0.513

As we can see, in this case we get different values of probabilities, in accordance with the fluctuations that are recorded on real quantum devices.