Stim parser
stim_parser
T = TypeVar('T')
module-attribute
SINGLE_QUBIT_GATE_OPS: dict[Instruction, type] = {Instruction.H: HOp, Instruction.S: SOp, Instruction.S_DAG: SDagOp, Instruction.X: XOp, Instruction.Y: YOp, Instruction.Z: ZOp, Instruction.I: IOp, Instruction.SQRT_X: SqrtXOp, Instruction.SQRT_X_DAG: SqrtXDagOp, Instruction.SQRT_Y: SqrtYOp, Instruction.SQRT_Y_DAG: SqrtYDagOp}
module-attribute
TWO_QUBIT_GATE_OPS: dict[Instruction, type] = {Instruction.CX: CXOp, Instruction.CY: CYOp, Instruction.CZ: CZOp, Instruction.SWAP: SwapOp, Instruction.ISWAP: ISwapOp, Instruction.ISWAP_DAG: ISwapDagOp}
module-attribute
MEASUREMENT_OPS: dict[Instruction, type] = {Instruction.M: MOp, Instruction.MX: MXOp, Instruction.MY: MYOp}
module-attribute
RESET_OPS: dict[Instruction, type] = {Instruction.R: ROp, Instruction.RX: RXOp, Instruction.RY: RYOp}
module-attribute
MEASURE_RESET_OPS: dict[Instruction, type] = {Instruction.MR: MROp, Instruction.MRX: MRXOp, Instruction.MRY: MRYOp}
module-attribute
NEWLINE = re.compile('\\n')
module-attribute
NOT_NEWLINE = re.compile('[^\\n]*')
module-attribute
INDENT = re.compile('[ \\t]*')
module-attribute
SPACE = re.compile('[ \\t]')
module-attribute
ARG = re.compile('\\d+(\\.\\d+)?')
module-attribute
TARGET = re.compile('\\d+')
module-attribute
NAME = re.compile('[a-zA-Z][a-zA-Z0-9_]*')
module-attribute
A regular expression for a name: a letter followed by zero or more letters, numbers, or underscores.
StimParseError
Bases: Exception
Source code in xdsl/dialects/stim/stim_parser.py
48 49 50 51 52 53 54 55 | |
position: Position = position
instance-attribute
message: str = message
instance-attribute
__init__(position: Position, message: str) -> None
Source code in xdsl/dialects/stim/stim_parser.py
52 53 54 55 | |
Instruction
Bases: StrEnum
Enum for the parse-able instructions in Stim.
Source code in xdsl/dialects/stim/stim_parser.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
COORD = 'QUBIT_COORDS'
class-attribute
instance-attribute
H = 'H'
class-attribute
instance-attribute
S = 'S'
class-attribute
instance-attribute
S_DAG = 'S_DAG'
class-attribute
instance-attribute
X = 'X'
class-attribute
instance-attribute
Y = 'Y'
class-attribute
instance-attribute
Z = 'Z'
class-attribute
instance-attribute
I = 'I'
class-attribute
instance-attribute
SQRT_X = 'SQRT_X'
class-attribute
instance-attribute
SQRT_X_DAG = 'SQRT_X_DAG'
class-attribute
instance-attribute
SQRT_Y = 'SQRT_Y'
class-attribute
instance-attribute
SQRT_Y_DAG = 'SQRT_Y_DAG'
class-attribute
instance-attribute
CX = 'CX'
class-attribute
instance-attribute
CY = 'CY'
class-attribute
instance-attribute
CZ = 'CZ'
class-attribute
instance-attribute
SWAP = 'SWAP'
class-attribute
instance-attribute
ISWAP = 'ISWAP'
class-attribute
instance-attribute
ISWAP_DAG = 'ISWAP_DAG'
class-attribute
instance-attribute
M = 'M'
class-attribute
instance-attribute
MX = 'MX'
class-attribute
instance-attribute
MY = 'MY'
class-attribute
instance-attribute
R = 'R'
class-attribute
instance-attribute
RX = 'RX'
class-attribute
instance-attribute
RY = 'RY'
class-attribute
instance-attribute
MR = 'MR'
class-attribute
instance-attribute
MRX = 'MRX'
class-attribute
instance-attribute
MRY = 'MRY'
class-attribute
instance-attribute
StimParser
Source code in xdsl/dialects/stim/stim_parser.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
input: Input = input
instance-attribute
pos: int = pos
instance-attribute
remaining: str
property
__init__(input: Input | str, pos: int = 0)
Source code in xdsl/dialects/stim/stim_parser.py
153 154 155 156 157 | |
parse_optional_chars(chars: str)
Source code in xdsl/dialects/stim/stim_parser.py
165 166 167 168 | |
parse_optional_pattern(pattern: re.Pattern[str])
Source code in xdsl/dialects/stim/stim_parser.py
170 171 172 173 | |
expect(message: str, parse: Callable[[StimParser], T | None]) -> T
Source code in xdsl/dialects/stim/stim_parser.py
178 179 180 181 | |
parse_one_of(StimParsers: Sequence[Callable[[StimParser], T | None]]) -> T | None
Source code in xdsl/dialects/stim/stim_parser.py
183 184 185 186 187 188 | |
parse_circuit() -> StimCircuitOp
Parse Stim dialect operations from a string formatted as a Stim file and return a StimCircuitOp containing the operations in its single block.
Circuits have format
circuit ::= (line)*
Collect by operations instead of by line to skip any lines that are not converted into operations.
Source code in xdsl/dialects/stim/stim_parser.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
parse_optional_comment() -> None
Parse a comment if there, but this is not stored.
Source code in xdsl/dialects/stim/stim_parser.py
209 210 211 212 213 214 215 216 217 218 219 220 221 | |
parse_optional_operation() -> Operation | None
Stim lines have format
line ::= indent? (instruction | block_start | block_end)? (comment ::= `#' NotNewLine)? NEWLINE
As this parser goes straight to operations, and stim comments, indentations and empty lines have no semantic meaning we look for operations, then skip straight through any lines without instructions or block starts or ends.
Operations are given by instructions, or the a block containing instructions.
Source code in xdsl/dialects/stim/stim_parser.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
parse_optional_instruction() -> Operation | None
Parse instruction with format
instruction ::= name (parens_arguments)? targets
Source code in xdsl/dialects/stim/stim_parser.py
252 253 254 255 256 257 258 259 260 261 262 263 264 | |
parse_optional_paren()
Parse an argument passed to an instruction in parentheses with format
arg ::= double
Source code in xdsl/dialects/stim/stim_parser.py
267 268 269 270 271 272 273 274 275 | |
parse_optional_parens() -> list[float] | None
Parse an optional parenthesis with optional arguments with format
parens ::= ( (INDENT arg INDENT (',' INDENT arg INDENT)*)? )
The Stim documentation uses this format:
but its implementation accepts empty arguments - this is kept here to be consistent.
Source code in xdsl/dialects/stim/stim_parser.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
parse_optional_target() -> QubitAttr | None
Parse a target with format
target ::=
TODO: Currently only supports target ::= qubit_target as the other relevant operations are not yet supported
Source code in xdsl/dialects/stim/stim_parser.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
parse_targets() -> list[QubitAttr]
Parse targets with format
targets = SPACE INDENTS target targets?
TODO: the Stim documentation indicates that their parser requires at least one target per instruction - but their actual parser does not enforce this. Check incongruency.
Source code in xdsl/dialects/stim/stim_parser.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
build_parens(parens: list[float]) -> ArrayAttr[FloatData | IntAttr]
Convert a list of parens into an ArrayAttr.
Source code in xdsl/dialects/stim/stim_parser.py
347 348 349 350 351 352 353 354 355 356 | |
build_operation(op: Instruction, parens: list[float], targets: Sequence[QubitAttr])
Build the operation corresponding to the name, parens, and targets found by the parser.
Source code in xdsl/dialects/stim/stim_parser.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
Comments have format
comment ::=
#NOT_NEWLINE