Bitwise, shift and index operations on `bit[n]` registers raise raw `TypeError`
Los mantenedores suelen responder en 1 día
Evaluación
Este issue todavía no se ha evaluado.
Descripción
Limitation
The spec defines bit[n] as a register of bits supporting
bitwise and shift operators and
indexing / slicing.
pyqasm stores a bit[n] value as a Python str, so every one of these operations reaches
a Python operator that str does not implement. The result is an uncaught TypeError
escaping the public API instead of either a correct value or a ValidationError.
Example QASM failure
import pyqasm
for op in ["a | b", "a & b", "a ^ b"]:
m = pyqasm.loads(f'''
OPENQASM 3.0;
bit[4] a = "1010";
bit[4] b = "0101";
bit[4] c = {op};
''')
m.validate()
TypeError: unsupported operand type(s) for |: 'str' and 'str'
TypeError: unsupported operand type(s) for &: 'str' and 'str'
TypeError: unsupported operand type(s) for ^: 'str' and 'str'
Shifts fail the same way:
OPENQASM 3.0;
bit[4] a = "1010";
bit[4] c = a << 1;
// TypeError: unsupported operand type(s) for <<: 'str' and 'int'
Indexing and slicing also fail, and these are plain reads:
OPENQASM 3.0;
bit[8] b = "00101010";
bit c = b[0];
// TypeError: string indices must be integers, not 'tuple'
OPENQASM 3.0;
bit[8] b = "00101010";
bit[4] c = b[0:3];
// TypeError: string indices must be integers, not 'tuple'
Related, and blocked by the same representation:
OPENQASM 3.0;
bit[4] a = "1010";
bit[4] c = ~a;
// ValidationError: Invalid initialization value for variable 'c'
Change Requested
- Give
bit[n]an internal representation that supports the spec's operator set. A
fixed-width integer plus an explicit width is the natural choice; thestrform is then
produced only at serialisation time. - Support
|,&,^,~,<<,>>onbit[n]operands, with the spec rule that both
operands of a binary bitwise operator must have equal width. - Support
b[i](yieldingbit) andb[a:b]/b[a:step:b](yieldingbit[k]). - No operation on a
bit[n]should ever surface a raw Python exception; anything genuinely
invalid must raiseValidationErrorwith the offending span.
Implementation Details
- Bit values are produced by the classical declaration path in
Qasm3Visitor._visit_classical_declaration(src/pyqasm/visitor.py:~1991) and consumed by
Qasm3ExprEvaluatorinsrc/pyqasm/expressions.py. - The binary/unary operator dispatch in
src/pyqasm/maps/expressions.pyapplies Python
operators directly to the stored value. Bit-typed operands need to be converted to their
integer form before the operator is applied, and the result re-widened and masked ton
bits. - Indexing is handled by
Qasm3Analyzer.analyze_classical_indices
(src/pyqasm/analyzer.py); it currently assumes an indexable non-strcontainer, which is
wherestring indices must be integers, not 'tuple'originates. - A shared helper pair —
bits_to_int(value, width)/int_to_bits(value, width)— keeps the
conversion in one place and makes the serialiser's job unambiguous. - Once this lands, the
rotl/rotr/popcountbuilt-ins (separate issue) become
straightforward, since they are defined onbit[n]. - Tests:
tests/qasm3/test_expressions.py— each operator, width-mismatch errors, single-bit
and ranged reads, and a round-trip throughdumps()confirming the"1010"literal form
is preserved.
- Lenguaje dominante
- Python
- Estrellas
- 29
- Forks
- 33
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 15
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de qBraid/pyqasm
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 63/100
Los mantenedores suelen responder en 1 día
-
enhancement error-handling program-validation
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 1 día
-
Move the QASM 2 printer and its branch pre-processing out of qasm2.pyPosiblemente ocupada @danielgaskins la tomó hace 9 días. Abiertoenhancement qasm2
Dificultad 4/5 3-5 días Aptitud para principiantes 22/100
qBraid/pyqasm#447 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
[BUG] dumps() emits extern calls without a semicolon, producing unparsable QASMPosiblemente ocupada @Jah-yee la tomó hace 17 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 25/100
qBraid/pyqasm#438 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Parse failures raise ValidationError with an empty message; the ANTLR diagnostic is lostPosiblemente ocupada @tonycoder-hub la tomó hace 11 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 22/100
qBraid/pyqasm#436 · 1 comentario ·
Los mantenedores suelen responder en 1 día
Todos los issues de qBraid/pyqasm
Issues similares
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
fossasia/visdom-dev#69 ·
Los mantenedores suelen responder en 3 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
Frontend build fails with Frappe version-16: DataImport is no longer exported by @framework/uiAbiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
BUG: PROD: v1.3.0-beta Onboarding Profile Picture Misaligns When Name Validation Error AppearsPosiblemente ocupada @RitikaSen27 la tomó hoy. Abiertobug good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
AOSSIE-Org/PictoPy#1647 · 2 comentarios ·
Los mantenedores suelen responder en 2 días
-
[oblt-aw][autodoc] Update workflow docs for gh-aw v0.92.5 and detector permission scopePosiblemente ocupada @github-actions la tomó hoy. Abierto
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
Los mantenedores suelen responder en 1 día