poseidon666 commited on
Commit
b09af2d
·
verified ·
1 Parent(s): ee6ae39

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. graders/unitary.py +9 -5
  2. server/my_env_environment.py +64 -17
  3. validate.sh +0 -0
graders/unitary.py CHANGED
@@ -54,6 +54,9 @@ class UnitaryGrader:
54
  Returns:
55
  Fidelity score in [0.0, 1.0].
56
  """
 
 
 
57
  dim = 2 ** num_qubits
58
  if target_unitary.shape != (dim, dim):
59
  logger.error("Unitary shape mismatch: target=%s, expected=(%d, %d)", target_unitary.shape, dim, dim)
@@ -64,10 +67,11 @@ class UnitaryGrader:
64
 
65
  # Compute normalized trace |Tr(U_t^dagger U_c)| / d
66
  trace_val = np.trace(target_unitary.conj().T @ current_unitary)
67
- fidelity = float(np.abs(trace_val) / dim)
68
 
69
- # Assertion: fidelity must be in [0, 1]
70
- assert 0.0 <= fidelity <= 1.0 + 1e-9, f"Unitary fidelity out of range: {fidelity}"
 
71
 
72
- logger.debug("Unitary Fidelity: %.6f (dim=%d)", fidelity, dim)
73
- return min(max(fidelity, 0.0), 1.0)
 
54
  Returns:
55
  Fidelity score in [0.0, 1.0].
56
  """
57
+ if len(current_gates) == 0:
58
+ return 0.2
59
+
60
  dim = 2 ** num_qubits
61
  if target_unitary.shape != (dim, dim):
62
  logger.error("Unitary shape mismatch: target=%s, expected=(%d, %d)", target_unitary.shape, dim, dim)
 
67
 
68
  # Compute normalized trace |Tr(U_t^dagger U_c)| / d
69
  trace_val = np.trace(target_unitary.conj().T @ current_unitary)
70
+ fidelity = abs(trace_val) / dim
71
 
72
+ # Shaping for RL friendliness
73
+ fidelity = max(0.1, fidelity)
74
+ fidelity -= 0.01 * len(current_gates)
75
 
76
+ # Clip and return
77
+ return float(np.clip(fidelity, 0.0, 1.0))
server/my_env_environment.py CHANGED
@@ -240,6 +240,8 @@ class QuantumCircuitEnvironment(Environment):
240
  self._prev_score: float = 0.0
241
  self._prev_fidelity: float = 0.0
242
  self._prev_depth: int = 0
 
 
243
  self._episode_id: str = str(uuid4())
244
 
245
  # Graders
@@ -297,6 +299,8 @@ class QuantumCircuitEnvironment(Environment):
297
  self._gates = []
298
  self._step_count = 0
299
  self._done = False
 
 
300
  self._episode_id = str(uuid4())
301
  self._swap_count = 0
302
 
@@ -305,7 +309,11 @@ class QuantumCircuitEnvironment(Environment):
305
  self._prev_score = initial_scores["aggregate"]
306
  self._prev_fidelity = initial_scores["fidelity"]
307
  self._prev_depth = initial_scores["depth"]
308
- self._prev_penalty = (1.0 - initial_scores["efficiency"]) + (1.0 - initial_scores["noise"]) + (1.0 - initial_scores["constraints"])
 
 
 
 
309
 
310
  return self._build_observation(reward=0.0)
311
 
@@ -374,33 +382,72 @@ class QuantumCircuitEnvironment(Environment):
374
  # STEP 7: Reward function (physically meaningful)
375
  # ------------------------------------------------------------------
376
 
377
- # Calculate current penalties (inversion of efficiency/noise/constraints)
378
- current_penalty = (1.0 - scores["efficiency"]) + (1.0 - scores["noise"]) + (1.0 - scores["constraints"])
379
-
380
- # Add explicit SWAP penalty to the tracked penalties
381
- if action.action_type == ActionType.SWAP:
382
- self._swap_count += 1
383
- current_penalty += 0.02 * self._swap_count
384
-
385
- # Add redundancy penalty
386
  if len(self._gates) >= 2:
387
  prev_gate = self._gates[-2]
388
  curr_gate = self._gates[-1]
389
- if (prev_gate.get("gate") == curr_gate.get("gate") and prev_gate.get("qubits") == curr_gate.get("qubits")):
390
- current_penalty += 0.01
391
 
 
 
 
 
 
 
 
 
 
392
  fidelity_delta = current_fidelity - self._prev_fidelity
393
  penalty_delta = current_penalty - getattr(self, '_prev_penalty', 0.0)
394
 
395
- reward = fidelity_delta * 1.5 - penalty_delta
396
-
397
- # Overfitting penalty: high fidelity but unnecessarily deep
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
  optimal_depth = self._task_config.get("optimal_depth", 5)
399
  if current_fidelity > 0.95 and current_depth > optimal_depth:
400
  reward -= 0.1
401
 
402
- # Clamp reward
403
- reward = max(-1.0, min(1.0, reward))
 
 
 
 
404
 
405
  # Episode limit
406
  if self._step_count >= self._max_steps:
 
240
  self._prev_score: float = 0.0
241
  self._prev_fidelity: float = 0.0
242
  self._prev_depth: int = 0
243
+ self._prev_action: Optional[ActionType] = None
244
+ self._last_reward: float = 0.0
245
  self._episode_id: str = str(uuid4())
246
 
247
  # Graders
 
299
  self._gates = []
300
  self._step_count = 0
301
  self._done = False
302
+ self._prev_action = None
303
+ self._last_reward = 0.0
304
  self._episode_id = str(uuid4())
305
  self._swap_count = 0
306
 
 
309
  self._prev_score = initial_scores["aggregate"]
310
  self._prev_fidelity = initial_scores["fidelity"]
311
  self._prev_depth = initial_scores["depth"]
312
+ self._prev_penalty = (
313
+ 0.5 * (1.0 - initial_scores["efficiency"]) +
314
+ 0.3 * (1.0 - initial_scores["noise"]) +
315
+ 0.2 * (1.0 - initial_scores["constraints"])
316
+ )
317
 
318
  return self._build_observation(reward=0.0)
319
 
 
382
  # STEP 7: Reward function (physically meaningful)
383
  # ------------------------------------------------------------------
384
 
385
+ # --- Penalty computation ---
386
+ current_penalty = (
387
+ 0.5 * (1.0 - scores["efficiency"]) +
388
+ 0.3 * (1.0 - scores["noise"]) +
389
+ 0.2 * (1.0 - scores["constraints"])
390
+ )
391
+
392
+ # Redundancy penalty
 
393
  if len(self._gates) >= 2:
394
  prev_gate = self._gates[-2]
395
  curr_gate = self._gates[-1]
396
+ if prev_gate.get("gate") == curr_gate.get("gate") and prev_gate.get("qubits") == curr_gate.get("qubits"):
397
+ current_penalty += 0.005
398
 
399
+ # PARAM penalty
400
+ if action.action_type == ActionType.PARAM:
401
+ current_penalty += 0.003
402
+
403
+ # SWAP penalty
404
+ if action.action_type == ActionType.SWAP:
405
+ current_penalty += 0.02
406
+
407
+ # --- Reward core ---
408
  fidelity_delta = current_fidelity - self._prev_fidelity
409
  penalty_delta = current_penalty - getattr(self, '_prev_penalty', 0.0)
410
 
411
+ reward = fidelity_delta * 3.0 - penalty_delta * 0.5
412
+
413
+ # --- ADD vs REMOVE balance: reward constructive actions ---
414
+ if action.action_type == ActionType.ADD and fidelity_delta < 0:
415
+ reward += 0.15 # Increased from 0.1 to reduce ADD penalty
416
+
417
+ # --- REMOVE logic: NOT profitable ---
418
+ if action.action_type == ActionType.REMOVE:
419
+ # Cap REMOVE reward - it can never profit more than half of last reward
420
+ if self._last_reward != 0:
421
+ reward = min(reward, abs(self._last_reward) * 0.5)
422
+
423
+ # Small optimization bonus only if depth improved without fidelity loss
424
+ if current_depth < pre_action_depth and abs(current_fidelity - pre_action_fidelity) < 1e-6:
425
+ reward = min(reward + 0.005, 0.01) # Tiny bonus, capped at 0.01
426
+ elif current_fidelity < pre_action_fidelity:
427
+ reward -= 0.05 # Stronger penalty for fidelity loss
428
+
429
+ # --- Penalize ADD→REMOVE loops (direct exploit killer) ---
430
+ if action.action_type == ActionType.REMOVE and self._prev_action == ActionType.ADD:
431
+ reward -= 0.05
432
+
433
+ # --- Prevent REMOVE spam loops ---
434
+ if self._prev_action == ActionType.REMOVE:
435
+ reward -= 0.02
436
+
437
+ # --- Exploration bias ---
438
+ reward += 0.02
439
+
440
+ # --- Overfitting penalty ---
441
  optimal_depth = self._task_config.get("optimal_depth", 5)
442
  if current_fidelity > 0.95 and current_depth > optimal_depth:
443
  reward -= 0.1
444
 
445
+ # --- Smooth ---
446
+ reward = np.tanh(reward)
447
+
448
+ # Update trackers
449
+ self._prev_action = action.action_type
450
+ self._last_reward = reward
451
 
452
  # Episode limit
453
  if self._step_count >= self._max_steps:
validate.sh ADDED
File without changes