quantumlib / OpenFermion

The electronic structure package for quantum computers.
Apache License 2.0
1.51k stars 372 forks source link

Trotter evolution time may be off by a factor of 2 #876

Open zmorrell opened 4 months ago

zmorrell commented 4 months ago

Hello,

I was playing with the trotterize_exp_qubop_to_qasm function, and I think it may be outputting incorrect circuits. I have constructed the following minimal broken example to demonstrate.

If we consider the Hamiltonian $H = \sigma^x + \sigma^z$, then the expected time evolution of $H$ is given by

| \psi(t) \rangle = \exp \left( -i H t \right) | \psi(0) \rangle.

To perform this evolution, we can use the first order Trotter formula

\exp \left( A + B \right) = \lim_{n \to \infty} \left( \exp{\left( \frac{A}{n} \right)} \exp{\left(\frac{B}{n} \right )} \right)^n.

If we approximate this series using $n = 1$ (perform the evolution using a single trotter step) and consider an evolution time of $t = 1$, we obtain

|\psi(1) \rangle \approx \exp(-i \sigma^x)  \exp(-i \sigma^z) |\psi(0) \rangle

According to the documentation for Cirq, the definition for cirq.Rx is Rx(rads) = exp(-i X rads / 2) and the definition for cirq.Rz is Rz(rads) = exp(-i Z rads / 2). This means that the qasm circuit for the above described problem would be the following (note that I am using the identity that Rx(rads) = H Rz(rads) H to be consistent with the output of trotterize_exp_qubop_to_qasm)

H 0
Rz 2.0 0
H 0
Rz 2.0 0

However, when I implement the circuit in openfermion using the following code:

import openfermion as of

H = of.QubitOperator('X0') + of.QubitOperator('Z0')
trotter_generator = of.trotterize_exp_qubop_to_qasm(H, evolution_time = 1)
[print(op) for op in trotter_generator]

the output is the following:

H 0
Rz 1.0 0
H 0
Rz 1.0 0

which implies that the rotation angles are half of what they should be. I believe that the bug arises from src/openfermion/circuits/trotter_exp_to_qgates.py in the function pauli_exp_to_qasm at lines 250 and 254. In these lines, the factor of 2 is not multiplied in and seems to cause the issue.

I am not sure whether there are more instances of this potential mistake in the code base. Hopefully my logic is sound, and I am not just missing something.

Thank you.

fdmalone commented 3 months ago

This certainly looks like an error or should at least be clarified.

fdmalone commented 3 months ago

This looks like a duplicate of #721, which should definitely be fixed.