You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ak...@apache.org on 2026/10/20 17:57:07 UTC

(mahout) 02/04: [Qumat] Allow one parameter name in several Qiskit gates (#749)

This is an automated email from the ASF dual-hosted git repository.

andrewmusselman pushed a commit to branch pr/749-param-optimization
in repository https://gitbox.apache.org/repos/asf/mahout.git

commit 3a4ce89ac59344d0128649c68f263159e6743921
Author: Andrew Musselman <ak...@apache.org>
AuthorDate: Wed Oct 7 22:50:20 2026 -0700

    [Qumat] Allow one parameter name in several Qiskit gates (#749)
    
    Qumat registers a parameter by name, and on Cirq (sympy.Symbol) and
    Braket (FreeParameter) two gates that use the same name share the
    parameter. The Qiskit backend created a new qiskit Parameter object
    on every apply_r*_gate call, and Qiskit rejects two distinct objects
    with the same name in one circuit:
    
        CircuitError: "name conflict adding parameter 'theta'"
    
    Look the name up with QuantumCircuit.get_parameter() and reuse the
    existing object when there is one. Shared parameters are common in
    ansatz and data-reuploading circuits, and the parameter-shift
    gradient added next handles them explicitly, so all three backends
    need to accept them.
    
    Add a cross-backend test: RY(theta) RY(theta) with theta = pi/2 must
    flip |0> to |1>. It fails on Qiskit without this change.
    
    Part of #749
    
    Co-Authored-By: Claude Opus 5.5 <no...@anthropic.com>
---
 qumat/qiskit_backend.py                 | 21 ++++++++++++++++++---
 testing/qumat/test_parameter_binding.py | 24 ++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/qumat/qiskit_backend.py b/qumat/qiskit_backend.py
index e8df0c694..c78264264 100644
--- a/qumat/qiskit_backend.py
+++ b/qumat/qiskit_backend.py
@@ -209,12 +209,27 @@ def draw_circuit(circuit: qiskit.QuantumCircuit) -> Any:
     return circuit.draw()
 
 
+def _resolve_angle(
+    circuit: qiskit.QuantumCircuit,
+    angle: float | str | qiskit.circuit.Parameter,
+) -> Any:
+    """Return a Qiskit angle, reusing an existing Parameter with the same name.
+
+    Qiskit refuses two distinct ``Parameter`` objects with the same name in
+    one circuit, so a name used by several gates must map to one object.
+    """
+    if not isinstance(angle, str):
+        return angle
+    existing = circuit.get_parameter(angle, None)
+    return existing if existing is not None else qiskit.circuit.Parameter(angle)
+
+
 def apply_rx_gate(
     circuit: qiskit.QuantumCircuit,
     qubit_index: int,
     angle: float | str | qiskit.circuit.Parameter,
 ) -> None:
-    param = qiskit.circuit.Parameter(angle) if isinstance(angle, str) else angle
+    param = _resolve_angle(circuit, angle)
     circuit.rx(param, qubit_index)
 
 
@@ -223,7 +238,7 @@ def apply_ry_gate(
     qubit_index: int,
     angle: float | str | qiskit.circuit.Parameter,
 ) -> None:
-    param = qiskit.circuit.Parameter(angle) if isinstance(angle, str) else angle
+    param = _resolve_angle(circuit, angle)
     circuit.ry(param, qubit_index)
 
 
@@ -232,7 +247,7 @@ def apply_rz_gate(
     qubit_index: int,
     angle: float | str | qiskit.circuit.Parameter,
 ) -> None:
-    param = qiskit.circuit.Parameter(angle) if isinstance(angle, str) else angle
+    param = _resolve_angle(circuit, angle)
     circuit.rz(param, qubit_index)
 
 
diff --git a/testing/qumat/test_parameter_binding.py b/testing/qumat/test_parameter_binding.py
index 7a7976cde..eab180d1b 100644
--- a/testing/qumat/test_parameter_binding.py
+++ b/testing/qumat/test_parameter_binding.py
@@ -256,3 +256,27 @@ class TestParameterBinding:
         assert backend_config == original_config, (
             f"backend_config was mutated by get_final_state_vector in {backend_name}."
         )
+
+    @pytest.mark.parametrize("backend_name", TESTING_BACKENDS)
+    def test_parameter_reused_across_gates(self, backend_name):
+        """One parameter name may drive several gates (shared parameter).
+
+        RY(theta) applied twice equals RY(2 * theta), which takes |0> to |1>
+        with probability sin^2(theta). With theta = pi/2 that is 1, whereas a
+        single RY(pi/2) would give 0.5. Qiskit used to raise CircuitError
+        ("name conflict adding parameter") here.
+        """
+        backend_config = get_backend_config(backend_name)
+        qumat = QuMat(backend_config)
+        qumat.create_empty_circuit(num_qubits=1)
+        qumat.apply_ry_gate(0, "theta")
+        qumat.apply_ry_gate(0, "theta")
+        assert list(qumat.parameters) == ["theta"]
+
+        results = qumat.execute_circuit(parameter_values={"theta": math.pi / 2})
+
+        prob = get_state_probability(results, "1", num_qubits=1)
+        assert prob > 0.95, (
+            f"Expected |1> after RY(theta)RY(theta), theta=pi/2, in "
+            f"{backend_name}, got probability {prob:.4f}"
+        )