Make cirq.final_density_matrix work on classical controls
Is your feature request related to a use case or problem? Please describe.
The ignore_measurement_results argument of final_density_matrix is used for calculating the final density matrix without ever collapsing the DM due to a measurement. However this causes the function to fail when presented with a classically controlled operation.
Describe the solution you'd like
This can be fixed by first passing the circuit through the defer_measurements transformer, before passing through the dephase_measurements transformer. The simulator can then run on this circuit. The produced DM will include axes for the ancilla qubits that were added in the deferral transform. These can be removed by doing a partial_trace and only retaining the axes corresponding to the original circuit's qubits.
Note that within final_density_matrix we'd also have to apply the noise model explicitly, prior to calling defer_measurements, instead of just passing it into the simulator. Otherwise any noise would be applied to the ancilla "measurement qubits" as well, which is not desired.
(good for learning, good first issue with some guidance) I'll leave this open for a while if someone wants to contribute.
[optional] Describe alternatives/workarounds you've considered
We could build this functionality into the simulators themselves. This would be a lot longer project and I imagine could get quite gnarly. Though a possible approach for that was prototyped and discussed here https://github.com/quantumlib/Cirq/issues/6040#issuecomment-1488956215
[optional] Additional context (e.g. screenshots)
What is the urgency from your perspective for this issue? Is it blocking important work?
P3 - I'm not really blocked by it, it is an idea I'd like to discuss / suggestion based on principle
Hi all,
I can take this up. Will reach out in case I have any questions. Upd: Looks like I used the google github account to comment here. Can you please reassign it to @TarunSinghania.
Hi! If no one else is working on this, I would like to pick it up.
Yes, please feel free to pick it up.