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

Enable pulling through Clifford operations, also add an option of only applying dd to single qubit gate moments #6675

Closed babacry closed 1 month ago

babacry commented 2 months ago

Enable adding dynamical decoupling in single qubit gate moments only.

Adding mechanism is described in the code. In short:

For each qubit:
      For moments in (first_active_moment, last_active_single_qubit_moment):
            Insert
      Absorb the inverse to the last_active_single_qubit_moment

2 test cases added for single qubit gates moments insertion mode:

Test case 1: add dd preserves the circuit.

0: ───────────────────────────────H───@───
                                      │
1: ───────────────────────H───@───H───X───
                              │
2: ───────────────H───@───H───X───────────
                      │
3: ───H───────@───H───X───────────────────
              │
4: ───H───@───X───────────────────────────
          │
5: ───H───X───────H───@───────────────────
                      │
6: ───────────────H───X───H───@───────────
                              │
7: ───────────────────────H───X───H───@───
                                      │
8: ───────────────────────────────H───X───

Test case 2:

input_circuit's diagram is:

0: ───────────────────────────────H───@───H───
                                      │
1: ───────────────────────H───@───H───X───H───
                              │
2: ───────────────H───@───H───X───────────H───
                      │
3: ───H───────@───H───X───────────────────H───
              │
4: ───H───@───X───────────────────────────H───
          │
5: ───H───X───────H───@───────────────────H───
                      │
6: ───────────────H───X───H───@───────────H───
                              │
7: ───────────────────────H───X───H───@───H───
                                      │
8: ───────────────────────────────H───X───H───

output diagram is

0: ───────────────────────────────H───@───H────────────────────────
                                      │
1: ───────────────────────H───@───H───@───H────────────────────────
                              │
2: ───────────────H───@───H───@───X───────PhXZ(a=-0.5,x=0.5,z=0)───
                      │
3: ───H───────@───H───@───X───────X───────H────────────────────────
              │
4: ───H───@───@───X───────X───────X───────PhXZ(a=-0.5,x=0.5,z=0)───
          │
5: ───H───@───────H───@───X───────X───────H────────────────────────
                      │
6: ───────────────H───@───H───@───X───────PhXZ(a=-0.5,x=0.5,z=0)───
                              │
7: ───────────────────────H───@───H───@───H────────────────────────
                                      │
8: ───────────────────────────────H───@───H────────────────────────
codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 97.83%. Comparing base (e2e8aef) to head (74305a0). Report is 2 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #6675 +/- ## ======================================== Coverage 97.83% 97.83% ======================================== Files 1075 1077 +2 Lines 92325 92493 +168 ======================================== + Hits 90324 90492 +168 Misses 2001 2001 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

eliottrosenberg commented 2 months ago

I suspect that the bug is in the logic of how to pull Pauli gates through Clifford gates (e.g. cirq.CZ)

babacry commented 1 month ago
cirq.testing.assert_circuits_have_same_unitary_given_final_permutation(
        input_circuit, transformed_circuit, {q: q for q in input_circuit.all_qubits()}
    )

and

sampler = cirq.Simulator(dtype=np.complex128)
psi0 = sampler.simulate(input_circuit).final_state_vector
psi1 = sampler.simulate(transformed_circuit).final_state_vector
assert np.isclose(np.abs(np.vdot(psi0, psi1)) ** 2, 1.0)

both passed for all test cases.

babacry commented 1 month ago

Now, we rely on cirq.has_stabilizer_effect to determine if an operation is Clifford. While, cirq.has_stabilizer_effect(cirq.M) returns True, resulting in a polluted clifford piece. Add a criteria for function _is_clifford_moment and the test case in the comment above.

babacry commented 1 month ago

Also changed default schema in the new commit.