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]
Model
is 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, 1 is a scalar)
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(scalar_input, dim) None [source]
Encodes the scalar input in the correspondent qubit.
Example
To encode a sequence of input vectors, given tau and n:
for t in range(model.tau): # loop through time for dim in range(model.n): # loop through dimensions model.encode(sequence[t][dim], dim)
- get_density_matrix() ndarray [source]
Returns the simulated density matrix of the model.
- Returns:
Model’s density matrix.
- Return type:
numpy.ndarray
- get_statevector() ndarray [source]
Returns the simulated state vector of the model.
- Returns:
Model’s state vector.
- Return type:
numpy.ndarray
- measure(shots=1, backend: Backend | None = None) Dict[str, int] [source]
Measures the qubits using a IBMQ backend
- Parameters:
shots (int) – Number of times to repeat the measurement shot
backend (qiskit Backend) – Quantum backend for the execution (if None, AER simulator is chosen as default)
- Returns:
State occurrences counts in the form {“state”: count}
- Return type:
dict
- plot_state_mat() None [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(model.n): # 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]
AngularModel
is a kind ofModel
which encodes the perceptual information in the angle of the qubits’ Bloch sphere representations- decode()[source]
The decoding for the
AngularModel
is a single measurement.- Returns:
The string label corresponding to the decoded state
- Return type:
str
- encode(scalar_input, dim)[source]
Encodes the scalar 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 (values between
0
andn-1
)
- 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]
LinearModel
corrects theAngularModel
the encoding, allowing a linear decoding for single-event sequencies (tau=1).Warning
The current implementation of
LinearModel
provides 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(scalar_input, dim)[source]
Encodes the scalar input in the correspondent qubit
- Parameters:
scalar_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 scalar input belongs.
- Returns:
The rotation angle applied to the qubit.
- Return type:
float