Models

Model

class qrobot.models.Model(n: int, tau: int, backend: QuantumBackend | None = None)[source]

Base class for quantum-like perception models.

A model encodes an n-dimensional sequence over tau samples in a quantum circuit. Subclasses define the input-to-rotation mapping, query transformation, and decoding rule.

Parameters:
  • n (int) – Number of input dimensions. Each dimension is represented by one qubit.

  • tau (int) – Number of samples encoded in one temporal window.

n

Model’s dimension.

Type:

int

tau

Number of samples of the temporal window.

Type:

int

circ

Backend-specific circuit containing the encoded window.

Type:

object

clear() None[source]

Re-initialize the model with an empty circuit.

abstractmethod decode() str[source]

Measure and decode the model state as a basis-state label.

abstractmethod encode(scalar_input: float | int, dim: int) float[source]

Encode one normalized input in the qubit for dim.

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]

Return the simulated density matrix of the model.

Returns:

Model’s density matrix.

Return type:

numpy.ndarray

get_statevector() ndarray[source]

Return the simulated state vector of the model.

Returns:

Model’s state vector.

Return type:

numpy.ndarray

measure(shots: int = 1) dict[str, int][source]

Measure the qubits using the configured backend.

Parameters:

shots (int) – Number of times to repeat the measurement shot

Returns:

State occurrences counts in the form {“state”: count}

Return type:

dict

plot_state_mat() None[source]

Plot the real parts of the state vector and density matrix.

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.

print_circuit() None[source]

Print the quantum circuit on which the model is implemented.

abstractmethod query(target_vector: Sequence[float | int] | float | int) None[source]

Change basis so target_vector maps to state |00…0>.

AngularModel

class qrobot.models.AngularModel(n: int, tau: int, backend: QuantumBackend | None = None)[source]

Encode normalized inputs as Bloch-sphere rotation angles.

Each sample contributes scalar_input * pi / tau to the qubit assigned to its input dimension.

decode() str[source]

Decode the model with one computational-basis measurement.

Returns:

Measured basis-state bit string.

Return type:

str

encode(scalar_input: float | int, dim: int) float[source]

Encode one scalar input as a fractional y-axis rotation.

Parameters:
  • scalar_input (float) – Normalized input in the interval [0, 1].

  • dim (int) – Zero-based input dimension.

Returns:

The rotation angle applied to the qubit.

Return type:

float

query(target_vector: Sequence[float | int] | float | int) None[source]

Change basis so target_vector maps to state |00…0>.

Parameters:

target_vector (list) – Normalized target value for every model dimension.

LinearModel

class qrobot.models.LinearModel(n: int, tau: int, backend: QuantumBackend | None = None)[source]

Map a single normalized input linearly to measurement probability.

Warning

For tau == 1, measuring 1 has probability scalar_input. A constant input repeated over a longer window has the same relationship. Time-varying windows accumulate inverse-sine rotation angles, so their measurement probability is generally not the arithmetic mean of the inputs.

encode(scalar_input: float | int, dim: int) float[source]

Encode one scalar input using the linear-probability angle map.

Parameters:
  • scalar_input (float) – Normalized input in the interval [0, 1].

  • dim (int) – Zero-based input dimension.

Returns:

The rotation angle applied to the qubit.

Return type:

float