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 overtausamples 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
- abstractmethod encode(scalar_input: float | int, dim: int) float[source]
Encode one normalized scalar in a single model dimension.
Subclasses implement the mapping from a normalized input to their backend operation. Implementations apply that operation to
dimand return the numeric encoding parameter they used.
- encode_vector(input_vector: Sequence[float | int]) list[float][source]
Encode one input value for every model dimension.
This is the multidimensional counterpart of
encode(). The value at each vector index is passed to the model’sencode()implementation for the matching dimension.- Parameters:
input_vector (sequence of float or int) – Exactly one model-specific scalar input per dimension.
- Returns:
Numeric encoding parameters per dimension.
- Return type:
list of float
- Raises:
- 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 model.encode_vector([.5] * model.n) 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.
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 / tauto 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.
Use this method for one value in one dimension. Use
encode_vector()to encode a complete multidimensional sample at once.- 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
Examples
Encode one value in dimension zero:
model = AngularModel(n=2, tau=1) angle = model.encode(0.25, dim=0)
Encode both dimensions together:
angles = model.encode_vector([0.25, 0.75])
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, measuring1has probabilityscalar_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.
Use this method for one value in one dimension. Use
encode_vector()to encode a complete multidimensional sample at once.- 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
Examples
Encode one value in dimension zero:
model = LinearModel(n=2, tau=1) angle = model.encode(0.25, dim=0)
Encode both dimensions together:
angles = model.encode_vector([0.25, 0.75])