From 2c9b29cf70b82de92740a43cc648721e9fed1a0b Mon Sep 17 00:00:00 2001 From: zengqianfan-hub Date: Tue, 3 Nov 2026 16:38:17 -0500 Subject: [PATCH 1/2] Fix inverse quantum multiplication decomposition order --- projectq/libs/math/_quantummath.py | 32 +++++++------------------ projectq/libs/math/_quantummath_test.py | 16 +++++++++++++ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/projectq/libs/math/_quantummath.py b/projectq/libs/math/_quantummath.py index 59aa7cf9..b80b7851 100644 --- a/projectq/libs/math/_quantummath.py +++ b/projectq/libs/math/_quantummath.py @@ -512,23 +512,8 @@ def quantum_multiplication(eng, quint_a, quint_b, product): ) -def inverse_quantum_multiplication(eng, quint_a, quint_b, product): - """ - Inverse of the multiplication of two quantum integers. - - i.e, - - |a>|b>|a*b> -> |a>|b>|0> - - (only works if quint_a and quint_b are of the same size, n qubits and product has size 2n+1) - - Args: - eng (MainEngine): ProjectQ MainEngine - quint_a (list): Quantum register (or list of qubits) - quint_b (list): Quantum register (or list of qubits) - product (list): Quantum register (or list of qubits) storing the result - """ +def inverse_quantum_multiplication(eng, quint_a, quint_b, product): n_a = len(quint_a) if len(quint_a) != len(quint_b): @@ -536,20 +521,21 @@ def inverse_quantum_multiplication(eng, quint_a, quint_b, product): if len(product) != ((2 * n_a) + 1): raise ValueError('product size must be 2*n + 1') - for j in range(2, n_a): + for j in reversed(range(2, n_a)): with Control(eng, quint_b[j]): SubtractQuantum | ( - quint_a[0 : (n_a - 1)], # noqa: E203 - product[(0 + j) : (n_a - 1 + j)], # noqa: E203 + quint_a[0 : (n_a - 1)], + product[j : (n_a - 1 + j)], [product[n_a + j], product[n_a + j + 1]], ) - for i in range(0, n_a): - with Control(eng, [quint_a[i], quint_b[0]]): - X | product[i] with Control(eng, quint_b[1]): SubtractQuantum | ( - quint_a[0 : (n_a - 1)], # noqa: E203 + quint_a[0 : (n_a - 1)], product[1:n_a], [product[n_a + 1], product[n_a + 2]], ) + + for i in reversed(range(0, n_a)): + with Control(eng, [quint_a[i], quint_b[0]]): + X | product[i] \ No newline at end of file diff --git a/projectq/libs/math/_quantummath_test.py b/projectq/libs/math/_quantummath_test.py index d32715ca..416b7814 100644 --- a/projectq/libs/math/_quantummath_test.py +++ b/projectq/libs/math/_quantummath_test.py @@ -391,3 +391,19 @@ def test_quantummultiplication(eng): assert 1.0 == pytest.approx(eng.backend.get_probability([0, 1, 0], qureg_a)) assert 1.0 == pytest.approx(eng.backend.get_probability([1, 1, 0], qureg_b)) assert 1.0 == pytest.approx(eng.backend.get_probability([0, 0, 0, 0, 0, 0, 0], qureg_c)) + + +def test_quantummultiplication_inverse(eng): + qureg_a = eng.allocate_qureg(3) + qureg_b = eng.allocate_qureg(3) + qureg_c = eng.allocate_qureg(7) + + init(eng, qureg_a, 7) + init(eng, qureg_b, 3) + + MultiplyQuantum | (qureg_a, qureg_b, qureg_c) + MultiplyQuantum.get_inverse() | (qureg_a, qureg_b, qureg_c) + + assert 1.0 == pytest.approx(eng.backend.get_probability([1, 1, 1], qureg_a)) + assert 1.0 == pytest.approx(eng.backend.get_probability([1, 1, 0], qureg_b)) + assert 1.0 == pytest.approx(eng.backend.get_probability([0, 0, 0, 0, 0, 0, 0], qureg_c)) From f355135dbf06185004748702a277502ed2e8c489 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:43:48 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- projectq/libs/math/_quantummath.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/projectq/libs/math/_quantummath.py b/projectq/libs/math/_quantummath.py index b80b7851..f8fc5bd1 100644 --- a/projectq/libs/math/_quantummath.py +++ b/projectq/libs/math/_quantummath.py @@ -512,7 +512,6 @@ def quantum_multiplication(eng, quint_a, quint_b, product): ) - def inverse_quantum_multiplication(eng, quint_a, quint_b, product): n_a = len(quint_a) @@ -538,4 +537,4 @@ def inverse_quantum_multiplication(eng, quint_a, quint_b, product): for i in reversed(range(0, n_a)): with Control(eng, [quint_a[i], quint_b[0]]): - X | product[i] \ No newline at end of file + X | product[i]