-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Can not bind parameter a circuit if a circuit is built by multiprocesses #2429
Comments
Hi @chunfuchen , thanks for reporting! Your description of the problem is correct, |
Parameters were defined such that they would only ever __eq__ self. That way, if a user defined a Parameter('theta') and wanted to compose in a subcircuit defined elsewhere which contained a different Parameter('theta'), we would be able to detect that the parameter names overlapped and raise an error. However, if a user sent a Parameter to a different python instance though (say through multiprocessing.Pool or qiskit.tools.parallel_map), it would be instantiated as a different python object and so no longer be considered equal to the original parameter. This PR changes Parameter to instead generate a random UUID on instantiation and use that for equality testing. Unlike id(self), self._uuid will be preserved across pickling and de-pickling, but should otherwise preserve Parameter's equality behavior. Fixes #2429 Fixes #2864
…t#2947) Parameters were defined such that they would only ever __eq__ self. That way, if a user defined a Parameter('theta') and wanted to compose in a subcircuit defined elsewhere which contained a different Parameter('theta'), we would be able to detect that the parameter names overlapped and raise an error. However, if a user sent a Parameter to a different python instance though (say through multiprocessing.Pool or qiskit.tools.parallel_map), it would be instantiated as a different python object and so no longer be considered equal to the original parameter. This PR changes Parameter to instead generate a random UUID on instantiation and use that for equality testing. Unlike id(self), self._uuid will be preserved across pickling and de-pickling, but should otherwise preserve Parameter's equality behavior. Fixes Qiskit#2429 Fixes Qiskit#2864
This created another issue.
Thus when I am trying to bind parameters, it fails.
|
This is a very old issue, and the new report isn't caused by this. In this case, this is a deliberate design choice, as mentioned above. Parameters are not meant to be equal if they have the same name, in part to prevent accidental issues when multiple circuits happen to use parameters with the same name. You should use the same parameter instance: alpha = Parameter("alpha")
qc.qc(alpha, 0)
qc.bind_parameters({alpha: np.pi/2}) rather than constructing it new each time. You can also retrieve the parameter instances from a circuit object |
Hello I am facing a similar problem. After using |
|
Information
What is the current behavior?
Can not bind parameter a circuit if a circuit is built by multi-processes, an error is raised.
Steps to reproduce the problem
scripts
What is the expected behavior?
Bind parameters without error.
Suggested solutions
I think it fails to bind the parameters because the identity (get through
id()
method) of a parameter is changed during the parallelization (the parameter is pickled to another process); even though the parameter names are the same.Thus, I think another approach to recognize a parameter in a circuit is need to resolve this issue.
The text was updated successfully, but these errors were encountered: