Yuchen Yuan commited on
Commit
8a2d350
·
1 Parent(s): 102bda8

Validate fidelity method + fake-backend name; refuse unknown strings

Browse files

A4 fuzzing found three silent-coerce UX cracks:
* backend_name='FakeBollywood' silently fell back to FakeFez
* backend_name='' (empty) silently fell back to FakeFez
* method='banana' on Fidelity silently picked statevector

In all three the run reported ok and produced a number — but the
number was for inputs the user didn't ask for. That's worse than
erroring: it pretends to have done what was requested.

Replace the silent coerce with explicit validation. Allowed values
are enumerated in the error message so the user knows what to type.

Also surface the same hardening on unbound_param_policy (same enum
trap pattern).

backend/app/services/workflow_service.py CHANGED
@@ -160,6 +160,9 @@ def _default_label(node_type: str) -> str:
160
  }.get(node_type, node_type)
161
 
162
 
 
 
 
163
  def _load_fake_backend(name: str):
164
  """Lazy-import a fake backend by name; falls back to FakeFez."""
165
  from qiskit_ibm_runtime.fake_provider import FakeFez, FakeMarrakesh, FakeTorino
@@ -171,6 +174,10 @@ def _load_fake_backend(name: str):
171
  }.get(name, FakeFez)()
172
 
173
 
 
 
 
 
174
  # ---------- Per-node handlers ----------
175
 
176
  def _handle_fake_backend(node: FlowNode, ctx: dict, _settings: Settings) -> StepResult:
@@ -179,7 +186,28 @@ def _handle_fake_backend(node: FlowNode, ctx: dict, _settings: Settings) -> Step
179
  actually run measurements (e.g. Fidelity in `sampled` mode) can
180
  pick it up from ctx without each having its own shots param."""
181
  t0 = _now()
182
- name = node.data.get("backend_name", "FakeFez")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  # Clamp shots to a sensible range to avoid DOS via "shots=10**9".
184
  shots_raw = node.data.get("shots", 1024)
185
  shots = int(shots_raw) if isinstance(shots_raw, (int, float)) else 1024
@@ -555,6 +583,27 @@ def _handle_fidelity(node: FlowNode, ctx: dict, _settings: Settings) -> StepResu
555
  method = node.data.get("method", "statevector")
556
  unbound_policy = node.data.get("unbound_param_policy", "bind_zero")
557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558
  try:
559
  if method == "sampled":
560
  backend = ctx.get("backend")
 
160
  }.get(node_type, node_type)
161
 
162
 
163
+ _FAKE_BACKENDS_AVAILABLE = ("FakeFez", "FakeMarrakesh", "FakeTorino")
164
+
165
+
166
  def _load_fake_backend(name: str):
167
  """Lazy-import a fake backend by name; falls back to FakeFez."""
168
  from qiskit_ibm_runtime.fake_provider import FakeFez, FakeMarrakesh, FakeTorino
 
174
  }.get(name, FakeFez)()
175
 
176
 
177
+ def _is_known_fake_backend(name: str) -> bool:
178
+ return name in _FAKE_BACKENDS_AVAILABLE
179
+
180
+
181
  # ---------- Per-node handlers ----------
182
 
183
  def _handle_fake_backend(node: FlowNode, ctx: dict, _settings: Settings) -> StepResult:
 
186
  actually run measurements (e.g. Fidelity in `sampled` mode) can
187
  pick it up from ctx without each having its own shots param."""
188
  t0 = _now()
189
+ name_raw = node.data.get("backend_name", "FakeFez")
190
+ # Refuse empty / unknown names with a clear message instead of
191
+ # silently picking FakeFez — users typo backend names and got
192
+ # confused that their "FakeBollywood" run mysteriously matched
193
+ # FakeFez results.
194
+ if not isinstance(name_raw, str) or not name_raw:
195
+ return _make_step(
196
+ node, "error", started_at=t0,
197
+ message=(
198
+ f"Backend name must be a non-empty string. Allowed values: "
199
+ f"{', '.join(_FAKE_BACKENDS_AVAILABLE)}."
200
+ ),
201
+ )
202
+ if not _is_known_fake_backend(name_raw):
203
+ return _make_step(
204
+ node, "error", started_at=t0,
205
+ message=(
206
+ f"Unknown fake backend {name_raw!r}. Allowed values: "
207
+ f"{', '.join(_FAKE_BACKENDS_AVAILABLE)}."
208
+ ),
209
+ )
210
+ name = name_raw
211
  # Clamp shots to a sensible range to avoid DOS via "shots=10**9".
212
  shots_raw = node.data.get("shots", 1024)
213
  shots = int(shots_raw) if isinstance(shots_raw, (int, float)) else 1024
 
583
  method = node.data.get("method", "statevector")
584
  unbound_policy = node.data.get("unbound_param_policy", "bind_zero")
585
 
586
+ # Validate enums explicitly. Silently coercing "banana" → statevector
587
+ # produced confusing UX (user saw a value they didn't ask for).
588
+ _FIDELITY_METHODS = {"statevector", "sampled"}
589
+ _FIDELITY_POLICIES = {"bind_zero", "error"}
590
+ if method not in _FIDELITY_METHODS:
591
+ return _make_step(
592
+ node, "error", started_at=t0,
593
+ message=(
594
+ f"Unknown fidelity method {method!r}. "
595
+ f"Choose one of: {', '.join(sorted(_FIDELITY_METHODS))}."
596
+ ),
597
+ )
598
+ if unbound_policy not in _FIDELITY_POLICIES:
599
+ return _make_step(
600
+ node, "error", started_at=t0,
601
+ message=(
602
+ f"Unknown unbound_param_policy {unbound_policy!r}. "
603
+ f"Choose one of: {', '.join(sorted(_FIDELITY_POLICIES))}."
604
+ ),
605
+ )
606
+
607
  try:
608
  if method == "sampled":
609
  backend = ctx.get("backend")