quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.24k stars 1.01k forks source link

`cleanup_operations` replaces CZ with CZ**-1 #6428

Closed NoureldinYosri closed 7 months ago

NoureldinYosri commented 8 months ago

Description of the issue cleanup_operations replaced CZ with CZ-1 where that was unneccassry. in this case the result is still correct since CZ==CZ-1. but it's worth invisigating whether this a side effec of CZ==CZ**-1 or a symptom of a bug.

Digging deeper into the method the change happens when it calls cirq.transformers.eject_phased_paulis

How to reproduce the issue

The input (a CNOT written in CZ gateset)

c = cirq.Circuit(
         (cirq.Z**0.75).on(cirq.LineQubit(0)) ,
         (cirq.X**-0.25).on(cirq.LineQubit(1)) ,
         (cirq.X**0.5000000000000001).on(cirq.LineQubit(0)) ,
         (cirq.Y**-0.5).on(cirq.LineQubit(1)) ,
         (cirq.S**-1).on(cirq.LineQubit(0)) ,
         (cirq.Y**-0.5).on(cirq.LineQubit(0)) ,
         cirq.CZ(cirq.LineQubit(0), cirq.LineQubit(1)) ,
         (cirq.S**-1).on(cirq.LineQubit(0)) ,
         (cirq.S**-1).on(cirq.LineQubit(1)) ,
         (cirq.Y**0.5).on(cirq.LineQubit(0)) ,
         (cirq.Y**0.5).on(cirq.LineQubit(1)) ,
         (cirq.Y**0.5).on(cirq.LineQubit(0)) ,
         (cirq.X**-0.24999999999999997).on(cirq.LineQubit(1)) ,
         (cirq.Z**-0.75).on(cirq.LineQubit(0)) ,
)
print(cirq.transformers.analytical_decompositions.two_qubit_to_cz.cleanup_operations(c))

result

[cirq.PhasedXPowGate(phase_exponent=-0.5000000000000002, exponent=0.5).on(cirq.LineQubit(1)), (cirq.CZ**-1.0).on(cirq.LineQubit(0), cirq.LineQubit(1)), cirq.PhasedXPowGate(phase_exponent=0.4999999999999998, exponent=0.5).on(cirq.LineQubit(1))]

related to https://github.com/quantumlib/Cirq/issues/6422

eliottrosenberg commented 8 months ago

Thanks, Nour!

NoureldinYosri commented 8 months ago

the change happens in https://github.com/quantumlib/Cirq/blob/2ef19094f2225b2f2707d169bafe75ecdcdc0537/cirq-core/cirq/transformers/eject_phased_paulis.py#L212

I don't think this is a bug. the function $f(t) = {CZ}^t$ is periodic with period 2. so if we restrict $t$ to $-1 \leq t \leq 1$ then $f(t) = \overline{f(-t)}$.

Even if I restrict $t$ to that range, deciding between 1 and -1 will be ambiguous. and dropping either end (-1 or 1) is not an option

NoureldinYosri commented 8 months ago

offline update from @eliottrosenberg, it's a bug because in the original example https://github.com/quantumlib/Cirq/issues/6422#issuecomment-1913798978 partial CZs aren't allowed