Skip to content

Commit

Permalink
Fix PauliString exponentiation (#2989)
Browse files Browse the repository at this point in the history
Fixes the bug pointed out in #2710 .
  • Loading branch information
kevinsung committed May 11, 2020
1 parent f532a86 commit e5af689
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cirq/ops/pauli_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ def __rpow__(self, base):
'Exponentiated to a non-Hermitian PauliString '
f'<{base}**{self}>. Coefficient must be imaginary.')

half_turns = math.log(base) * (-self.coefficient.imag / math.pi)
half_turns = 2 * math.log(base) * (-self.coefficient.imag / math.pi)

if len(self) == 1:
q, p = next(iter(self.items()))
Expand All @@ -621,8 +621,8 @@ def __rpow__(self, base):
from cirq.ops import pauli_string_phasor
return pauli_string_phasor.PauliStringPhasor(
PauliString(qubit_pauli_map=self._qubit_pauli_map),
exponent_neg=+half_turns / 2,
exponent_pos=-half_turns / 2)
exponent_neg=+half_turns / 4,
exponent_pos=-half_turns / 4)
return NotImplemented

def map_qubits(self, qubit_map: Dict[raw_types.Qid, raw_types.Qid]
Expand Down
13 changes: 9 additions & 4 deletions cirq/ops/pauli_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ def test_exponentiation_as_exponent():
def test_exponentiate_single_value_as_exponent():
q = cirq.LineQubit(0)

assert cirq.approx_eq(math.e**(-0.25j * math.pi * cirq.X(q)),
assert cirq.approx_eq(math.e**(-0.125j * math.pi * cirq.X(q)),
cirq.rx(0.25 * math.pi).on(q))

assert cirq.approx_eq(math.e**(-0.25j * math.pi * cirq.Y(q)),
assert cirq.approx_eq(math.e**(-0.125j * math.pi * cirq.Y(q)),
cirq.ry(0.25 * math.pi).on(q))

assert cirq.approx_eq(math.e**(-0.25j * math.pi * cirq.Z(q)),
assert cirq.approx_eq(math.e**(-0.125j * math.pi * cirq.Z(q)),
cirq.rz(0.25 * math.pi).on(q))

assert cirq.approx_eq(np.exp(-0.3j * math.pi * cirq.X(q)),
assert cirq.approx_eq(np.exp(-0.15j * math.pi * cirq.X(q)),
cirq.rx(0.3 * math.pi).on(q))

assert cirq.approx_eq(cirq.X(q)**0.5, cirq.XPowGate(exponent=0.5).on(q))
Expand Down Expand Up @@ -202,6 +202,11 @@ def test_exponentiation_as_base():
np.exp(0.5j * math.pi * p),
cirq.PauliStringPhasor(p, exponent_neg=-0.25, exponent_pos=0.25))

assert cirq.approx_eq(
cirq.unitary(np.exp(0.5j * math.pi * cirq.Z(a))),
np.diag([np.exp(0.5j * math.pi),
np.exp(-0.5j * math.pi)]))


@pytest.mark.parametrize('pauli', (cirq.X, cirq.Y, cirq.Z))
def test_list_op_constructor_matches_mapping(pauli):
Expand Down

0 comments on commit e5af689

Please sign in to comment.