Skip to content

【代码贡献】QSVR: build the show_res test grid from each column's own min/max - #46

Open
mnn31 wants to merge 1 commit into
OriginQ:developfrom
mnn31:fix/qsvr-plot-ranges
Open

【代码贡献】QSVR: build the show_res test grid from each column's own min/max#46
mnn31 wants to merge 1 commit into
OriginQ:developfrom
mnn31:fix/qsvr-plot-ranges

Conversation

@mnn31

@mnn31 mnn31 commented Aug 10, 2026

Copy link
Copy Markdown

Problem

Quantum_SVR.show_res() draws the regression surface over the wrong region.
Both axes of the test grid get the same range, and that range is not the range
of either input column: it starts at column 0's minimum and ends at column 1's
maximum. The surface is therefore evaluated far outside the training data on one
axis, and the other axis covers an interval the data never occupies, so the plot
does not line up with the scatter points it is drawn against.

Root cause

pyqpanda_alg/QSVR/QSVR.py, show_res (lines 137-138):

x0_test = np.linspace(min(self.x[:, 0]), max(self.x[:, 1]), 30)
x1_test = np.linspace(min(self.x[:, 0]), max(self.x[:, 1]), 30)

The column index is crossed. Both lines take the minimum from column 0 and the
maximum from column 1, and the two lines are identical, so x1_test never sees
column 1's minimum and x0_test never sees column 0's maximum.

It is easy to miss because __init__ runs StandardScaler and then PCA, which
usually leaves both columns centred near zero and roughly the same size. The
error is still there in that case: with a strongly correlated 2-feature input
(case B below), PC1 spans [-4.12, 4.30] and PC2 [-0.12, 0.12], and both
plotted axes come out as [-4.12, 0.12]. It becomes obvious as soon as the
columns differ in scale, for instance when self.x is assigned directly, or
when a 1-column input is zero-padded and column 1 is constant 0.

Fix

Take each axis from its own column:

x0_test = np.linspace(min(self.x[:, 0]), max(self.x[:, 0]), 30)
x1_test = np.linspace(min(self.x[:, 1]), max(self.x[:, 1]), 30)

Two lines. The grid size, the meshgrid, the kernel, the fit and the plotting
calls are untouched, and get_res() is not affected.

Verification

show_res is instrumented with a stub SVR so the grid it builds can be read
back without waiting on the quantum kernel, on three inputs whose columns have
different ranges. Case A sets self.x directly with column 0 in [0, 2] and
column 1 in [10, 30]. Case B goes through __init__, so the columns are the
PCA components of a correlated input: with rng = numpy.random.default_rng(0),
t = rng.normal(size=200) and
x = column_stack([t + 0.05*rng.normal(size=200), 20*t + rng.normal(size=200)]).
Case C is a single-column input that the class zero-pads.

   data col0           data col1        before axis0        before axis1        after axis0         after axis1
A  [0, 2]              [10, 30]         [0.000, 30.000]     [0.000, 30.000]     [0.000, 2.000]      [10.000, 30.000]
B  [-4.119, 4.304]     [-0.119, 0.122]  [-4.119, 0.122]     [-4.119, 0.122]     [-4.119, 4.304]     [-0.119, 0.122]
C  [-1.936, 1.455]     [0, 0]           [-1.936, 0.000]     [-1.936, 0.000]     [-1.936, 1.455]     [0.000, 0.000]

The docstring example runs end to end with the real quantum kernel and the same
random data as before, and still completes in about 5 s.

Tests

2 cases added to test/QAlgBase/Test_class_qsvr_Quantum_SVR.py. They swap in a
recording stub for SVR and for the pyplot figure, call show_res(), and read
the grid straight out of the points handed to predict: one asserts both axes
equal linspace over their own column's min and max for constructor-produced
data, the other pins the crisp case of column 0 in [0, 2] and column 1 in
[10, 30]. No figure is created and no quantum circuit is run, so both are fast
and deterministic. Both fail on develop and pass with this change.

cd test && python -m pytest -o addopts="" QAlgBase/Test_class_qsvr_Quantum_SVR.py -q
3 passed
cd test && python -m pytest -o addopts="" -q
19 passed          # 17 before this change

(-o addopts="" because test/pytest.ini hardcodes allure options that need
the allure-pytest plugin.)

example/QAlgBase/testeg_class_qsvr.py imports pyqpanda_alg.QFinance.class_qsvr,
which does not exist in this tree, and also imports pyqpanda rather than
pyqpanda3. That is a separate stale-example problem, unrelated to the grid
ranges, so I left it alone here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant