ingenii_quantum.hybrid_networks.edge_detection

class ingenii_quantum.hybrid_networks.edge_detection.EdgeDetector2D(size, backend='aer_simulator', shots=1000)[source]

Bases: EdgeDetectorBase

Quantum Hadamard Edge Detection algorithm for 2D images.

This class implements both Qiskit-based and PyTorch-based quantum edge detection algorithms for two-dimensional images.

size

Size of the (NxN) blocks used for partitioning the image.

Type:

int

backend

Computational backend (‘torch’ for PyTorch, ‘aer_simulator’ for Qiskit).

Type:

str

shots

Number of shots for Qiskit experiments.

Type:

int

run(data, tol=0.001, reduce=True, verbose=False)[source]

Runs the edge detection algorithm with either Pytorch or Qiskit code

Parameters:
  • data (tensor/np.array) – input data

  • tol (tensor/np.array) – Tolerance to be considered and edge

  • reduce (bool) – reduce the dimension by half at the end

  • verbose (bool) – If true, tqdm is used to show the evolution.

Returns:

Edge-detected image

Return type:

np.array/tensor

class ingenii_quantum.hybrid_networks.edge_detection.EdgeDetector3D(size, backend='aer_simulator', shots=100)[source]

Bases: EdgeDetectorBase

Quantum Hadamard Edge Detection algorithm for 3D volumes.

This class implements both Qiskit-based and PyTorch-based quantum edge detection algorithms for three-dimensional images.

size

Size of the (NxN) blocks used for partitioning the image.

Type:

int

backend

Computational backend (‘torch’ for PyTorch, ‘aer_simulator’ for Qiskit).

Type:

str

shots

Number of shots for Qiskit experiments.

Type:

int

run(data, num_filters=6, tol=0.001, reduce=True, verbose=False)[source]

Run edge detection for different rotations of the image.

Parameters:
  • data (np.array) – input data (samples, features, dim,dim,dim)

  • num_filters (int) – Number of rotations (permutations of the last 3

  • output (dimension) to)

  • tol (float) – Tolerance to consider an edge

  • reduce (bool) – reduce the size of the image at the end

  • verbose (bool) – If true, tqdm is used to show the evolution.

Returns:

Edge-detected image

Return type:

np.array/tensor

class ingenii_quantum.hybrid_networks.edge_detection.EdgeDetectorBase(n_dimensions: int, size: int, backend: str, shots: int)[source]

Bases: object

Base class for quantum-inspired edge detection.

This class implements both Pytorch-based and Qiskit-based quantum Hadamard edge detection algorithms for images and 3D volumes.

n_dimensions

Number of dimensions (2D or 3D).

Type:

int

size

Size of the (NxN) blocks used for partitioning the image.

Type:

int

backend

Computational backend (‘torch’ for PyTorch, ‘aer_simulator’ for Qiskit).

Type:

str

shots

Number of shots for Qiskit experiments.

Type:

int

total_qb

Total number of qubits used in the quantum circuit.

Type:

int

anc_qb

Number of ancilla qubits.

Type:

int

data_qb

Number of qubits required for data encoding.

Type:

int

device

PyTorch device (‘cpu’ or ‘cuda’), only for torch backend.

Type:

torch.device, optional

run_box(box, tol=0.001)[source]

Runs the edge detection algorithm for a single box of the image.

This function executes a quantum Hadamard-based edge detection algorithm for a given image segment.

Parameters:
  • box (np.array) – Input image segment.

  • tol (float, optional) – Threshold for detecting edges. Defaults to 1e-3.

Returns:

Edge-detected version of the input box.

Return type:

np.array

run_image_qiskit(data, tol=0.001, reduce=True, verbose=False)[source]

Runs the edge detection algorithm for the entire image using Qiskit.

Parameters:
  • data (np.array) – Input image.

  • tol (float, optional) – Threshold for detecting edges. Defaults to 1e-3.

  • reduce (bool, optional) – If True, reduces the image dimensions by half. Defaults to True.

  • verbose (bool, optional) – If True, displays progress using tqdm. Defaults to False.

Returns:

Edge-detected image.

Return type:

np.array

run_image_torch(data, tol=0.001, reduce=True, verbose=False)[source]

Runs edge detection for the entire image using PyTorch.

Parameters:
  • data (torch.Tensor) – Input image.

  • tol (float, optional) – Threshold for detecting edges. Defaults to 1e-3.

  • reduce (bool, optional) – If True, reduces the image dimensions by half. Defaults to True.

  • verbose (bool, optional) – If True, displays progress using tqdm. Defaults to False.

Returns:

Edge-detected image.

Return type:

torch.Tensor

ingenii_quantum.hybrid_networks.filters

class ingenii_quantum.hybrid_networks.filters.QuantumFilters2D(shape: tuple = (4, 4), stride: float = 1, encoding: str = 'frqi', shots: int = 4096, backend='torch')[source]

Bases: QuantumFiltersBase

Quantum filter for 2D images.

This class applies a quantum transformation to 2D images using random quantum circuits or Ising model evolution.

shape

Shape of the filter (NxN).

Type:

tuple

stride

Stride used when applying the quantum filter.

Type:

int

encoding

Quantum data encoding method (‘frqi’ or ‘angle’).

Type:

str

shots

Number of shots for Qiskit experiments.

Type:

int

backend

Execution backend (‘torch’ or ‘aer_simulator’).

Type:

str

class ingenii_quantum.hybrid_networks.filters.QuantumFilters3D(shape: tuple = (4, 4, 4), stride: float = 1, encoding: str = 'frqi', shots: int = 4096, backend='torch')[source]

Bases: QuantumFiltersBase

Quantum filter for 3D images.

This class applies a quantum transformation to 3D images using random quantum circuits or Ising model evolution.

shape

Shape of the filter (NxNxN).

Type:

tuple

stride

Stride used when applying the quantum filter.

Type:

int

encoding

Quantum data encoding method (‘frqi’ or ‘angle’).

Type:

str

shots

Number of shots for Qiskit experiments.

Type:

int

backend

Execution backend (‘torch’ or ‘aer_simulator’).

Type:

str

class ingenii_quantum.hybrid_networks.filters.QuantumFiltersBase(n_dimensions: int, shape: tuple, stride: float, encoding: str, shots: int, backend: str)[source]

Bases: object

Base class for quantum-inspired filters applied to 2D and 3D images.

This class implements different quantum encoding schemes (FRQI and angle) and quantum transformations using random quantum circuits or Ising models.

n_dimensions

Number of spatial dimensions (2D or 3D).

Type:

int

shape

Shape of the filter (e.g., (4,4) for 2D, (4,4,4) for 3D).

Type:

tuple

stride

Stride used when applying the quantum filter.

Type:

float

encoding

Encoding method (‘frqi’ or ‘angle’).

Type:

str

shots

Number of shots for Qiskit experiments.

Type:

int

backend

Execution backend (‘torch’ or ‘aer_simulator’).

Type:

str

nqbits

Number of qubits used in the quantum circuit.

Type:

int

device

PyTorch device (‘cpu’ or ‘cuda’), only if using torch backend.

Type:

torch.device, optional

unitaries_list

List of unitary transformations used in quantum filtering.

Type:

list

num_filters

Number of quantum filters applied per feature.

Type:

int

num_features

Number of features in the input data.

Type:

int

gates_name

Gate set used for quantum transformation (‘G1’, ‘G2’, ‘G3’, or ‘Ising’).

Type:

str

info

Metadata containing circuit details such as depth and nonlocal gate count.

Type:

dict

gate_names = {'G1': ['CNOT', 'H', 'X'], 'G2': ['CNOT', 'H', 'S'], 'G3': ['CNOT', 'H', 'T']}
generate_qc(gates_name='G3', num_gates=300, num_filters=3, num_features=19, save=False, saved_gates_filename='gates.pickle', saved_qubits_filename='qubits.pickle')[source]

Generate sets of random quantum gates and their associated qubits and saves them. This function is only used with Qiskit backends.

Parameters:
  • gates_name (str) – name of the family of quantum gates. Either G1,

  • G2

  • Ising (G3 or)

  • num_gates (int) – depth of the quantum circuits

  • num_filters (int) – Number of filters to apply to each feature

  • num_features (int) – Number of features of the data

  • save (bool) – Save the gates and qubits to pickle files

  • saved_gates_filename (str) – File name for saved gates set

  • saved_qubits_filename (str) – File name for saved qubit set

generate_reservoirs(gates_name='G3', num_gates=300, num_filters=3, num_features=19, save=False, filename='reservoir.pickle')[source]

Generate sets of random quantum gates and their associated qubits and saves them for Qiskit backends, and unitary tensors for torch backend.

Parameters:
  • gates_name (str) – name of the family of quantum gates. Either G1,

  • G2

  • Ising (G3 or)

  • num_gates (int) – depth of the quantum circuits

  • num_filters (int) – Number of filters to apply to each feature

  • num_features (int) – Number of features of the data

  • save (bool) – Save the gates and qubits to pickle files

  • filename (str) – File name for saved gates or unitaries

generate_unitaries(gates_name='G3', num_gates=300, num_filters=3, num_features=19, unitaries_file_name='unitaries.pickle', save=False)[source]

Generates the quantum unitaries that represent the quantum reservoirs (random quantum circuits) This function is only used with Pytorch backend

Parameters:
  • gates_name (str) – name of the family of quantum gates. Either G1,

  • G2

  • Ising (G3 or)

  • num_gates (int) – depth of the quantum circuits

  • num_filters (int) – Number of filters to apply to each feature

  • num_features (int) – Number of features of the data

  • unitaries_file_name (str) – name of the file containing unitary list (only needed if save=True)

  • save (bool) – Whether the generated unitaries are saved to a file

get_quantum_filters(data, tol=1e-06)[source]

Runs the quantum filters for all features multiple (num_filters) times

Parameters:
  • data (tensor) – input data (one feature), shape 2D (num_samples, num_features, N, N), 3D (num_samples, num_features, N, N, N)

  • tol (float) – tolerance for the masking matrix. All values from the original data which are smaller than the tolerance are set to 0

Returns:

output quantum filters

Return type:

(tensor/np.array)

load_gates(gates_name='G3', saved_gates_filename=None, saved_qubits_filename=None)[source]

Load set of quantum gates and qubits. This function is only used with Qiskit backends.

Parameters:
  • gates_name (str) – name of the family of quantum gates. Either G1,

  • G2

  • Ising (G3 or)

  • saved_gates_filename (str) – File name for saved gates set

  • saved_qubits_filename (str) – File name for saved qubit set

load_reservoirs(gates_name='G3', filename=None)[source]

Load set of quantum gates and qubits for Qiskit backends, or unitaries for pytorch backends.

Parameters:
  • gates_name (str) – name of the family of quantum gates. Either G1,

  • G2

  • Ising (G3 or)

  • filename (str) – File name for saved gates set or unitaries

load_unitaries(file_name)[source]

Loads the unitaries. This function is only used with Pytorch backend.

Parameters:

file_name (str) – File name for unitaries

ingenii_quantum.hybrid_networks.layers

class ingenii_quantum.hybrid_networks.layers.QuantumFCLayer(input_size, n_layers=2, encoding='qubit', ansatz=1, observables=None, backend='default.qubit')[source]

Bases: object

Quantum fully-connected layer for hybrid quantum-classical neural networks.

This class defines a quantum layer that applies various quantum encodings and ansatz circuits to process input data using a quantum neural network.

input_size

Dimension of the input data.

Type:

int

n_layers

Number of layers in the quantum ansatz circuit.

Type:

int

encoding

Name of the data encoding method. Supported options: ‘qubit’, ‘amplitude’, ‘ZZFeatureMap’, ‘QAOA’.

Type:

str

ansatz

Identifier for the ansatz quantum circuit (1-6).

Type:

int

observables

Observables measured at the end of the circuit. If None, defaults to ‘Z’ * nqbits.

Type:

str or list of str

backend

Quantum backend used for execution. Defaults to ‘default.qubit’ (Pennylane simulator).

Type:

str

nqbits

Number of qubits required for encoding.

Type:

int

dev

Quantum device used for simulation.

Type:

qml.Device

apply_ansatz(weights_layer, qubits=None)[source]

Applies the quantum ansatz to the quantum neural network.

Parameters:
  • weight_layers (array) – Weights of the ansatz layer

  • qubits – qubits to apply the ansatz to

Returns:

quantum neural network

Return type:

(QuantumNN)

create_layer(type_layer='keras')[source]

Creates a Quantum fully connected layer and initializes it.

Parameters:

type_layer (str) – Type of quantum layer. It can either be ‘keras’ or ‘torch’

Returns:

Quantum layer

get_shapes()[source]
qnn_layer(inputs, weights_layers)[source]

Creates the quantum neural network composed of the quantum encoding with qubit, amplitude or ZZ encoding, and a quantum ansatz.

Parameters:
  • inputs (array) – Input data

  • weight_layers (array) – Weights of the ansatz layer

Returns:

quantum neural network

Return type:

(QuantumNN)

qnn_layer_QAOA(inputs, weights_layers, weights_input)[source]

Creates the quantum neural network composed of the quantum encoding with QAOA encoding, and a quantum ansatz.

Args

inputs (array): Input data weight_layers (array): Weights of the ansatz layer weight_input (array): Weights of the QAOA quantum encoding

Returns:

quantum neural network

Return type:

(QuantumNN)

ingenii_quantum.hybrid_networks.qinsp_filter

class ingenii_quantum.hybrid_networks.qinsp_filter.QuantumInspiredImageProcessor(mu=0.4, neighbor_size=3, percentile=95, max_iter=10, threshold=0.0001, L=8)[source]

Bases: object

Applies a quantum-inspired image filter.

This transformation weighs each pixel’s original intensity by a relative measure of pairwise intensity difference versus total neighborhood contribution.

Class Parameters:
  • mu (float): Steepness factor. Higher values lead to sharper curves, whereas lower values provide smoother transitions.

  • neighbor_size (int): Neighborhood size for intensity computations.

  • percentile (int): Percentile to determine cluster values w.

  • max_iter (int): Maximum iterations for transformation.

  • threshold (float): Convergence threshold based on Mean Absolute Difference.

  • L (int): Number of cluster levels.

Example Parameters:
  • mu = 0.4

  • neighbor_size = 3

  • percentile = 50

  • max_iter = 10

  • threshold = 1e-5

  • L = 12

check_convergence(w1, w2)[source]

Check convergence of the quantum-inspired model.

If the Mean Absolute Difference (MAD) of two consecutive matrices is smaller than the threshold, the method has converged.

Parameters:
  • w1 (np.array) – Previous image matrix.

  • w2 (np.array) – Current image matrix.

Returns:

True if the model has converged, False otherwise. float: Mean Absolute Difference.

Return type:

bool

process(im, save=True, image_path='./filtered_image.png')[source]

Applies the quantum-inspired transformation until convergence.

Parameters:
  • im (np.array) – Input image.

  • save (bool, optional) – If True, saves the final image. Defaults to True.

  • image_path (str, optional) – Path to save the final image. Defaults to ‘./filtered_image.png’.

Returns:

Final processed image.

list: List of Mean Absolute Differences between iterations.

list: List of image transformations.

Return type:

np.array

select_image(mae_list, image_list)[source]

Selects the output of the image transformation after multiple iterations.

The selection is based on detecting peaks in the Mean Absolute Differences (MAD).

Parameters:
  • mae_list (list) – List of Mean Absolute Differences between iterations.

  • image_list (list) – List of image transformations.

Returns:

Final selected image.

int: Iteration index at which the best image was selected.

Return type:

np.array

sum_neighbours(image)[source]

Computes the sum of the neighborhood of each pixel.

Parameters:

image (np.array) – Input image.

Returns:

Sum of neighboring pixels for each pixel.

Return type:

np.array

transformation(I, cluster)[source]

Applies a quantum-inspired transformation.

Parameters:
  • I (np.array) – Input image.

  • cluster (np.array) – Array containing the cluster values [w0, w1, …, wn].

Returns:

Quantum-inspired image transformation (one iteration).

np.array: Alpha values calculated as 1 - (I_{i+p,j+q} - I_{ij}).

Return type:

np.array

ingenii_quantum.hybrid_networks.statistics

class ingenii_quantum.hybrid_networks.statistics.EntanglingCapacity(circuit, dev, params_shape)[source]

Bases: object

generate_random_params()[source]

Generates random parameters for the quantum circuit based on the specified parameter shape.

meyer_wallach_entanglement(n_samples)[source]

Computes the Meyer-Wallach entanglement measure for the quantum circuit.

Parameters:

n_samples (int) – Number of samples to calculate the entanglement measure

Returns:

Meyer-Wallach entanglement measure averaged over multiple samples.

Return type:

float

class ingenii_quantum.hybrid_networks.statistics.Expressibility(pqc, params_shape, dev)[source]

Bases: object

compute_expressibility(n_samples)[source]

Compute the expressibility of the PQC by comparing its fidelity distribution to that of Haar-random states using KL divergence.

Parameters:

n_samples (int) – Number of samples for the computation.

Returns:

Expressibility value, computed as the mean KL divergence.

Return type:

float

generate_random_params(params_shape)[source]

Generate random parameters for the parameterized quantum circuit.

Parameters:

params_shape (tuple) – Shape of the parameters.

Returns:

Randomly generated parameters in the range [0, 2π].

Return type:

np.ndarray

haar_fidelity(n_samples)[source]

Generate fidelities for Haar-random states.

Parameters:

n_samples (int) – Number of samples for fidelity computation.

Returns:

Array of Haar-random fidelities.

Return type:

np.ndarray

pqc_fidelity(n_samples)[source]

Calculate the fidelity between quantum states produced by the PQC.

Parameters:

n_samples (int) – Number of samples for fidelity computation.

Returns:

List of fidelity values for the sampled states.

Return type:

list

ingenii_quantum.hybrid_networks.utils

ingenii_quantum.hybrid_networks.utils.roll_numpy(input_array, window_array, dx=1, dy=1, dz=None)[source]

Rolling 3D window for numpy array. This function is only used with Qiskit backends.

Parameters:
  • input_array (np.array) – input array

  • window_array (np.array) – rolling 2D window array

  • dx (int) – horizontal step, abscissa, number of columns

  • dy (int) – vertical step, ordinate, number of rows

  • dz (int) – transverse step, applicate, number of layers. Only used with

  • window (3D)

Returns:

Stride of the rolling window tuple: Shape of the rolling window

Return type:

np.array

ingenii_quantum.hybrid_networks.utils.roll_torch(input_array, window_array, dx=1, dy=1, dz=None)[source]

Rolling 3D window for pytorch tensor. This function is only used with Pytorch backends.

Parameters:
  • input_array (tensor) – input array, shape (n_samples, N,N,N)

  • window_array (tensor) – rolling 3D window array, shape (n,n,n)

  • dx (int) – horizontal step, abscissa, number of columns

  • dy (int) – vertical step, ordinate, number of rows

  • dz (int) – transverse step, applicate, number of layers

Returns:

Stride of the rolling window tuple: Shape of the rolling window

Return type:

tensor