Models

qrobot.models module class diagram:

Inheritance diagram of qrobot.models.Model, qrobot.models.AngularModel, qrobot.models.LinearModel, qrobot.models.BurstAModel


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)

  • 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

clear()[source]

Re-initialize the model with an empty circuit.

abstract decode()[source]

Exploits the information encoded in the qubit (abstract class).

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.

print_circuit()[source]

Prints the quantum circuit on which the model is implemented.

abstract query(target)[source]

Changes the basis of the quantum system choosing target as the basis state |00…0>.


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 of Model 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.

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

query(target)[source]

Changes the basis of the quantum system choosing target as the basis state |00…0>

Parameters

target (list) – The target state, it must be a list containing n floats (between 0 and 1 inclusive).


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 the AngularModel the encoding, allowing a linear decoding for single-event sequencies (tau=1).

Notes

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 of AngularModel (see this notebook for more information)

decode()[source]

The decoding for the LinearModel is a single measurement.

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

query(target)[source]

Same query function as the AngularModel


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]

BurstAModel is a kind of AngularModel which returns a float, rather than a dictionary, as the decoded output.

decode()[source]

The decoding for the BurstAModel is “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


Module variables

qrobot.models.model.QASM_BACKEND = QasmSimulator( backend_name='qasm_simulator', provider=AerProvider())

Module-level qiskit backend variable for quantum circuit simulation on local classical hardware via QASM simulator.

Type

qiskit backend