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 73bd36742 hotfix: bind parameters in `get_final_state_vector()` for
all backends (#975)
73bd36742 is described below
commit 73bd36742869a9e2e82c1023bfcbf3d3a5519737
Author: Ryan Huang <[email protected]>
AuthorDate: Thu Jan 29 20:56:29 2026 +0800
hotfix: bind parameters in `get_final_state_vector()` for all backends
(#975)
* feat: support parameter binding for circuit execution in backends
* linter
---
qumat/amazon_braket_backend.py | 12 +++++++++++-
qumat/cirq_backend.py | 7 ++++++-
qumat/qiskit_backend.py | 8 ++++++++
qumat/qumat.py | 23 ++++++++++++++++++++++-
4 files changed, 47 insertions(+), 3 deletions(-)
diff --git a/qumat/amazon_braket_backend.py b/qumat/amazon_braket_backend.py
index 7f6ad6fd5..8710ab0f9 100644
--- a/qumat/amazon_braket_backend.py
+++ b/qumat/amazon_braket_backend.py
@@ -105,7 +105,17 @@ def execute_circuit(circuit, backend, backend_config):
# placeholder method for use in the testing suite
def get_final_state_vector(circuit, backend, backend_config):
circuit.state_vector()
- result = backend.run(circuit, shots=0).result()
+ parameter_values = backend_config.get("parameter_values", {})
+ if parameter_values and circuit.parameters:
+ # Braket accepts parameter names as strings in inputs dict
+ inputs = {
+ param_name: value
+ for param_name, value in parameter_values.items()
+ if param_name in {p.name for p in circuit.parameters}
+ }
+ result = backend.run(circuit, shots=0, inputs=inputs).result()
+ else:
+ result = backend.run(circuit, shots=0).result()
state_vector = result.values[0]
return state_vector
diff --git a/qumat/cirq_backend.py b/qumat/cirq_backend.py
index e22b3e3b6..3c2a3e3fb 100644
--- a/qumat/cirq_backend.py
+++ b/qumat/cirq_backend.py
@@ -159,7 +159,12 @@ def apply_u_gate(circuit, qubit_index, theta, phi, lambd):
def get_final_state_vector(circuit, backend, backend_config):
simulator = cirq.Simulator()
- result = simulator.simulate(circuit)
+ parameter_values = backend_config.get("parameter_values", None)
+ if parameter_values:
+ resolver = cirq.ParamResolver(parameter_values)
+ result = simulator.simulate(circuit, param_resolver=resolver)
+ else:
+ result = simulator.simulate(circuit)
return result.final_state_vector
diff --git a/qumat/qiskit_backend.py b/qumat/qiskit_backend.py
index 25f6bb97a..88acaf9a5 100644
--- a/qumat/qiskit_backend.py
+++ b/qumat/qiskit_backend.py
@@ -147,6 +147,14 @@ def get_final_state_vector(circuit, backend,
backend_config):
# Add save_statevector instruction
working_circuit.save_statevector()
+ # Bind parameters if present
+ if working_circuit.parameters:
+ parameter_values = backend_config.get("parameter_values", {})
+ parameter_bindings = {
+ param: parameter_values[str(param)] for param in
working_circuit.parameters
+ }
+ working_circuit = working_circuit.assign_parameters(parameter_bindings)
+
# Simulate the circuit
transpiled_circuit = qiskit.transpile(working_circuit, simulator)
job = simulator.run(transpiled_circuit)
diff --git a/qumat/qumat.py b/qumat/qumat.py
index 59c74313b..ae21ec335 100644
--- a/qumat/qumat.py
+++ b/qumat/qumat.py
@@ -356,13 +356,34 @@ class QuMat:
"""Return the final state vector of the quantum circuit.
The complete quantum state vector after circuit execution,
- representing the full quantum state of all qubits.
+ representing the full quantum state of all qubits. For parameterized
+ circuits, call bind_parameters() first to set parameter values.
:returns: The final state vector as a numpy array.
:rtype: numpy.ndarray
:raises RuntimeError: If the circuit has not been initialized.
+ :raises ValueError: If parameterized circuit has unbound parameters.
"""
self._ensure_circuit_initialized()
+
+ # Only pass bound parameters (non-None values) to backend
+ bound_parameters = {
+ param: value
+ for param, value in self.parameters.items()
+ if value is not None
+ }
+
+ # Check if there are unbound parameters in the circuit
+ if self.parameters and len(bound_parameters) < len(self.parameters):
+ unbound_params = [
+ p for p in self.parameters.keys() if self.parameters[p] is None
+ ]
+ raise ValueError(
+ f"Circuit contains unbound parameters: {unbound_params}. "
+ f"Please call bind_parameters() before
get_final_state_vector()."
+ )
+
+ self.backend_config["parameter_values"] = bound_parameters
return self.backend_module.get_final_state_vector(
self.circuit, self.backend, self.backend_config
)