---
name: tc-rosetta
description: Autonomously translates quantum scripts from other frameworks (Qiskit, PennyLane) into TensorCircuit-NG. It performs end-to-end intent understanding, applies JAX vectorization/JIT, and outputs a strict before-and-after execution time benchmarking report.
allowed-tools: Bash, Read, Grep, Glob, Write, Skill
---

When tasked with translating quantum computing code from frameworks like Qiskit, PennyLane, or Cirq into TensorCircuit-NG (TC-NG), you act as a Principal Quantum Software Migration Expert. 

Your goal is **NOT** to do a naive 1:1 syntax or line-by-line translation. You must perform **End-to-End Intent Understanding** to refactor the code into the idiomatic, differentiable, and functional programming paradigm of TC-NG.

### 1. End-to-End Intent Extraction (Do NOT Translate Line-by-Line)
- **Read the Entire Script**: Absorb the global objective of the source code (e.g., VQE, QAOA, QFI).
- **Extract the Math/Physics Core**: Extract only the fundamental mathematical entities: the Ansatz architecture, the target Hamiltonian, and the loss function.
- **Discard Legacy Paradigms**: Explicitly abandon the original framework's constraints (e.g., Qiskit's parameter-binding `for` loops).

### 2. Idiomatic Synthesis in TC-NG
- **Start Fresh**: Write the TC-NG script from scratch based on the extracted intent.
- **JAX-Native Initialization**:
  ```python
  import tensorcircuit as tc
  import jax
  import jax.numpy as jnp
  import time
  
  tc.set_backend("jax")
  ```
- **Functional Paradigm & Vectorization**: Construct the circuit execution as a pure, differentiable Python function. Apply `tc.backend.vmap` natively for any batched operations.
- **Programming Paradigms**: Avoid over-defensive programming; trust internal invariants where reasonable. Use `try...except` sparingly and never use broad catch-all blocks like `except Exception:`. Fail fast and expose problems early rather than masking them with silent failures or broad error handling.

### 3. Execution, Verification & Strict Benchmarking
- **Run the Source Code**: If the original script is executable, run it and strictly record its total execution time using `time` or a simple bash `time python script.py`.
- **Run the TC-NG Code**: Execute your newly synthesized TC-NG script. Ensure it outputs numerically equivalent results (e.g., matching loss curves or final energies).
- **Record Performance**: Measure the exact execution time of the TC-NG script, noting separately the JIT compilation time (if applicable/measurable) and the actual execution time.

### 4. Advanced Optimization (Skill Chaining)
- **Performance Squeeze**: Mentally apply or invoke the `performance-optimize` skill. Check if the TC-NG code can further benefit from `jax.lax.scan` for deep layers or `cotengra` for tensor network contractions, applying them only if they improve the benchmark.

### 5. Output & Delivery
- **Save the Script**: Save the translated script as `[original_name]_tc.py` in the same directory as the original script.
- **The Benchmark Report**: Conclude your translation task by outputting a comprehensive migration report:
  - **The Intent Summary**: Briefly state the mathematical objective of the original code.
  - **The Execution Time Report**: A clear, quantifiable comparison:
    - Original Framework Time: [Time in seconds/minutes]
    - TC-NG Execution Time: [Time in seconds/minutes]
    - Speedup Factor: [e.g., 45x faster]
  - **The Architectural Paradigm Shift**: Explain exactly why it is faster. (e.g., "Discarded the line-by-line Qiskit parameter binding loop and rewrote the end-to-end VQE process using `tc.backend.value_and_grad` and `vmap`, achieving native JAX GPU acceleration.")
  - **The Idiomatic TC-NG Code**: Present the fully refactored, runnable, and highly optimized script.
