|
23 | 23 |
|
24 | 24 | from __future__ import annotations |
25 | 25 |
|
26 | | -from typing import Any, Dict, List, Optional |
| 26 | +from typing import Any, Dict, List, Optional, Union |
27 | 27 |
|
28 | 28 | import numpy as np |
29 | 29 |
|
@@ -89,21 +89,23 @@ async def decode_endpoint(req: DecodeRequest) -> Dict[str, Any]: # type: ignore |
89 | 89 | raise HTTPException(status_code=400, detail="check_to_qubits must be non-empty") |
90 | 90 |
|
91 | 91 | try: |
| 92 | + dec_any: Union[BatchDecoder, UnionFindDecoder] |
92 | 93 | if req.use_batch: |
93 | | - dec = BatchDecoder(req.check_to_qubits, req.n_qubits) |
| 94 | + dec_any = BatchDecoder(req.check_to_qubits, req.n_qubits) |
94 | 95 | syndrome_arr = np.array([req.syndrome], dtype=np.uint8) |
95 | | - correction = dec.parallel_batch_decode(syndrome_arr)[0] |
| 96 | + correction = dec_any.parallel_batch_decode(syndrome_arr)[0] |
96 | 97 | else: |
97 | | - dec = UnionFindDecoder(req.check_to_qubits, req.n_qubits) |
| 98 | + uf_dec = UnionFindDecoder(req.check_to_qubits, req.n_qubits) |
| 99 | + dec_any = uf_dec |
98 | 100 | syndrome_arr = np.array(req.syndrome, dtype=np.uint8) |
99 | | - correction = dec.decode(syndrome_arr) |
| 101 | + correction = uf_dec.decode(syndrome_arr) |
100 | 102 | except Exception as exc: |
101 | 103 | raise HTTPException(status_code=500, detail=f"Decode error: {exc}") |
102 | 104 |
|
103 | 105 | return { |
104 | 106 | "correction": correction.tolist(), |
105 | | - "n_qubits": dec.n_qubits, |
106 | | - "n_checks": dec.n_checks, |
| 107 | + "n_qubits": dec_any.n_qubits, |
| 108 | + "n_checks": dec_any.n_checks, |
107 | 109 | "version": __version__, |
108 | 110 | } |
109 | 111 |
|
@@ -142,22 +144,24 @@ def decode_endpoint() -> Any: |
142 | 144 | return jsonify({"error": "check_to_qubits must be non-empty"}), 400 |
143 | 145 |
|
144 | 146 | try: |
| 147 | + dec_any: Union[BatchDecoder, UnionFindDecoder] |
145 | 148 | if use_batch: |
146 | | - dec = BatchDecoder(c2q, n_qubits) |
| 149 | + dec_any = BatchDecoder(c2q, n_qubits) |
147 | 150 | syndrome_arr = np.array([syndrome], dtype=np.uint8) |
148 | | - correction = dec.parallel_batch_decode(syndrome_arr)[0] |
| 151 | + correction = dec_any.parallel_batch_decode(syndrome_arr)[0] |
149 | 152 | else: |
150 | | - dec = UnionFindDecoder(c2q, n_qubits) |
| 153 | + uf_dec = UnionFindDecoder(c2q, n_qubits) |
| 154 | + dec_any = uf_dec |
151 | 155 | syndrome_arr = np.array(syndrome, dtype=np.uint8) |
152 | | - correction = dec.decode(syndrome_arr) |
| 156 | + correction = uf_dec.decode(syndrome_arr) |
153 | 157 | except Exception as exc: |
154 | 158 | return jsonify({"error": f"Decode error: {exc}"}), 500 |
155 | 159 |
|
156 | 160 | return jsonify( |
157 | 161 | { |
158 | 162 | "correction": correction.tolist(), |
159 | | - "n_qubits": dec.n_qubits, |
160 | | - "n_checks": dec.n_checks, |
| 163 | + "n_qubits": dec_any.n_qubits, |
| 164 | + "n_checks": dec_any.n_checks, |
161 | 165 | "version": __version__, |
162 | 166 | } |
163 | 167 | ) |
|
0 commit comments