vc.models module
This module is used to define quantum models.
To add a new model, you should implement the QModel
interface and update get_model()
.
- class vc.models.ExpectedValuesModel(backend, n_classes, n_layers=2, n_trainable_sublayers=2, scaling=0.5)[source]
Bases:
QModel
Model that uses angle encoding and expected values.
- __init__(backend, n_classes, n_layers=2, n_trainable_sublayers=2, scaling=0.5)[source]
Init ExpectedValuesModel.
This model implements a unitary \(U(x)\) of the form:
\[U(x) = W_{L + 1} \cdot S(x) \cdot W_{L} \cdot \ldots \cdot W_{2} \cdot S(x) \cdot W_{1}\]where:
\(x\) is the data to encode,
\(S\) is an encoding circuit,
\(W\) is a trainable circuit.
- Parameters:
backend (
Dict
[str
,Union
[str
,Any
]]) – see docstring ofQModel
.n_classes (
int
) – see docstring ofQModel
.n_layers (
int
) – number of layers in \(U(x)\) (equal to \(L\)).n_trainable_sublayers (
int
) – number of layers for each \(W\).scaling (
float
) – scaling to apply to the preprocessed data, seepreprocess()
.
- get_init_weights(random, random_state)[source]
Get init weights between 0 and \(2\pi\).
- Return type:
ndarray
[Any
,dtype
[float64
]]
- class vc.models.ModuloModel(backend, n_classes, n_layers=2, n_trainable_sublayers=2, scaling=0.5)[source]
Bases:
ProbabilitiesModel
Model that uses post-processing with modulo.
See docstring of
vc.models.ProbabilitiesModel
.The post-processing strategy assigns a class to an \(n\)-bit string \(b\) according to the following formula:
\[f(b) = \left[b\right]_{10} \mod M\]where:
\(M\) is the number of classes,
\([\cdot]_{10}\) is the decimal representation of the argument.
- class vc.models.ParityModel(backend, n_classes, n_layers=2, n_trainable_sublayers=2, scaling=0.5)[source]
Bases:
ProbabilitiesModel
Model that uses parity post-processing.
See docstring of
vc.models.ProbabilitiesModel
.The post-processing strategy assigns a class to an \(n\)-bit string \(b\) according to the following formula:
\[f(b) = \left[b_0 ... b_{m-2}\left(\bigoplus_{i=m-1}^{n-1} b_i\right) \right]_{10}\]where:
\(m=\lceil \log_2(M) \rceil\) with \(M\) being the number of classes,
\(n\) is the number of bits,
\([\cdot]_{10}\) is the decimal representation of the argument.
Reference: “Quantum Policy Gradient Algorithm with Optimized Action Decoding” by Meyer et al.
- class vc.models.ProbabilitiesModel(backend, n_classes, n_layers=2, n_trainable_sublayers=2, scaling=0.5)[source]
Bases:
QModel
,ABC
Generic model that uses probabilities.
- __init__(backend, n_classes, n_layers=2, n_trainable_sublayers=2, scaling=0.5)[source]
Init ProbabilitiesModel.
This model implements a unitary \(U(x)\) of the form:
\[U(x) = W_{L + 1} \cdot S(x) \cdot W_{L} \cdot \ldots \cdot W_{2} \cdot S(x) \cdot W_{1}\]where:
\(x\) is the data to encode,
\(S\) is an encoding circuit,
\(W\) is a trainable circuit.
- Parameters:
backend (
Dict
[str
,Any
]) – see docstring ofQModel
.n_classes (
int
) – see docstring ofQModel
.n_layers (
int
) – number of layers in \(U(x)\) (equal to \(L\)).n_trainable_sublayers (
int
) – number of layers for each \(W\).scaling (
float
) – scaling to apply to the preprocessed data, seepreprocess()
.
- get_init_weights(random, random_state)[source]
Get init weights between \(0\) and \(2\pi\).
- Return type:
ndarray
[Any
,dtype
[float64
]]
- class vc.models.QModel(backend, n_classes)[source]
Bases:
ABC
Abstract base class for quantum models.
- __init__(backend, n_classes)[source]
Init QModel.
- Parameters:
backend (
Dict
[str
,Any
]) – dictionary of the form{"name": str, "options": dict}
, where the value for"name"
is the name of a PennyLane device and the value for"options"
is a dict to be passed as kwargs to PennyLane when creating the device. Example:{"name": "default.qubit", "options": {}}
n_classes (
int
) – number of classes.
- abstract get_init_weights(random, random_state)[source]
Generate weights to be used as initial trainable parameters.
- Return type:
ndarray
[Any
,dtype
[float64
]]
- vc.models.get_model(model, backend, n_classes)[source]
Create instance of a quantum model.
- Parameters:
model (
Dict
[str
,Any
]) – dictionary of the form{"name": str, "options": dict}
. The value for"name"
is used to determine the model class. The value for"options"
should be adict
and is passed as kwargs to the constructor of the model class. Note: if there’s a"backend"
key in the value for"options"
, it will be ignored.backend (
Dict
[str
,Any
]) – see docstring ofQModel
.n_classes (
int
) – number of classes.
- Return type:
- Returns:
An instance of a quantum model.