Skip to content

Qubit and BitVar supports get item #67

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

Conversation

yitchen-tim
Copy link
Contributor

@yitchen-tim yitchen-tim commented Aug 24, 2023

Closes #8 and #62

Description of change:

  • Move OQIndexExpression to oqpy.base
  • Support indexing in Qubit class. __getitem__ returns an OQIndexExpression
  • Remove QubitArray class
  • Support variable indexing in BitVar (the changes in this PR include the ones in Support BitVar indexing by IntVar #63)

@yitchen-tim yitchen-tim marked this pull request as ready for review August 24, 2023 19:17
@yitchen-tim
Copy link
Contributor Author

@PhilReinhold This PR closes issue #8 and #62. Could you review and provide feedback on the implementation? Appreciated!

@yitchen-tim yitchen-tim changed the title Qubit class supports get item Qubit and BitVar supports get item Aug 30, 2023
Copy link
Collaborator

@PhilReinhold PhilReinhold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One request, but otherwise looks good. Thanks for the contribution!

def __getitem__(self, index: AstConvertible) -> OQIndexExpression:
if self.size is None:
raise TypeError(f"'{self.name}' is not subscriptable")
return OQIndexExpression(collection=self, index=index, type=ast.Identifier)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think returning an OQIndexExpression here is not quite correct, since OQIndexExpression is a subtype of OQPyExpression which is only for classical typed expressions. I think here you could either make a new type (e.g. IndexedQubitArray) or just return the ast.IndexedExpresssion directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the feedback, I updated and added a IndexedQubitArray class

# Todo (#51): support QubitArray
class QubitArray:
"""Represents an array of qubits."""
class IndexedQubitArray:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason for not being a subclass of OQPyExpression: There is no ast type for qubits
Reason for not being a subclass of Var: It cannot not be declared

Copy link
Collaborator

@PhilReinhold PhilReinhold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks for the changes, LGTM

@PhilReinhold PhilReinhold merged commit 862d0c1 into openqasm:main Sep 7, 2023
Copy link
Collaborator

@jcjaskula-aws jcjaskula-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm late but we should add this fix later

decl = ast.QubitDeclaration(ast.Identifier(self.name), size=None)
decl = ast.QubitDeclaration(
ast.Identifier(self.name),
size=ast.IntegerLiteral(self.size) if self.size else self.size,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size=ast.IntegerLiteral(self.size) if self.size else None,

is better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support QubitArray
3 participants