squlearn.encoding_circuit.RandomLayeredEncodingCircuit

class squlearn.encoding_circuit.RandomLayeredEncodingCircuit(num_qubits: int, num_features: int, seed: int = 0, min_num_layers: int = 2, max_num_layers: int = 10, feature_probability=0.3)

Creates a random Layered encoding circuit with no trainable parameters.

A random Layered encoding circuit is generated by randomly selecting gates from a predefined action space. The action space contains single qubit gates, two qubit gates. Non-parametrized gates that are placed in the random circuit are: H, X, Y, Z, cx, cy, cz. Included paramererized gates are Rx, Ry, Rz, crx, cry, crz, where the parameter is either a fixed angle or a feature. Fixed angles are \(\pi\), \(\pi/2\), \(\pi/3\), \(\pi/4\), \(\pi/8\). Feature \(x\) are encoded as \(x\), \(x\pi\), \(\arctan(x)\). The random circuit generation enforces, that all features are encoded at least once.

A seed is used to identify the random circuit, so that the same circuit can be reproduced. The number of layers is randomly chosen between min_num_layers and max_num_layers. The probability of a layer containing an encoding gate is given by feature_probability.

Example for 4 qubits and a 6 dimensional feature vector

(Source code, png, hires.png, pdf)

../../_images/squlearn-encoding_circuit-RandomLayeredEncodingCircuit-1.png
Parameters:
  • num_qubits (int) – Number of qubits of the encoding circuit

  • num_features (int) – Dimension of the feature vector

  • seed (int) – Seed for the random number generator (default: 0)

  • min_num_layers (int) – Minimum number of layers (default: 2)

  • max_num_layers (int) – Maximum number of layers (default: 10)

  • feature_probability (float) – Probability of a layer containing an encoding gate (default: 0.3)

compose(x, concatenate_features=True, concatenate_parameters=False)

Composition of encoding circuits with options for handling features and parameters

Number of qubits and features have to be equal in both encoding circuits! The special function and properties of the both encoding circuits are lost by this composition.

Parameters:
Returns:

Returns the composed encoding circuit as special class ComposedEncodingCircuit

draw(output: str = None, feature_label: str = 'x', parameter_label: str = 'p', decompose: bool = False, **kwargs) None

Draws the encoding circuit circuit using the QuantumCircuit.draw() function.

Parameters:
  • feature_label (str) – Label for the feature vector (default:”x”).

  • parameter_label (str) – Label for the parameter vector (default:”p”).

  • decompose (bool) – If True, the circuit is decomposed before printing (default: False).

  • kwargs – Additional arguments from Qiskit’s QuantumCircuit.draw() function.

Returns:

Returns the circuit in qiskit QuantumCircuit.draw() format

generate_initial_parameters(seed: int | None = None) ndarray

Generates random parameters for the encoding circuit

Parameters:

seed (Union[int,None]) – Seed for the random number generator (default: None)

Returns:

The randomly generated parameters

get_circuit(features: ParameterVector | ndarray, parameters: ParameterVector | ndarray = None) QuantumCircuit

Returns the quantum circuit of the Random Layered encoding circuit.

Parameters:
  • features (Union[ParameterVector, np.ndarray]) – The input features.

  • parameters (Union[ParameterVector, np.ndarray]) – The trainable parameters of the circuit (not used, since there are no free parameters).

Returns:

The quantum circuit of the Random Layered encoding circuit.

Return type:

QuantumCircuit

get_params(deep: bool = True) dict

Returns hyper-parameters and their values of the Random Layered encoding circuit

Parameters:

deep (bool) – If True, also the parameters for contained objects are returned (default=True).

Returns:

Dictionary with hyper-parameters and values.

inverse()

Returns the inverse of the encoding circuit.

Returns:

The inverse of the encoding circuit

set_params(**params) EncodingCircuitBase

Sets value of the random layered encoding circuit hyper-parameters.

The random circuit is regenerated with the new hyper-parameters.

Parameters:

params – Hyper-parameters and their values, e.g. num_qubits=2.