-
Notifications
You must be signed in to change notification settings - Fork 376
Make ListOp.num_qubits check all ops for same num_qubits #1189
Make ListOp.num_qubits check all ops for same num_qubits #1189
Conversation
op = ListOp([X ^ Y, Y ^ Z]) | ||
self.assertEqual(op.num_qubits, 2) | ||
op = ListOp([X ^ Y, Y]) | ||
|
||
with self.assertRaises(ValueError): | ||
op.num_qubits | ||
|
||
with self.assertRaises(ValueError): | ||
X @ op |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you wrap these in sub-tests, one for the ListOp with equal number of qubits and one for the other one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(somehow missed this comment earlier!) Yes.
It might be a good idea to test that op @ X
(as opposed to X @ op
) does not raise an error.
@@ -129,7 +129,10 @@ def primitive_strings(self) -> Set[str]: | |||
|
|||
@property | |||
def num_qubits(self) -> int: | |||
return self.oplist[0].num_qubits | |||
num_qubits0 = self.oplist[0].num_qubits | |||
if not all(num_qubits0 == op.num_qubits for op in self.oplist): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not all(num_qubits0 == op.num_qubits for op in self.oplist): | |
if not all(num_qubits0 == op.num_qubits for op in self.oplist[1:]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.oplist[1:]
is likely less efficient than self.oplist
because the former allocates and makes a copy of the list, whereas the latter adds one dereference and integer comparison... On the other hand, efficiency is not a design criterion for aqua, and using the slice signals algorithmic intent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that it ends up checking itself is fine - what we care about is that they are all the same, whichever one we pick as some reference.
d84b0e5
to
e4354b9
Compare
Previously, the number of qubits in the first operator in the ListOp was returned. With this change, an additional check is made that all other operators also have the same number of qubits. Closes qiskit-community#1086
e4354b9
to
fcaf075
Compare
Is this ready to go? Still marked [WIP] |
|
Can we move forwards on this. It seems fine to me. |
Yes. I let this slip. |
d1f2125
to
a3e829a
Compare
…unity#1189) * Make ListOp.num_qubits check all ops for same num_qubits Previously, the number of qubits in the first operator in the ListOp was returned. With this change, an additional check is made that all other operators also have the same number of qubits. Closes qiskit-community#1086 * Add release note for listop-check-num-qubits Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
…unity#1189) * Make ListOp.num_qubits check all ops for same num_qubits Previously, the number of qubits in the first operator in the ListOp was returned. With this change, an additional check is made that all other operators also have the same number of qubits. Closes qiskit-community#1086 * Add release note for listop-check-num-qubits Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
…unity/qiskit-aqua#1189) * Make ListOp.num_qubits check all ops for same num_qubits Previously, the number of qubits in the first operator in the ListOp was returned. With this change, an additional check is made that all other operators also have the same number of qubits. Closes qiskit-community/qiskit-aqua#1086 * Add release note for listop-check-num-qubits Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
…unity/qiskit-aqua#1189) * Make ListOp.num_qubits check all ops for same num_qubits Previously, the number of qubits in the first operator in the ListOp was returned. With this change, an additional check is made that all other operators also have the same number of qubits. Closes qiskit-community/qiskit-aqua#1086 * Add release note for listop-check-num-qubits Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
…unity/qiskit-aqua#1189) * Make ListOp.num_qubits check all ops for same num_qubits Previously, the number of qubits in the first operator in the ListOp was returned. With this change, an additional check is made that all other operators also have the same number of qubits. Closes qiskit-community/qiskit-aqua#1086 * Add release note for listop-check-num-qubits Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
Previously, the number of qubits in the first operator in the ListOp
was returned. With this change, an additional check is made that all
other operators also have the same number of qubits.
To be clear: This check that the number of qubits is the same for each operator does not occur on construction of the
ListOp
. Rather, it occurs whennum_qubits
is accessed. This will cause some nonsensical aggregate constructions involvingListOp
to raise an error, rather than succeed. For exampleX @ ListOp([X, Y^Z])
, will now raise aValueError
, rather than succeeding as before. However,ListOp([X, Y^Z]) @ X
will still succeed. This should perhaps be changed, but is beyond the scope of this PR, which is the behavior ofListoP.num_qubits
.There is an alternative PR #1118 open for the same issue. However the present PR is probably the preferred solution.
Closes #1086
X I have added the tests to cover my changes.
✅ I have updated the documentation accordingly.
✅ I have read the CONTRIBUTING document.
There is an alternative PR open for the same issue: #1118 . However the present PR is probably the preferred solution.