Skip to content
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

V4 Setters for the Gate class #1535

Merged
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
11 changes: 6 additions & 5 deletions pyquil/quilatom.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,16 @@ def __init__(self, op1: ExpressionDesignator, op2: ExpressionDesignator):
def _from_rs_infix_expression(cls, infix_expression: quil_rs.InfixExpression):
left = _convert_to_py_parameter(infix_expression.left)
right = _convert_to_py_parameter(infix_expression.right)
if infix_expression.operator.is_plus():
print(infix_expression.operator)
if infix_expression.operator == quil_rs.InfixOperator.Plus:
return Add(left, right)
if infix_expression.operator.is_minus():
if infix_expression.operator == quil_rs.InfixOperator.Minus:
return Sub(left, right)
if infix_expression.operator.is_slash():
if infix_expression.operator == quil_rs.InfixOperator.Slash:
return Div(left, right)
if infix_expression.operator.is_star():
if infix_expression.operator == quil_rs.InfixOperator.Star:
return Mul(left, right)
if infix_expression.operator.is_caret():
if infix_expression.operator == quil_rs.InfixOperator.Caret:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just a quick fix unrelated to the other changes. We changed how the InfixOperator enum was exported from the quil library and these needed to be updated to match.

return Pow(left, right)
raise ValueError(f"{type(infix_expression)} is not a valid InfixExpression")

Expand Down
8 changes: 8 additions & 0 deletions pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,18 @@ def get_qubits(self, indices: bool = True) -> Set[QubitDesignator]:
def qubits(self):
return list(self.get_qubits(indices=False))

@qubits.setter
def qubits(self, qubits: Iterable[Union[Qubit, QubitPlaceholder, FormalArgument]]):
quil_rs.Gate.qubits.__set__(self, _convert_to_rs_qubits(qubits))
Copy link
Contributor Author

@MarquessV MarquessV Mar 8, 2023

Choose a reason for hiding this comment

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

This syntax may look weird, but it's necessary. If you are like me, you might think that something like this would work:

super().qubits = ...

But it doesn't! Turns out that syntax is only good for getting attributes, not setting them. This workaround was shamelessly stolen from this decade old issue on the topic of setting superclass attributes via super().


@property
def params(self):
return _convert_to_py_parameters(super().parameters)

@params.setter
def params(self, params: Iterable[ParameterDesignator]):
quil_rs.Gate.parameters.__set__(self, _convert_to_rs_expressions(params))

def get_qubit_indices(self) -> Set[int]:
return {qubit.as_fixed() for qubit in super().qubits}

Expand Down
4 changes: 4 additions & 0 deletions test/unit/test_quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ def test_name(self, gate, name):

def test_params(self, gate, params):
assert gate.params == params
gate.params = [pi / 2]
assert gate.params == [pi / 2]

def test_qubits(self, gate, qubits):
assert gate.qubits == qubits
gate.qubits = [Qubit(123)]
assert gate.qubits == [Qubit(123)]

def test_get_qubits(self, gate, qubits):
assert gate.get_qubit_indices() == {q.index for q in qubits}
Expand Down