-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Fixed random_circuit for large circuits #8425
Conversation
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review this:
|
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 getting a start to this, I"m looking forward to having this fixed. Besides the inline comments (the algorithm will need to be updated as it's not producing correct results with this anymore). This should have a release note documenting the fix (and potential change in behavior for a fixed seed) and also some unit tests to validate the fix moving forward.
qiskit/circuit/random/utils.py
Outdated
parts = rng.integers(0, 1<<16, size=4) | ||
shift = 0 | ||
condition_int = 0 | ||
for part in parts: | ||
ipart = (int)(part) | ||
condition_int += ipart << shift | ||
shift += 16 | ||
op.condition = (cr, condition_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.
Shouldn't this be based on the number of qubits? We want to pick a random integer between 0 and 2**num_qubits
as the random condition state. Right now this is hard coded as a random large integer. Also I think this can return negatives which we don't support for the value.
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 the suggestion. I agree that this should be based on the number of qubits, so I did the following:
parts = rng.integers(0, 1<<16, size=int(num_qubits/16))
shift = 0
condition_int = 0
for part in parts:
ipart = int(part)
condition_int += ipart << shift
shift += 16
condition_int = condition_int<<(num_qubits%16)
value_add = rng.integers(0, 1<<(num_qubits%16))
condition_int += int(value_add)
Here, for any number of qubits this code generates a random number with maximum value of 2**num_qubits - 1. For example, if num_qubits is 70, then the loop runs 4 times generating a random number upto 2^64 and then that number is shifted bitwise by 6 bits (70-64) and another random number of 6 bits is added to it. Kindly let me know if this approach is correct.
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.
Hi @mtreinish, just wanted to remind you about the changes I did after your suggestion. Could you please review the code above so that I can commit the changes to the PR if the code is correct?
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.
Sorry for the delay, that approach looks reasonable to me. Please make the changes to your PR branch and probably add some unit tests at the same time to verify that for large circuits we still can set a condition. Then I think with a release not this will be ready to go
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
9b04ab5
to
41f338c
Compare
Summary
This PR resolves the issue #6994 by the following approaches:
Details and comments
The issue gets resolved if we do this in the random_circuit function:
I have done typecasting on parts (numpy int) and created iparts (python int) so that the condition_int value is in python int. This ensures that condition_int has no upper limit like Numpy int.
We can see that for num_qubits > 63 (80 in my case) we get no error, so the issue is fixed