Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/openfermion/circuits/trotter_exp_to_qgates.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ def pauli_exp_to_qasm(qubit_operator_list, evolution_time=1.0, qubit_list=None,
]
else:
if len(qids) > 0:
ret_list = ret_list + ["Rz {} {}".format(term_coeff * evolution_time, qids[-1])]
ret_list = ret_list + [
"Rz {} {}".format(2 * term_coeff * evolution_time, qids[-1])
]
Comment on lines +258 to +260
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

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 $\exp(-i \theta P)$ controlled on the ancilla, the relative phase on the ancilla should be $e^{-i \theta \lambda}$ (where $\lambda \in {1, -1}$ is the eigenvalue of the target state):

  • 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:

  • Rz on the ancilla has angle $\theta$, giving a base relative phase of $e^{i \theta}$.
  • C-Phase has 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 $\exp(i \theta P)$ instead of $\exp(-i \theta P)$. To fix this, the signs of the angles in the ancilla is not None block should be inverted:

  1. Change the C-Phase angle to 2 * term_coeff * evolution_time.
  2. Change the Rz angle on the ancilla to -1 * term_coeff * evolution_time (both in the if len(qids) > 0 and else branches).

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.


# 4. Second set of CNOTs
ret_list = ret_list + cnots2
Expand Down
17 changes: 11 additions & 6 deletions src/openfermion/circuits/trotter_exp_to_qgates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,16 @@ def test_qasm_string_Z(self):
# Correct string
strcorrect = '''5
CNOT 3 4
Rz 0.6 4
Rz 1.2 4
CNOT 3 4'''

self.assertEqual(qasmstr, strcorrect)

def test_pauli_exp_to_qasm_uses_rz_angle_for_exp_minus_i_theta_z(self):
qasmstr = "\n".join(pauli_exp_to_qasm([QubitOperator('Z0', 0.5)], evolution_time=3))

self.assertEqual(qasmstr, 'Rz 3.0 0')

def test_qasm_string_XYZ(self):
# Testing for correct QASM string output w/ Pauli-{X,Y,Z}
# QubitOperator('X0 Z1 Y3', 0.5)
Expand All @@ -231,7 +236,7 @@ def test_qasm_string_XYZ(self):
Rx 1.5707963267948966 3
CNOT 0 1
CNOT 1 3
Rz 0.5 3
Rz 1.0 3
CNOT 1 3
CNOT 0 1
H 0
Expand Down Expand Up @@ -329,13 +334,13 @@ def test_qasm_string_multiple_operator(self):
# the order in which the operators loop.
strcorrect1 = '''5
CNOT 3 4
Rz 0.6 4
Rz 1.2 4
CNOT 3 4
H 0
Rx 1.5707963267948966 3
CNOT 0 1
CNOT 1 3
Rz 0.5 3
Rz 1.0 3
CNOT 1 3
CNOT 0 1
H 0
Expand All @@ -346,13 +351,13 @@ def test_qasm_string_multiple_operator(self):
Rx 1.5707963267948966 3
CNOT 0 1
CNOT 1 3
Rz 0.5 3
Rz 1.0 3
CNOT 1 3
CNOT 0 1
H 0
Rx -1.5707963267948966 3
CNOT 3 4
Rz 0.6 4
Rz 1.2 4
CNOT 3 4'''
try:
self.assertEqual(qasmstr, strcorrect1)
Expand Down
Loading