Fix QASM Pauli exponential Rz angle#1343
Conversation
Use the doubled Rz angle needed to implement exp(-i theta Z) in the uncontrolled Pauli exponential path, and update QASM regression expectations accordingly. Co-authored-by: OpenAI Codex <codex@openai.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request doubles the Rz rotation angle in pauli_exp_to_qasm for uncontrolled exponentiation to correct the rotation angle, updating the corresponding test cases and adding a new test. However, the reviewer identified that the controlled version of the exponentiation (when ancilla is not None) still contains a sign inversion bug, implementing exp(i * theta * P) instead of exp(-i * theta * P). To resolve this, the signs of the angles in the controlled block should be inverted, and the corresponding tests should be updated.
| ret_list = ret_list + [ | ||
| "Rz {} {}".format(2 * term_coeff * evolution_time, qids[-1]) | ||
| ] |
There was a problem hiding this comment.
While doubling the uncontrolled Rz angle correctly fixes the rotation for the uncontrolled case, the controlled version of the exponentiation (when ancilla is not None on lines 242-255) still contains a sign inversion bug.
Specifically, to implement
- For
$\lambda = 1$ , we need a relative phase of$e^{-i \theta}$ . - For
$\lambda = -1$ , we need a relative phase of$e^{i \theta}$ .
With the current implementation:
-
Rzon the ancilla has angle$\theta$ , giving a base relative phase of$e^{i \theta}$ . -
C-Phasehas angle$-2\theta$ , giving an additional phase of$e^{-2i \theta}$ when the target is$|1\rangle$ ($\lambda = -1$ ).
This results in: - For
$\lambda = 1$ : relative phase is$e^{i \theta}$ (should be$e^{-i \theta}$ ). - For
$\lambda = -1$ : relative phase is$e^{-i \theta}$ (should be$e^{i \theta}$ ).
This means the controlled version implements ancilla is not None block should be inverted:
- Change the
C-Phaseangle to2 * term_coeff * evolution_time. - Change the
Rzangle on the ancilla to-1 * term_coeff * evolution_time(both in theif len(qids) > 0andelsebranches).
Note that you will also need to update the corresponding tests in trotter_exp_to_qgates_test.py (such as test_qasm_string_Controlled_XYZ and test_qasm_string_Controlled_XYZ_ancilla_list) to reflect the corrected signs.
Fixes #721.
Summary
Rzangle emitted bypauli_exp_to_qasmso it implementsexp(-i * theta * Z)rather thanexp(-i * theta * Z / 2)evolution_timeValidation
C:\hades\oss\.venvs\openfermion-1317\Scripts\python.exe -m pytest src\openfermion\circuits\trotter_exp_to_qgates_test.py -qC:\hades\oss\.venvs\openfermion-1317\Scripts\python.exe -m black --check src\openfermion\circuits\trotter_exp_to_qgates.py src\openfermion\circuits\trotter_exp_to_qgates_test.pyC:\hades\oss\.venvs\openfermion-1317\Scripts\python.exe -m compileall -q src\openfermion\circuits\trotter_exp_to_qgates.py src\openfermion\circuits\trotter_exp_to_qgates_test.pygit diff --checkAI-assisted contribution: implemented with OpenAI Codex and reviewed locally before submission.