Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

akshayp2003
Copy link

Summary

This PR resolves the issue #6994 by the following approaches:

  • Generating 4 random numbers using numpy rng and using them to form a random number of any number of bits as we want
  • Using python int instead of numpy int so that there is no integer overflow due to the upper limit in numpy int

Details and comments

The issue gets resolved if we do this in the random_circuit function:

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)

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

image

@akshayp2003 akshayp2003 requested a review from a team as a code owner July 29, 2022 21:03
@qiskit-bot
Copy link
Collaborator

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:

  • @Qiskit/terra-core

@CLAassistant
Copy link

CLAassistant commented Jul 29, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@mtreinish mtreinish left a 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.

Comment on lines 149 to 156
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)
Copy link
Member

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.

Copy link
Author

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.

Copy link
Author

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?

Copy link
Member

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

qiskit/circuit/random/utils.py Outdated Show resolved Hide resolved
akshayp2003 and others added 2 commits August 11, 2022 01:39
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@HuangJunye HuangJunye added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Aug 16, 2022
@mergify mergify bot closed this in #8983 Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PR PRs from contributors that are not 'members' of the Qiskit repo
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants