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

guanmingchiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/mahout.git


The following commit(s) were added to refs/heads/main by this push:
     new a59e8b8ba Avoid mutating user backend config (#1076)
a59e8b8ba is described below

commit a59e8b8bac3177c3138dfa6f8cb65ab370827c36
Author: Shivam Mittal <[email protected]>
AuthorDate: Mon Feb 23 10:21:38 2026 +0530

    Avoid mutating user backend config (#1076)
---
 qumat/qumat.py                          |  6 +++---
 testing/qumat/test_parameter_binding.py | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/qumat/qumat.py b/qumat/qumat.py
index ae21ec335..449938666 100644
--- a/qumat/qumat.py
+++ b/qumat/qumat.py
@@ -60,12 +60,12 @@ class QuMat:
                 "Please provide a dictionary with backend-specific options "
             )
 
-        self.backend_config = backend_config
-        self.backend_name = backend_config["backend_name"]
+        self.backend_config = backend_config.copy()
+        self.backend_name = self.backend_config["backend_name"]
         self.backend_module = import_module(
             f".{self.backend_name}_backend", package="qumat"
         )
-        self.backend = self.backend_module.initialize_backend(backend_config)
+        self.backend = 
self.backend_module.initialize_backend(self.backend_config)
         self.circuit = None
         self.num_qubits = None
         self.parameters = {}
diff --git a/testing/qumat/test_parameter_binding.py 
b/testing/qumat/test_parameter_binding.py
index b304e8e5c..ae8fdb9e6 100644
--- a/testing/qumat/test_parameter_binding.py
+++ b/testing/qumat/test_parameter_binding.py
@@ -220,3 +220,35 @@ class TestParameterBinding:
         # This should raise ValueError
         with pytest.raises(ValueError, match="unbound parameters"):
             qumat.execute_circuit(parameter_values={"theta0": math.pi})
+
+    @pytest.mark.parametrize("backend_name", TESTING_BACKENDS)
+    def test_execute_circuit_does_not_mutate_backend_config(self, 
backend_name):
+        """Test that execute_circuit does not mutate the user's backend_config 
across all backends."""
+        backend_config = get_backend_config(backend_name).copy()
+        original_config = backend_config.copy()
+
+        qumat = QuMat(backend_config)
+        qumat.create_empty_circuit(num_qubits=1)
+        qumat.apply_rx_gate(0, "theta")
+        qumat.execute_circuit(parameter_values={"theta": math.pi})
+
+        assert backend_config == original_config, (
+            f"backend_config was mutated in {backend_name}; "
+            "parameter_values or other keys must not be added."
+        )
+
+    @pytest.mark.parametrize("backend_name", TESTING_BACKENDS)
+    def test_get_final_state_vector_does_not_mutate_backend_config(self, 
backend_name):
+        """Test that get_final_state_vector does not mutate the user's 
backend_config across all backends."""
+        backend_config = get_backend_config(backend_name).copy()
+        original_config = backend_config.copy()
+
+        qumat = QuMat(backend_config)
+        qumat.create_empty_circuit(num_qubits=1)
+        qumat.apply_rx_gate(0, "theta")
+        qumat.bind_parameters({"theta": math.pi / 2})
+        qumat.get_final_state_vector()
+
+        assert backend_config == original_config, (
+            f"backend_config was mutated by get_final_state_vector in 
{backend_name}."
+        )

Reply via email to