-
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
Add Uniform State Preparation #12112
Conversation
Just an update I have created a draft pull request here for the original issue #11735 |
@woodsp-ibm I have put this as draft as you mentioned before. But I don't understand the errors in some of these checks. Can you please give some insight regarding this? |
One aspect is the formatting of the code, the style is checked via
The other is a cyclic import - I copied some of the error below. You have add this import
You will need to add unit test(s) too, its good to have already as it will show the code is working as intended. |
Hi @woodsp-ibm, I have done the changes as you suggested and even tested it locally. Please have a look into this and suggest future steps. Thank you! Edit: Sorry Steve, the tests passed locally and I tried fixing some of these ones, but I am unable to understand the Coverage report check which is causing the errors in later tests. |
It seems a test, that is a general test for equivalence, is picking up on your new "Gate" and failing. If you look that test has a exclusion list
Since its failing on num_qubits, I was looking at StatePreparation and how it handles it there. Could this new one be made more compatible with that - ie compute for M the smallest n (where I think n there is synonomous with num_qubits right?) and let one expand things out otherwise by specifying num_qubits explicitly. I get that one can require num_qubits to always be specified - just wondering if it was done more like StatePreparation whether that would be good. |
Thank you, Steve, for your helpful response. So, if the
Then they just get num_qubits through the _get_num_qubits method .
|
If StatePreparation is excluded from that test it made sense to me that your new class probably should be too. Yes StatePreparation has num_qubits as optional and does that computation you refer to. It seemed like this could be done the same if that seems reasonable - it would perhaps make them more consistent in that regard. |
Okay, that makes sense. I will try to do that as well. Thank you so much, Steve, for all the help and support! |
num_qubits (int): The number of qubits used. | ||
""" | ||
super().__init__("USup", num_qubits, []) | ||
assert ( |
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 you look at StatePreparation this type of check raises an error rather than doing and assert. I think assert if not used in Qiskit rather it does some checks and raises and error in generaI. Here I would check and raise a ValueError rather than being an assert - my take is that would be more standard than the QiskitError that StatePreparation uses.
Your unit tests should check these error paths and make sure an error is raised when expected.
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.
You're right, I have replaced assert
with raise ValueError
. I will look into unit tests too. Thanks, again!
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 have added two test cases, hopefully everything is all well now. Thank you, Steve, for everything!
@woodsp-ibm I think the reason why it is still giving the same error in Coverage report test is it might be testing it without the updated |
Its not failing the same way though
this is from your code. In StatePreparation the super.init call is made later once the number of qubits is computed. Maybe compare to that as I think its failing due the super init call now. Most likely you end up passing None now because its optional so it needs to be done once you compute the value like StatePreparation does |
Yes, Steve, the error is different, but only slightly. The fundamental error is the same. I think the error coming from |
params: Union[str, list, int, Statevector], | ||
M : int = 2, | ||
num_qubits: Optional[int] = None, |
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 am not understanding why params is here. I know they are passed to the parent but this is not something that is used here right - your only params are M. In your test, where you are doing Generalized_Uniform_Superposition_Gate(M, n)
the M there goes to params (the first parameter in the list here), n ends up in M (the 2nd position) and num_qubits is left as None.
Hence this call fails in the parent super().__init__("USup", num_qubits, [])
as it passes None (as far as I can see).
My take would be to remove params from here, have M first here, not have a default value so you have to provide a value, and then num_qubits as you have it.
Next move this call super().__init__("USup", num_qubits, [])
to the end of the method and change it to super().__init__("USup", self._num_qubits, [M])
This way you always pass a valid value of num qubits to the parent since you compute it if its None based on M. Then M is made single value in a list passed as parameters to the parent - this is kinda the equivalent of what params is to the StatePreparation. You can see how its handled there.
As to ddt you are not using it here. You can parameterize the test and even create the data so you do not need a for loop rather ddt will create a single test for each data entry there. That is better as all the tests will always run - in the for loop, as you have it, it will stop at the first failure. Usually we like to run all tests and if a test had independent parts like this, but is in one test, it would be done with the self.subTest. This is something that can be sorted once the code is running properly - I think what I am suggesting above will fix things.
incorporated Julien's changes!
Incorporated Julien's optimization suggestion.
implemented both reviewers' suggestions.
I have added almost every suggestion (handled even @1ucian0 's last suggestion), if you get time, please take a look at this. Thanks in advance XD! |
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 taking care of the comments! There's some more steps we can take to clean up the code, then it looks good to go from my side 🙂
releasenotes/notes/uniform-superposition-gate-3bd95ffdf05ef18c.yaml
Outdated
Show resolved
Hide resolved
Hi @Cryoris, I have incorporated all the changes you suggested, and it will pass all the tests now. The inverse function in the latest release of Qiskit, caused one test to fail, but have fixed it now. Can you please look at it and see if there's anything else required? |
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 all the work and incorporating all the changes! This looks great 🙂
Thank you so much to you, Jake, Luciano, and especially Steve, for going through my amateurishly written code and helping bring it to this stage, despite your incredibly busy schedules. Steve, I'm incredibly grateful for the support and assistance you've provided throughout this journey. Without you, it wouldn't have been possible. I'll probably bother you guys again with another pull request 😅, hopefully next time it'll be easier to deal with. |
Hirmay, you are very welcome. Congratulations on your successful contribution to Qiskit. |
Thanks, again, Steve! XD |
* Update state_preparation.py * Update state_preparation.py * Add files via upload * Update __init__.py * Update test_generalized_uniform_superposition_gate.py * Update test_generalized_uniform_superposition_gate.py * Update test_gate_definitions.py * made the format consistent with StatePreparation class * Put description and arguments in init * replaced assert with raise ValueError * small mistake in Returns * added test cases for ValueError cases * Update state_preparation.py * incorporate Steve's helpful suggestions * small bug * test_case fix * function for returning M * oops..forgot "" marks * Update state_preparation.py * removed get methods for num_qubit and M * added power of 2 condition * included test function when num_qubits is None * blacked init * blacked state_prep * blacked test_generalized_uniform_superposition_gate.py * reblacked state_prep * reblacked test_generalized_uniform_superposition_gate.py * shorterned max line length * reblacked state_prep * reblacked state_prep * for pyline * pylinted state_preparation.py * pylinted test_generalized_uniform_superposition_gate.py * pylinted test_gate_definitions.py * pylinted test_generalized_uniform_superposition_gate.py * Added GeneralizedUniformSuperposition gate Added GeneralizedUniformSuperposition gate class to the StatePreparation file in Circuit library. * modified: releasenotes/notes/generalized-uniform-superposition-gate-3bd95ffdf05ef18c.yaml * Updated release notes based on Steve's suggestions * Update release note * implemented the changes * fixed error * Update test_uniform_superposition_gate.py * Update uniform-superposition-gate-3bd95ffdf05ef18c.yaml * Update uniform-superposition-gate-3bd95ffdf05ef18c.yaml * Update qiskit/circuit/library/data_preparation/state_preparation.py Sounds good! Co-authored-by: Julien Gacon <[email protected]> * Update qiskit/circuit/library/data_preparation/state_preparation.py Okay! Co-authored-by: Julien Gacon <[email protected]> * Update qiskit/circuit/library/data_preparation/state_preparation.py oh that's interesting...I didn't know that. Thanks for the info! Co-authored-by: Julien Gacon <[email protected]> * Update releasenotes/notes/uniform-superposition-gate-3bd95ffdf05ef18c.yaml Ahhh...that's nice! Co-authored-by: Julien Gacon <[email protected]> * Update releasenotes/notes/uniform-superposition-gate-3bd95ffdf05ef18c.yaml You're quite right, will help for others who might look at the code in future. Co-authored-by: Luciano Bello <[email protected]> * Update state_preparation.py incorporated Julien's changes! * Update test_uniform_superposition_gate.py Incorporated Julien's optimization suggestion. * Update uniform-superposition-gate-3bd95ffdf05ef18c.yaml implemented both reviewers' suggestions. * Update uniform-superposition-gate-3bd95ffdf05ef18c.yaml removed SV24 * Update state_preparation.py * Update state_preparation.py * Update test_uniform_superposition_gate.py * Update uniform-superposition-gate-3bd95ffdf05ef18c.yaml * Update qiskit/circuit/library/data_preparation/state_preparation.py Co-authored-by: Julien Gacon <[email protected]> * blacked state_preparation.py * Update test_uniform_superposition_gate.py * pylinted state_preparation.py * removed transpile test_uniform_superposition_gate.py --------- Co-authored-by: Julien Gacon <[email protected]> Co-authored-by: Luciano Bello <[email protected]>
Summary
Closes #11735
Details and comments