Skip to content

update qubit array test #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions oqpy/quantum_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ def to_ast(self, prog: Program) -> ast.Expression:

def make_declaration_statement(self, program: Program) -> ast.Statement:
"""Make an ast statement that declares the OQpy variable."""
if self.size == 0:
raise ValueError("The size of the qubit register cannot be zero.")
decl = ast.QubitDeclaration(
ast.Identifier(self.name),
size=ast.IntegerLiteral(self.size) if self.size else self.size,
size=ast.IntegerLiteral(self.size) if self.size else None,
)
decl.annotations = make_annotations(self.annotations)
return decl
Expand Down Expand Up @@ -85,10 +87,10 @@ def __init__(self, collection: Qubit, index: AstConvertible):
self.collection = collection
self.index = index

def to_ast(self, program: Program) -> ast.IndexExpression:
def to_ast(self, program: Program) -> ast.IndexedIdentifier:
"""Converts this indexed qubit array into an ast node."""
return ast.IndexExpression(
collection=to_ast(program, self.collection), index=[to_ast(program, self.index)]
return ast.IndexedIdentifier(
name=to_ast(program, self.collection), indices=[[to_ast(program, self.index)]]
)


Expand Down
21 changes: 17 additions & 4 deletions tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -2282,21 +2282,34 @@ def test_include():
def test_qubit_array():
prog = oqpy.Program()
q = oqpy.Qubit("q", size=2)
i = IntVar(2, "i")
prog.gate(q[0], "h")
prog.gate([q[0], q[1]], "cnot")
prog.gate([q[oqpy.Range(0, i)]], "cnot")

with pytest.raises(TypeError):
prog.gate([q[0:2]], "cnot")

s = oqpy.Qubit("s")
with pytest.raises(TypeError):
prog.gate(s[0], "h")

expected = textwrap.dedent(
"""
OPENQASM 3.0;
qubit[2] q;
int[32] i = 2;
h q[0];
cnot q[0], q[1];
cnot q[0:i - 1];
"""
).strip()

assert prog.to_qasm() == expected
_check_respects_type_hints(prog)

with pytest.raises(TypeError):
prog = oqpy.Program()
q = oqpy.Qubit("q")
prog.gate(q[0], "h")
prog_with_errors = oqpy.Program()
q0 = oqpy.Qubit("q0", size=0)
prog_with_errors.gate(q0, "h")
with pytest.raises(ValueError):
prog_with_errors.to_qasm()