pyqpanda_alg.QmRMR.QmRMR_core

Classes

SPSAOptimizer

Simultaneous Perturbation Stochastic Approximation (SPSA) Optimizer.

Feature_Selection

Quantum-based Feature Selection using parameterized quantum circuits.

Functions

plot_bar(dic)

Plot a bar chart from a dictionary.

plot_loss(los)

Plot a line chart showing the loss over iterations.

control(qubits1, qubits2, theta)

Construct a multi-controlled RY rotation gate.

Module Contents

pyqpanda_alg.QmRMR.QmRMR_core.plot_bar(dic)

Plot a bar chart from a dictionary.

Highlights the bar with the highest value in red, while the rest are shown in blue.

Parameters

dicdict

A dictionary where keys are labels and values are numerical quantities to be plotted.

pyqpanda_alg.QmRMR.QmRMR_core.plot_loss(los)

Plot a line chart showing the loss over iterations.

Useful for visualizing the optimization process.

Parameters

loslist of float

A list of loss values, where each entry corresponds to one optimization step.

pyqpanda_alg.QmRMR.QmRMR_core.control(qubits1, qubits2, theta)

Construct a multi-controlled RY rotation gate.

Creates an RY rotation gate on the target qubit with multiple control qubits.

Parameters

qubits1list or Qubit

Control qubit(s). Can be a single Qubit or a list of Qubits.

qubits2Qubit

Target qubit on which the RY rotation will be applied.

thetafloat

Rotation angle for the RY gate.

Returns

QGate

A controlled RY gate that can be appended to a quantum circuit.

class pyqpanda_alg.QmRMR.QmRMR_core.SPSAOptimizer(objective_function, a=0.1, c=0.1, alpha=0.602, beta=0.101, max_iters=1000)

Simultaneous Perturbation Stochastic Approximation (SPSA) Optimizer.

This class implements the SPSA algorithm for optimizing a scalar-valued objective function that depends on multiple parameters. SPSA is useful in cases where the objective function is noisy, non-differentiable, or expensive to evaluate, such as in variational quantum algorithms.

Parameters

objective_functionCallable[[np.ndarray], float]

The objective function to be minimized. It must take a parameter vector as input and return a scalar loss value.

afloat, optional

Scaling factor for the learning rate (step size), default is 0.1.

cfloat, optional

Scaling factor for the perturbation, default is 0.1.

alphafloat, optional

Exponent controlling the decay rate of the learning rate, default is 0.602.

betafloat, optional

Exponent controlling the decay rate of the perturbation size, default is 0.101.

max_itersint, optional

Maximum number of iterations to perform, default is 1000.

objective_function
a = 0.1
c = 0.1
alpha = 0.602
beta = 0.101
max_iters = 1000
optimize(initial_params)

Run the SPSA optimization to minimize the objective function.

Parameters

initial_paramsnp.ndarray

Initial values for the parameters to be optimized.

Returns

np.ndarray

The optimized parameter vector.

list[float]

History of objective function values over iterations.

class pyqpanda_alg.QmRMR.QmRMR_core.Feature_Selection(quadratic, linear, select_num)

Quantum-based Feature Selection using parameterized quantum circuits.

This class builds a variational quantum circuit to model a feature selection task. The objective is to optimize the selection of a subset of features based on a given linear and quadratic objective function.

Parameters

quadraticnp.ndarray

Quadratic coefficient matrix used in the objective function.

linearlist[float]

Linear coefficient list used in the objective function.

select_numint

Number of features to be selected (i.e., number of bits set to 1).

Attributes

qb_numint

Number of qubits required, equal to the number of features.

l1list[float]

History of linear loss values during optimization.

l2list[float]

History of quadratic loss values during optimization.

>>> from pyqpanda_alg import QmRMR
>>> import numpy as np
>>> from pyqpanda3.core import *
>>> import matplotlib.pyplot as plt
>>> m = 6
>>> u = np.random.random(m)
>>> cor = np.random.random([m, m])
>>> cor = (cor.T + cor)/2
>>> ini_par = np.random.random(int(m/2)*m)*np.pi
>>> loss, choice, dic = QmRMR.Feature_Selection(cor, u, 3).get_his_res(ini_par)
>>> print(choice)
>>> plt.plot(loss)
>>> plt.show()
[0, 1, 0, 0, 1, 1]
quadratic
linear
qb_num
select_num
l1 = []
l2 = []
Circuit(qbs, para)
select_key(dit)
get_theory(para)
cal_loss(para)
get_his_res(ini_para)