-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Adds various convenience functions to qiskit #12470
base: main
Are you sure you want to change the base?
Conversation
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 9384252860Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
--- | ||
features: | ||
- | | ||
Added convenience functions to the :class:`.QuantumCircuit` class that return the depth in terms of |
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 proposed new features are methods, not functions.
qiskit/circuit/quantumcircuit.py
Outdated
def depth_nq(self, n: int, include_directives: bool = False) -> int: | ||
"""Returns the depth in terms of quantum gates with at least n-qubits, |
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 key phrase seems to be at least. And for this reason, depth_nq
with n=2
is not the same thing as depth_2q
. This is potentially confusing.
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.
why the function depth_nq
is with >=n
and num_nq_gates
is with ==n
?
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.
Maybe have just this one with default n = 2
and drop depth_2q
Thanks for spotting these @garrison - I addressed your comments in the recent commit. |
releasenotes/notes/adds-convenience-functions-f14907a13fcb7929.yaml
Outdated
Show resolved
Hide resolved
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.
I think these functions are generally useful, but do you see any specific use-cases? especially for the n-qubit functions.
For example, the function of depth_2q
has already been used in several tests, including:
test/python/synthesis/test_cx_cz_synthesis.py:
self.assertTrue(depth2q <= 5 * num_qubits)
test/python/synthesis/test_clifford_decompose_layers.py:
self.assertTrue(depth2q <= 7 * num_qubits + 2)
test/python/synthesis/test_cz_synthesis.py:
self.assertTrue(depth2q == 2 * num_qubits + 2)
test/python/synthesis/test_stabilizer_synthesis.py:
self.assertTrue(depth2q == 2 * num_qubits + 2)
qiskit/circuit/quantumcircuit.py
Outdated
def depth_nq(self, n: int, include_directives: bool = False) -> int: | ||
"""Returns the depth in terms of quantum gates with at least n-qubits, |
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.
why the function depth_nq
is with >=n
and num_nq_gates
is with ==n
?
qiskit/circuit/quantumcircuit.py
Outdated
@@ -3386,6 +3386,91 @@ def depth( | |||
|
|||
return max(op_stack) | |||
|
|||
def depth_2q(self, include_directives: bool = False) -> int: | |||
"""Returns the depth in terms of quantum gates with at least 2-qubits, |
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.
why the function depth_2q
is with >=2
and num_2q_gates
is with ==2
?
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.
num_nq_gates
exactly checks the number of qubits because I thought users might be interested in that quantity in general. Also, num_nonlocal_gates
determines the number of gates with at least two qubits. I extended the arguments of num_nonlocal_gates
to include an integer n
that specifies the minimum number of qubits in a gate to still be counted towards the quantity returned by num_nonlocal_gates
. The default behavior of num_nonlocal_gates
stays the same, so I hope our stability policy is not violated...
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.
Agree the name is a bit confusing an inconsistent with the others. Consider changing to something like depth_multi_qubit
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.
I was also thinking about whether depth_nq
should work on gates with exactly n
qubits or for >= n
qubits. I decided for the latter because I thought the reason an user might want depth_nq
to exclude certain gates is that they are comparatively cheap. I think it is safe to assume that if an user regards a gate with n
qubits to be relatively expensive, they would also consider a gate with n+i
qubits to be expensive (if there are no other properties to decide on). I also was not able to see a situation where an user might only be interested in the two-qubit gate depth while ignoring all other gates in the presence of e.g. three-qubit gates.
….yaml Co-authored-by: Jim Garrison <[email protected]>
Thanks for your comments, I addressed them in the most recent commits! :-)
I think these functions will be useful to any user that is interested in building their own circuits or looking into the characteristics of these circuits. Regarding the On the other hand, the two-qubit versions are going to be searched for first by users that only worked with two-qubit gates yet. |
qiskit/circuit/quantumcircuit.py
Outdated
else: | ||
return self.size(lambda x: x.qubits == 2) | ||
|
||
def num_nq_gates(self, n: int, include_directives: bool = False) -> int: |
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.
Same suggestion: have n:int = 2
and drop the num_2q_gates
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.
Thanks for your suggestion. I removed the n=2
versions of the methods and updated the release notes accordingly!
Co-authored-by: Eli Arbel <[email protected]>
…qiskit into convenience-functions
I think there was a meeting where these were discussed that I wasn't in, so apologies if I'm repeating something: Did we consider the alternative of doing all these by "examples" documentation within the relevant functions or discussion of the circuit data model? The depth ones in particular feel like they could have been examples, and the others are relatively simple filters on an explicitly public data field - I'm concerned that adding more methods makes it harder to find everything on |
That's a good option and I think we should include the added procedures in the documentation at some point. However, it turns out many users need these functions and defining them repeatedly seems bothersome. There likely are also users that don't want to learn Qiskit but still want to use it in order to get accurate information on a circuit they were cooking up somehow. To make it worse, the definition of qiskit/qiskit/circuit/quantumcircuit.py Line 3333 in bac400d
|
Generally I agree with Jake that we've been trying to avoid adding methods that don't have an explicit use-case to not clutter the circuit (which already has loads of methods) with more. Since most functionality here is already in place, a compromise could be to extend docstrings or functions of the existing methods.
|
This is the only one that I think (IMO) may be a good candidate for its own new method. And I think it'd be more useful as a |
I'm fine with having the functionality provided by these convenience procedures as a parameter to the default
|
Summary
This PR adds some convenience functions to Qiskit's
QuantumCircuit
class. The functionality was previously available but required users to dive into the API before determining the wanted quantities. The new 'depth' and 'number of gates' methods were previously available by defining a custom filter function fordepth
andsize
methods ofQuantumCircuit
but I thought it might be worthwhile for new users to have a convenience function that defines such a filter function for them (and saves them e.g. from accidentally counting barriers towards their metrics).