run_simulation
Execute a stored quantum circuit using Qiskit AerSimulator with configurable depolarizing noise. Returns measurement counts, probabilities, and simulation metadata. Select noise preset: ideal, low, or high noise.
Instructions
Run a stored circuit on AerSimulator with optional depolarizing noise. noise_preset: ideal | low_noise | high_noise. Returns counts, probabilities, metadata.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| circuit_id | Yes | ||
| shots | Yes | ||
| noise_preset | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The main handler function 'run_simulation' that executes the tool logic: validates noise_preset, retrieves circuit by ID, builds noise model, runs AerSimulator with given shots, and returns JSON with counts, probabilities, and metadata.
def run_simulation(circuit_id: str, shots: int, noise_preset: str) -> str: if noise_preset not in _VALID_NOISE_PRESETS: return json.dumps(mcp_error( f"Unknown noise_preset: {noise_preset!r}. Choose from {sorted(_VALID_NOISE_PRESETS)}" )) try: circuit = get_circuit(circuit_id) except CircuitNotFoundError as e: return json.dumps(mcp_error(str(e))) noise_model = _build_noise_model(noise_preset) simulator = AerSimulator(noise_model=noise_model) transpiled = transpile(circuit, backend=simulator) result = simulator.run(transpiled, shots=shots).result() counts_raw = result.get_counts() counts = cast_counts(counts_raw) try: verify_shot_count(shots, counts) except ShotCountMismatchError as e: return json.dumps(mcp_error(str(e))) return json.dumps({ "counts": counts, "probabilities": to_probabilities(counts), "metadata": {"circuit_id": circuit_id, "shots": shots, "noise_preset": noise_preset}, }) - Input validation constants: _VALID_NOISE_PRESETS defines accepted noise presets (ideal, low_noise, high_noise) and _NOISE_P maps noise levels to depolarizing error probabilities.
_VALID_NOISE_PRESETS = {"ideal", "low_noise", "high_noise"} _NOISE_P = {"low_noise": 0.001, "high_noise": 0.01} - src/quantum_mcp_demo/server.py:23-28 (registration)Tool registration in FastMCP server: wraps run_simulation as an MCP tool with description of parameters (circuit_id, shots, noise_preset) and return type.
mcp.tool( description=( "Run a stored circuit on AerSimulator with optional depolarizing noise. " "noise_preset: ideal | low_noise | high_noise. Returns counts, probabilities, metadata." ) )(run_simulation) - src/quantum_mcp_demo/tools/__init__.py:3-5 (registration)Exports run_simulation from the tools package so it can be imported by server.py.
from .simulate import run_simulation __all__ = ["create_circuit", "visualize_circuit", "run_simulation"] - Helper function _build_noise_model: returns None for 'ideal' or constructs a NoiseModel with depolarizing errors on single- and two-qubit gates for low_noise/high_noise presets.
def _build_noise_model(preset: str) -> NoiseModel | None: if preset == "ideal": return None p = _NOISE_P[preset] nm = NoiseModel() nm.add_all_qubit_quantum_error(depolarizing_error(p, 1), ["h", "x", "rx", "ry"]) nm.add_all_qubit_quantum_error(depolarizing_error(p, 2), ["cx", "cz"]) return nm