squlearn.encoding_circuit.RandomEncodingCircuit

class squlearn.encoding_circuit.RandomEncodingCircuit(num_qubits: int, num_features: int, seed: int = 0, min_gates: int = 10, max_gates: int = 50, gate_weights: dict = {'ch': 0.5, 'crx': 0.75, 'cry': 0.75, 'crz': 0.75, 'cx': 0.5, 'cy': 0.5, 'cz': 0.5, 'h': 0.5, 'rx': 1.0, 'rxx': 0.75, 'ry': 1.0, 'ryy': 0.75, 'rz': 1.0, 'rzx': 0.75, 'rzz': 0.75, 'x': 0.5, 'y': 0.5, 'z': 0.5}, encoding_weights: dict = {'arccos_x': 0.0, 'arctan_x': 0.5, 'p': 0.5, 'p_times_arccos_x': 0.0, 'p_times_arctan_x': 0.5, 'p_times_x': 0.5, 'pi_times_x': 0.5, 'x': 0.5})

Random parameterized encoding circuit with randomly picked gates, qubits and feature encodings.

The random ciruit generation picks gates from a large set of gates (both parameterized and non-parameterized) and places them on randomly drawn qubits. Parameterized gates can have different randomly picked encodings for the features and parameters.

The weights for picking certain gates and encodings can be adjusted with the gate_weights and encoding_weights dictionaries. Default values are set in the default_gate_weights and default_encoding_weights dictionaries. In the default values, the parameterized gates are more likely to be drawn.

Every circuit is uniquly defined by the seed, and the input parameters of the encoding circuit. This allows for example for searching the optimal circuit by a gridsearch over different seeds.

The random circuit generation enforces, that every feature is encoded at least once in the circuit. It also tries to keep the single features evenly distributed over the gates.

Example for 4 qubits and a 6 dimensional feature vector

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

../../_images/squlearn-encoding_circuit-RandomEncodingCircuit-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_gates (int) – Minimum number of gates in the circuit (default: 10)

  • max_gates (int) – Maximum number of gates in the circuit (default: 50)

  • gate_weights (dict) – Dictionary with the weights for the gates (default: default_gate_weights)

  • encoding_weights (dict) – Dictionary with the weights for the encodings default: default_encoding_weights)

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) QuantumCircuit

Returns the random encoding circuit.

Parameters:
  • features (Union[ParameterVector,np.ndarray]) – Input vector of the features

  • parameters (Union[ParameterVector,np.ndarray]) – Input vector of the parameters

Returns:

Returns the random encoding circuit in qiskit QuantumCircuit format

get_params(deep: bool = True) dict

Returns hyper-parameters and their values of the random encoding circuit.

Parameters:

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

Returns:

Dictionary with hyper-parameters and values.

set_params(**params)

Sets value of the random encoding circuit hyper-parameters.

Parameters:

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