Models¶
qrobot.models module class diagram:
The Model abstract class¶
This class is the parent class which defines the general features a perception model has to implement. It is used to define custom models as a child classes:
from qrobot.models import Model
class myModel(Model):
(...)
-
class
qrobot.models.Model(n, tau)[source]¶ Modelis an abstract class which embeds the general features needed in a model for QL perception.- Parameters
n (int) – Model’s dimension ( must be greater than 0)
tau (int) – Number of samples of the temporal window (must be greater than 0)
-
n¶ Model’s dimension.
- Type
int
-
tau¶ Number of samples of the temporal window.
- Type
int
-
circ¶ Quantum circuit which implements the model.
- Type
qiskit.QuantumCircuit
-
abstract
encode(input, dim)[source]¶ Encodes the input in the correspondent qubit.
Example
To encode a sequence of input vectors, given tau and n:
for t in range(0,model.tau): # loop through time for dim in range(1, model.n + 1): # loop through dimensions model.encode(sequence[t][dim-1], dim)
-
get_density()[source]¶ Returns the simulated density matrix of the model.
- Returns
Model’s density matrix.
- Return type
numpy.ndarray
-
get_state()[source]¶ Returns the simulated state vector of the model.
- Returns
Model’s state vector.
- Return type
numpy.ndarray
-
measure(shots=1, backend=QasmSimulator(backend_name='qasm_simulator', provider=AerProvider()))[source]¶ Measures the qubits using a IBMQ backend
- Parameters
shots (int) – Number of measurement shots
backend (qiskit backend) – Quantum backend for the execution (QASM simulator as default)
- Returns
State occurrences counts in the form {“state”: count}
- Return type
dict
-
plot_state_mat()[source]¶ Plots the state and density matrix of the quantum system (just the real parts).
Example
To plot a perfectly balanced superposition of states:
model = Model(n, tau) # change Model with the desired child class for t in range(0,model.tau): # loop through time for dim in range(1, model.n + 1): # loop through dimensions model.encode(.5, dim) model.plot_state_mat()
- Raises
OverflowError – If the dimension of the model is 6 or greater, plotting fails due to the high number of basis states.
The AngularModel class¶
This is a custom Model provided by the package. This model encodes the perceptual information in
the angle of the qubits’ Bloch sphere representations.
-
class
qrobot.models.AngularModel(n, tau)[source]¶ AngularModelis a kind ofModelwhich encodes the perceptual information in the angle of the qubits’ Bloch sphere representations-
encode(input, dim)[source]¶ Encodes the input in the correspondent qubit
- Parameters
input (float) – The scalar input for a certain dimension, must be a number between 0 and 1 inclusive.
dim (int) – The model’s dimension which the input belongs.
- Returns
The rotation angle applied to the qubit.
- Return type
float
-
The LinearModel class¶
This is a custom model derived from AngularModel, which corrects it with a nonlinear encoding.
By means of its nonlinear encoding, this model provides a linear decoding
(a notebook
is provided in order to illustrate the difference between this and the angular one).
-
class
qrobot.models.LinearModel(n, tau)[source]¶ LinearModelcorrects theAngularModelthe encoding, allowing a linear decoding for single-event sequencies (tau=1).Notes
The current implementation of
LinearModelprovides a linear dependency between encoded input and decoding probabilities only for tau = 1 (or for tau > 1 if and only if the input is always the same one). For a time-varying sequencee (tau > 1) the results obtained are again nonlinear, and similar to the one ofAngularModel(see this notebook for more information)-
encode(input, dim)[source]¶ Encodes the input in the correspondent qubit
- Parameters
input (float) – The scalar input for a certain dimension, must be a number between 0 and 1 inclusive.
dim (int) – The model’s dimension which the input belongs.
- Returns
The rotation angle applied to the qubit.
- Return type
float
-
The BurstAModel class¶
This is a modified AngularModel which provides a burst instead of a
dictionary as decoding, which is the ration of qubits recorded in a |0> state
out of the dimension of the mode:
>>> AngularModel.decode(model)
{"state" = 1}
>>> BurstAModel.decode(model)
float
# E.g.
>>> AngularModel.decode(model)
{"111100" = 1}
>>> BurstAModel.decode(model)
2/6 = 0.2
-
class
qrobot.models.BurstAModel(n, tau)[source]¶ BurstAModelis a kind ofAngularModelwhich returns a float, rather than a dictionary, as the decoded output.-
decode()[source]¶ The decoding for the
BurstAModelis “burst” between 0 and 1, which corresponds to the sum of the “zeros” in the measured state divided by the dimension n of the model. For example, if the measure is {“11100” = 1} the decoded burst is 2/6 = 0.333333333333- Returns
The burst, a value between 0 and 1 (the nearer to 0, the closer to the basis state |00…0> )
- Return type
float
-