Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Make ListOp.num_qubits check all ops for same num_qubits #1189

Merged
merged 4 commits into from
Aug 21, 2020

Conversation

jlapeyre
Copy link
Contributor

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 when num_qubits is accessed. This will cause some nonsensical aggregate constructions involving ListOp to raise an error, rather than succeed. For example X @ ListOp([X, Y^Z]), will now raise a ValueError, 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 of ListoP.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.

Comment on lines 454 to 464
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
Copy link
Contributor

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?

Copy link
Contributor Author

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):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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:]):

Copy link
Contributor Author

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.

Copy link
Member

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.

@jlapeyre jlapeyre force-pushed the listop-check-num-qubits branch 2 times, most recently from d84b0e5 to e4354b9 Compare August 13, 2020 17:19
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
@jlapeyre jlapeyre force-pushed the listop-check-num-qubits branch from e4354b9 to fcaf075 Compare August 13, 2020 19:19
@woodsp-ibm
Copy link
Member

Is this ready to go? Still marked [WIP]

@jlapeyre
Copy link
Contributor Author

  • I keep forgetting to remove the WIP
  • If this finally looks like a good solution, then I still need to add a release note. Then I'll remove the WIP.

@woodsp-ibm
Copy link
Member

Can we move forwards on this. It seems fine to me.

@jlapeyre
Copy link
Contributor Author

Yes. I let this slip.
I'll remove WIP and push the release note.

@jlapeyre jlapeyre force-pushed the listop-check-num-qubits branch from d1f2125 to a3e829a Compare August 20, 2020 19:32
@jlapeyre jlapeyre changed the title [WIP] Make ListOp.num_qubits check all ops for same num_qubits Make ListOp.num_qubits check all ops for same num_qubits Aug 20, 2020
@woodsp-ibm woodsp-ibm merged commit 2316733 into qiskit-community:master Aug 21, 2020
@woodsp-ibm woodsp-ibm added this to the 0.8 milestone Sep 2, 2020
Cryoris pushed a commit to Cryoris/qiskit-aqua that referenced this pull request Sep 3, 2020
…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>
pbark pushed a commit to pbark/qiskit-aqua that referenced this pull request Sep 16, 2020
…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>
mtreinish pushed a commit to mtreinish/qiskit-core that referenced this pull request Nov 20, 2020
…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>
manoelmarques added a commit to manoelmarques/qiskit-terra that referenced this pull request Dec 2, 2020
…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>
manoelmarques added a commit to manoelmarques/qiskit-terra that referenced this pull request Dec 7, 2020
…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>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ListOp: number of qubits
4 participants