Skip to content

Fix confusion between stdlib random and np.random#4645

Merged
abbycross merged 4 commits intoQiskit:mainfrom
jlapeyre:random-stdlib-vs-numpy
Feb 9, 2026
Merged

Fix confusion between stdlib random and np.random#4645
abbycross merged 4 commits intoQiskit:mainfrom
jlapeyre:random-stdlib-vs-numpy

Conversation

@jlapeyre
Copy link
Collaborator

@jlapeyre jlapeyre commented Feb 9, 2026

The existing code that generates

https://quantum.cloud.ibm.com/learning/en/courses/basics-of-quantum-information/entanglement-in-action/qiskit-implementation

imports both pi and random from numpy. The notebook uses nothing else from numpy. These two objects are used only in a scalar context. In some cases, using numpy can degrade performance. In qiskit there has been an effort, reflected in PRs, to avoid using numpy habitually, when not necessary. This is done even in non-performance critical code.

This PR changes the code to import pi from math. And it imports random from the stdlib. When producing single samples, standard library random.randint is about 5 times faster than numpy.random.randint.

Furthermore, the existing code calls numpy's random.randint(0, 1), which always returns zero. This was mistakenly introduced. Since this PR uses randint from the standard library, the range is 0 to 1, inclusive. So it may return either 0 or 1. My best understanding is that this is the original intent of the code.

The following issues and PRs are on exactly the same code touched here

Closes #4643

The existing code imports both `pi` and `random` from numpy.  The notebook uses
nothing else from numpy. These two objects are used only in a scalar context.
In some cases, using numpy can degrade performance. In qiskit there has been an
effort, reflected in PRs, to avoid using numpy habitually when not
necessary. This is done even in non-performance critical code.

This PR changes the code to import `pi` from `math`. And it imports `random`
from the stdlib.  When producing single samples, standard library
`random.randint` is about 5 times faster than `numpy.random.randint`.

The existing code calls numpy's `random.randint(0, 1)`, which always returns
zero. This was mistakenly introduced. Since this PR uses `randint` from the
standard library, the range is 0 to 1, inclusive.  So it may return either 0 or
1. My best understanding is that this is the original intent of the code.
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@qiskit-bot
Copy link
Contributor

One or more of the following people are relevant to this code:

@jlapeyre a linting error flagged a json-breaking change. Does this fix preserve your intent?
"from qiskit.result import marginal_distribution\n",
"from qiskit.circuit.library import UGate\n",
"from numpy import pi, random"
"from math import pi, random"
Copy link
Collaborator

Choose a reason for hiding this comment

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

@jlapeyre the json was invalid - this fix now passes the linter but pls let me know if it doesn't match what is needed here

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've now put import random on its own line in the notebook (vs. directly in the json), and it healed the lint problem - that looks more like what you wanted to accomplish

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@abbycross Thankyou. This is correct. Next time, I'll run jq.

Copy link
Collaborator

@christopherporter1 christopherporter1 left a comment

Choose a reason for hiding this comment

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

Thank you, John!

@abbycross abbycross added this pull request to the merge queue Feb 9, 2026
Merged via the queue into Qiskit:main with commit a23947e Feb 9, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

random is imported from an inconsistent source

4 participants