Skip to content

Commit ecb7cdd

Browse files
update qubit array test (#68)
* update qubit array test * handle size=0 independently
1 parent 862d0c1 commit ecb7cdd

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

oqpy/quantum_types.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ def to_ast(self, prog: Program) -> ast.Expression:
5555

5656
def make_declaration_statement(self, program: Program) -> ast.Statement:
5757
"""Make an ast statement that declares the OQpy variable."""
58+
if self.size == 0:
59+
raise ValueError("The size of the qubit register cannot be zero.")
5860
decl = ast.QubitDeclaration(
5961
ast.Identifier(self.name),
60-
size=ast.IntegerLiteral(self.size) if self.size else self.size,
62+
size=ast.IntegerLiteral(self.size) if self.size else None,
6163
)
6264
decl.annotations = make_annotations(self.annotations)
6365
return decl
@@ -85,10 +87,10 @@ def __init__(self, collection: Qubit, index: AstConvertible):
8587
self.collection = collection
8688
self.index = index
8789

88-
def to_ast(self, program: Program) -> ast.IndexExpression:
90+
def to_ast(self, program: Program) -> ast.IndexedIdentifier:
8991
"""Converts this indexed qubit array into an ast node."""
90-
return ast.IndexExpression(
91-
collection=to_ast(program, self.collection), index=[to_ast(program, self.index)]
92+
return ast.IndexedIdentifier(
93+
name=to_ast(program, self.collection), indices=[[to_ast(program, self.index)]]
9294
)
9395

9496

tests/test_directives.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,21 +2282,34 @@ def test_include():
22822282
def test_qubit_array():
22832283
prog = oqpy.Program()
22842284
q = oqpy.Qubit("q", size=2)
2285+
i = IntVar(2, "i")
22852286
prog.gate(q[0], "h")
22862287
prog.gate([q[0], q[1]], "cnot")
2288+
prog.gate([q[oqpy.Range(0, i)]], "cnot")
2289+
2290+
with pytest.raises(TypeError):
2291+
prog.gate([q[0:2]], "cnot")
2292+
2293+
s = oqpy.Qubit("s")
2294+
with pytest.raises(TypeError):
2295+
prog.gate(s[0], "h")
22872296

22882297
expected = textwrap.dedent(
22892298
"""
22902299
OPENQASM 3.0;
22912300
qubit[2] q;
2301+
int[32] i = 2;
22922302
h q[0];
22932303
cnot q[0], q[1];
2304+
cnot q[0:i - 1];
22942305
"""
22952306
).strip()
22962307

22972308
assert prog.to_qasm() == expected
2309+
_check_respects_type_hints(prog)
22982310

2299-
with pytest.raises(TypeError):
2300-
prog = oqpy.Program()
2301-
q = oqpy.Qubit("q")
2302-
prog.gate(q[0], "h")
2311+
prog_with_errors = oqpy.Program()
2312+
q0 = oqpy.Qubit("q0", size=0)
2313+
prog_with_errors.gate(q0, "h")
2314+
with pytest.raises(ValueError):
2315+
prog_with_errors.to_qasm()

0 commit comments

Comments
 (0)