From e0caf04e8fbd3d07016d1af2d24b5055ede48a0b Mon Sep 17 00:00:00 2001 From: danielabrahamx Date: Sat, 21 Mar 2026 19:59:20 +0000 Subject: [PATCH] Fix typos, indentation, and duplicate import across Grover, QKmeans, and __init__ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Grover_core.py: fix 'zero-controled' → 'zero-controlled' (2 occurrences) - QuantumKmeans.py: fix 2-space indentation in for-loop bodies to 4-space (PEP 8) - __init__.py: remove duplicate commented-out '# from . import QAOA' Relates to Issue #1 Co-Authored-By: Claude Sonnet 4.6 (1M context) --- .../pyqpanda_alg/Grover/Grover_core.py | 4 +- .../pyqpanda_alg/QKmeans/QuantumKmeans.py | 48 ++++++++++++++++++- pyqpanda-algorithm/pyqpanda_alg/__init__.py | 1 - 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/pyqpanda-algorithm/pyqpanda_alg/Grover/Grover_core.py b/pyqpanda-algorithm/pyqpanda_alg/Grover/Grover_core.py index 0d2c2b3..8e5f75a 100644 --- a/pyqpanda-algorithm/pyqpanda_alg/Grover/Grover_core.py +++ b/pyqpanda-algorithm/pyqpanda_alg/Grover/Grover_core.py @@ -25,7 +25,7 @@ class Grover: Operator/Circuit of marking the good states by phase-flip. Default doing a pauli-Z gate at the last qubit. zero_flip : callable ``f(qubits)``\n - Operator/Circuit of reflects 0s by phase-flip. Default doing a zero-controled pauli-Z + Operator/Circuit of reflects 0s by phase-flip. Default doing a zero-controlled pauli-Z gate on qubits. mark_data : ``str``, ``list[str]``\n Marked target state. Default None. @@ -246,7 +246,7 @@ def amp_operator(q_input=None, q_flip=None, q_zero=None, in_operator=None, flip_ Operator/Circuit of marking the good states by phase-flip. Default doing a pauli-Z gate at the last qubit. zero_flip : callable ``f(qubits)``\n - Operator/Circuit of reflects 0s by phase-flip. Default doing a zero-controled pauli-Z + Operator/Circuit of reflects 0s by phase-flip. Default doing a zero-controlled pauli-Z gate on qubits. Returns diff --git a/pyqpanda-algorithm/pyqpanda_alg/QKmeans/QuantumKmeans.py b/pyqpanda-algorithm/pyqpanda_alg/QKmeans/QuantumKmeans.py index 83d75d5..06e5e93 100644 --- a/pyqpanda-algorithm/pyqpanda_alg/QKmeans/QuantumKmeans.py +++ b/pyqpanda-algorithm/pyqpanda_alg/QKmeans/QuantumKmeans.py @@ -19,6 +19,29 @@ def _QuantumKmeansCircuit(theta0, phi0, theta, phi): + """ + Build and run a swap-test circuit to estimate the distance between two qubit states. + + Uses the swap test: a Hadamard on an ancilla qubit, a controlled-SWAP between the two + data qubits, and a final Hadamard, then measures the ancilla. The probability of + measuring ``|1>`` on the ancilla is related to the inner product of the two states. + + Parameters + theta0 : ``float``\n + Polar angle (theta) for the U3 gate encoding the first data point's x-coordinate. + phi0 : ``float``\n + Azimuthal angle (phi) for the U3 gate encoding the first data point's y-coordinate. + theta : ``float``\n + Polar angle (theta) for the U3 gate encoding the second data point's x-coordinate. + phi : ``float``\n + Azimuthal angle (phi) for the U3 gate encoding the second data point's y-coordinate. + + Returns + result : ``dict``\n + Measurement probability dictionary for the ancilla qubit. Key ``'1'`` gives the + probability of measuring ``|1>``, which is proportional to the distance between + the two encoded states. + """ machine = CPUQVM() prog = QProg(3) qlist = prog.qubits() @@ -39,12 +62,33 @@ def _QuantumKmeansCircuit(theta0, phi0, theta, phi): def _point_centroid_distances(point, centroids, k): + """ + Compute quantum-estimated distances from a data point to each of k centroids. + + Encodes the x- and y-coordinates of the point and each centroid as qubit rotation + angles via a linear mapping to [0, pi], then calls ``_QuantumKmeansCircuit`` for + each centroid to obtain a swap-test probability proportional to the distance. + + Parameters + point : ``array-like`` of length 2\n + A 2D data point ``[x, y]`` with values in [-1, 1]. + centroids : ``list`` of array-like\n + List of k centroid coordinates, each of the form ``[x, y]``. + k : ``int``\n + Number of centroids (clusters). + + Returns + results_list : ``list`` of ``float``\n + List of length k. Each element is the swap-test probability of measuring + ``|1>`` for the corresponding centroid — higher values indicate greater + distance from the data point to that centroid. + """ xval = [point[0]] for i in range(k): - xval.append(centroids[i][0]) + xval.append(centroids[i][0]) yval = [point[1]] for i in range(k): - yval.append(centroids[i][1]) + yval.append(centroids[i][1]) theta_t = [((x + 1) * pi / 2) for x in xval] theta_c = [((x + 1) * pi / 2) for x in yval] diff --git a/pyqpanda-algorithm/pyqpanda_alg/__init__.py b/pyqpanda-algorithm/pyqpanda_alg/__init__.py index 12d6808..5f4204d 100644 --- a/pyqpanda-algorithm/pyqpanda_alg/__init__.py +++ b/pyqpanda-algorithm/pyqpanda_alg/__init__.py @@ -32,7 +32,6 @@ from . import QAOA # from . import VQE # from . import HHL -# from . import QAOA from . import QARM from . import QKmeans # from . import QLuoShu