Download example_plugins/prefix_x/handler.py from jqub21/quaim: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/jqub21/quaim/resolve/8311ef0480c4e9dd94de89120b420e1957f31fc9/example_plugins/prefix_x/handler.py
- Command line
-
hf download hf://spaces/jqub21/quaim@8311ef0480c4e9dd94de89120b420e1957f31fc9/example_plugins/prefix_x/handler.py
-
curl -L -o handler.py https://huggingface.co/spaces/jqub21/quaim/resolve/8311ef0480c4e9dd94de89120b420e1957f31fc9/example_plugins/prefix_x/handler.py
1.36 kB
| """Tiny algorithm-family plugin: prepend an X gate on the chosen qubit. | |
| Demonstrates returning a modified circuit. The new circuit propagates | |
| to downstream blocks (Output, Fidelity, etc.) just like a built-in | |
| algorithm block would. | |
| """ | |
| from __future__ import annotations | |
| import io | |
| def run(inputs: dict, params: dict) -> dict: | |
| from qiskit import QuantumCircuit, qpy | |
| blob = inputs.get("circuit_qpy_bytes") | |
| if not blob: | |
| return {"error": "No upstream circuit. Place this block downstream of an Input."} | |
| loaded = qpy.load(io.BytesIO(blob)) | |
| qc = loaded[0] if isinstance(loaded, list) else loaded | |
| target = int(params.get("target_qubit", 0)) | |
| if not (0 <= target < qc.num_qubits): | |
| return { | |
| "error": ( | |
| f"target_qubit={target} is out of range for a " | |
| f"{qc.num_qubits}-qubit circuit." | |
| ), | |
| } | |
| new_qc = QuantumCircuit(qc.num_qubits, name="x_prefixed") | |
| new_qc.x(target) | |
| new_qc.compose(qc, inplace=True) | |
| out = io.BytesIO() | |
| qpy.dump(new_qc, out) | |
| return { | |
| "summary": { | |
| "target_qubit": target, | |
| "depth_before": qc.depth(), | |
| "depth_after": new_qc.depth(), | |
| "size_before": qc.size(), | |
| "size_after": new_qc.size(), | |
| }, | |
| "circuit_qpy_bytes": out.getvalue(), | |
| } | |