Skip to content

Commit 96313c9

Browse files
committed
Add support for non-parametric circuits
1 parent 9561c9a commit 96313c9

File tree

2 files changed

+629
-30
lines changed

2 files changed

+629
-30
lines changed

hadamard_random_forest/sample.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
from __future__ import annotations
8-
from typing import Dict, List, Tuple , Any
8+
from typing import Dict, List, Tuple, Any, Optional
99
import warnings
1010
import functools
1111

@@ -87,8 +87,8 @@ def get_samples_noisy(
8787
num_qubits: int,
8888
circuits: List[qiskit.QuantumCircuit],
8989
shots: int,
90-
parameters: np.ndarray,
91-
backend_sim: Backend,
90+
parameters: Optional[np.ndarray] = None,
91+
backend_sim: Backend = None,
9292
error_mitigation: bool = False
9393
) -> List[np.ndarray]:
9494
"""
@@ -98,7 +98,8 @@ def get_samples_noisy(
9898
num_qubits: Number of qubits.
9999
circuits: List of QuantumCircuit to transpile and run.
100100
shots: Number of shots per circuit execution.
101-
parameters: Parameter values to assign.
101+
parameters: Optional parameter values to assign. If None, assumes circuits
102+
already have parameters bound.
102103
backend_sim: Qiskit backend to run circuits on.
103104
error_mitigation: If True, perform M3 calibration and mitigation.
104105
@@ -137,8 +138,9 @@ def get_samples_noisy(
137138
mit.cals_from_system(mapping)
138139
mapping_mit[key] = mit
139140

140-
# Assign parameters and execute the circuit.
141-
transpiled.assign_parameters(parameters, inplace=True)
141+
# Assign parameters if provided and circuit has unbound parameters
142+
if parameters is not None and transpiled.parameters:
143+
transpiled.assign_parameters(parameters, inplace=True)
142144
counts = backend_sim.run(transpiled, shots=shots).result().get_counts()
143145
counts_data.append((counts, mapping, key))
144146

@@ -160,7 +162,9 @@ def get_samples_noisy(
160162
else:
161163
for circuit in circuits:
162164
transpiled = pm.run(circuit)
163-
transpiled.assign_parameters(parameters, inplace=True)
165+
# Assign parameters if provided and circuit has unbound parameters
166+
if parameters is not None and transpiled.parameters:
167+
transpiled.assign_parameters(parameters, inplace=True)
164168
counts = backend_sim.run(transpiled, shots=shots).result().get_counts()
165169
proba = np.zeros(2**num_qubits, dtype=float)
166170
for bitstr, count in counts.items():
@@ -217,8 +221,8 @@ def get_samples_hardware(
217221
num_qubits: int,
218222
shots: int,
219223
circuits: List[qiskit.QuantumCircuit],
220-
parameters: np.ndarray,
221-
device: Backend,
224+
parameters: Optional[np.ndarray] = None,
225+
device: Backend = None,
222226
error_mitigation: bool = True
223227
) -> Tuple[List[np.ndarray], List[np.ndarray], List[str], List[float]]:
224228
"""
@@ -228,7 +232,8 @@ def get_samples_hardware(
228232
num_qubits: Number of qubits (defines vector size 2**num_qubits).
229233
shots: Number of shots per circuit execution.
230234
circuits: List of transpiled QuantumCircuit objects.
231-
parameters: 1D array of parameter values to bind to each circuit.
235+
parameters: Optional 1D array of parameter values to bind to each circuit.
236+
If None, assumes circuits already have parameters bound.
232237
device: Qiskit backend to run circuits on.
233238
error_mitigation: If True, perform M3 calibration and apply measurement mitigation.
234239
@@ -260,7 +265,11 @@ def get_samples_hardware(
260265
mapping_mit[key] = mit
261266

262267
# Run circuit on hardware
263-
job = sampler.run([(circ, parameters)])
268+
# Pass parameters only if provided, otherwise run circuit as-is
269+
if parameters is not None:
270+
job = sampler.run([(circ, parameters)])
271+
else:
272+
job = sampler.run([circ])
264273
result = job.result()[0]
265274
counts = result.data.meas.get_counts()
266275
results.append((counts, key))

0 commit comments

Comments
 (0)