-
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
Update rg_qft_multiplier.py
for Allowance of Float Exponentiation
#10983
base: main
Are you sure you want to change the base?
Conversation
There exists trouble when processing scientific computing like exponentiation with values deriving from `numpy`, which are actually reasonable inputs of this API. In that case, the program terminates unexpectedly, leaving a warning that `Integers to negative integer powers are not allowed`. To cope with it, I modify the `int` type `2` to the `float` type `2.0`. Then, this API can run in a normal manner, which seems to accept inputs in the `numpy` form, like a variable `np.int64(2)`.
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:
|
rg_qft_multiplier.py
for the Allowance of Float Exponentiation
rg_qft_multiplier.py
for the Allowance of Float Exponentiationrg_qft_multiplier.py
for Allowance of Float Exponentiation
Thanks for your further advice @Cryoris . It is exciting and amazing for a test researcher to identify this kind of latent bug I have never noticed. As an initial attempt, I've referred to the source codes and discovered in surprise that there is no logical condition to distinguish the consequence whether for j in range(1, num_state_qubits + 1):
for i in range(1, num_state_qubits + 1):
for k in range(1, self.num_result_qubits):
lam = (2 * np.pi) / (2.0 ** (i + j + k - 2 * num_state_qubits))
circuit.append(
PhaseGate(lam).control(2),
[qr_a[num_state_qubits - j], qr_b[num_state_qubits - i], qr_out[k - 1]],
) Based on the reference provided, I have come to understand the function that the codes are designed to achieve. Thus, with the help of differential testing, I have added an for j in range(1, num_state_qubits + 1):
for i in range(1, num_state_qubits + 1):
for k in range(1, self.num_result_qubits):
if i + j + k - 2 * num_state_qubits > 0: # added sentence
lam = (2 * np.pi) / (2.0 ** (i + j + k - 2 * num_state_qubits))
circuit.append(
PhaseGate(lam).control(2),
[qr_a[num_state_qubits - j], qr_b[num_state_qubits - i], qr_out[k - 1]],
) For the testing phase, I have tried to utilize swap test to compare the final states from the above two versions. If all the measurement results from swap test are |
Thanks for pursuing this bug. This PR will also need
In addition to what you and @Cryoris investigate in the comments above, there is the following. |
Well, I am glad to wait for your conclusion @jlapeyre. In fact, I think testing quantum programs varies from testing classical programs, especially for the quantum circuits by unit testing. Apart from paying attention to possible different types of bit inputs, we should take the appropriate initial quantum states into account. However, simple test cases input through the ports of API may not include or cover the input Hilbert space of quantum states. The way I try to test with various quantum states is to construct another quantum circuit to prepare these states and then append the circuit to be tested. It seems to be indirect and the test oracle (direct measurement for immense times or measure for fewer times by swap test) is worth to be clearified 🤔. |
As I have created an open bug report, there exists accidental trouble when processing scientific computing like exponentiation with values deriving from$n=2$ and $m=2$ , nothing unexpected happened. By contrast, if the variables $n$ and $m$ were generated from sentences like
numpy
. Actually, they are reasonable candidates as the inputs of this API, regardless of the application ofnumpy
. For an occurrence, when I appliedRGQFTMultiplier(n, m)
withnp.arange(1,5)
andnp.arange(n, 2 * n + 1)
, the program would terminate out of my expectation, leaving a warning thatIntegers to negative integer powers are not allowed
.Summary
To cope with it, I modify the
int
type2
to thefloat
type2.0
. Then, this API can run in a normal manner, which seems to accept inputs in thenumpy
form, likenp.int64(2)
.Comment
In my opinion, this kind of bug may not merely occur in this API, and quantum program developpers may trigger the same warning as accidentally using
numpy
package, where it is not attributed to their mistakes. It is still meaningful to seek out the latent bugs in other APIs uponqiskit
, because compared to other programs, quantum programs require quite a large scale of algebraic operations based onPython
language.