Repository: quantumlib/Cirq

Issue #3678: Remove implicit assumption that Operations have Gates
Created: 2021-01-14T17:20:43Z
Is your feature request related to a use case or problem? Please describe.
The Operation has a gate property which is frequently used to identify its type. However, not all Operations are GateOperations, meaning that this field is not always populated.
Until recently this has been a relatively minor problem, since the vast majority of Operations are GateOperations. However, with the introduction of CircuitOperation (#3580) this issue is becoming more pronounced.
Related issue: #3235.
Describe the solution you'd like
Remove gate field in Operation and replace with a get_operator abstract method, defined in implementations. This will require deprecating the field and replacing it across the Cirq repo.
[optional] Describe alternatives/workarounds you've considered

Add local fixes to sections of code that need to recognize CircuitOperation, and leave the rest unchanged.

Issue: eventually, CircuitOperation should be supported everywhere.


Make Circuit a subclass of Gate, with CircuitOperations containing CircuitGates

Rejected during review (#3509): treating circuits as gates introduces undesirable behavior



What is the urgency from your perspective for this issue? Is it blocking important work?
P1 - I need this no later than the next release (end of quarter)
This confounds work on CircuitOperations, as they do not have a value in their gate field. Continuing work in that space without resolving this issue requires workarounds which will increase the cost of an eventual fix.

Duplicate issue #3723: DensityMatrixSimulator not working with subcircuits
Created: 2021-01-31T17:08:56Z
Description of the issue
It appears density matrix simulator does not work with subcircuits? After looking through the noise simulation code I was trying to check whether noise propagated through subcircuits but I'm unable to even get a measurement. Entirely possible I'm using this incorrectly, or that this is expected behavior.
Note: if changed to the sparse simulator, the test passes.
How to reproduce the issue
    def test_simulate_noise_with_subcircuits():
        q = cirq.LineQubit(0)
        circuit1 = cirq.Circuit(cirq.measure(q))
        circuit2 = cirq.Circuit(cirq.CircuitOperation(cirq.Circuit(cirq.measure(q)).freeze()))

        simulator = cirq.DensityMatrixSimulator(noise=cirq.X)
        result1 = simulator.run(circuit1, repetitions=10)
        result2 = simulator.run(circuit2, repetitions=10)

>       assert result1 == result2
E       AssertionError: assert cirq.Result(params=cirq.ParamResolver({}), measurements={'0': np.array([[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], dtype=np.int8)}) == cirq.Result(params=cirq.ParamResolver({}), measurements={})
E         +cirq.Result(params=cirq.ParamResolver({}), measurements={'0': np.array([[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], dtype=np.int8)})
E         -cirq.Result(params=cirq.ParamResolver({}), measurements={})
Cirq version 0.10.0.dev (latest master)

Closing comment (2021-02-02T20:27:49Z): I think the root cause is that DensityMatrixSimulator considers only ops.MeasurementGate for sampling. The fix would be to use protocols.is_measurement instead of circuit.findall_operations_with_gate_type(ops.MeasurementGate).
This should apply to (almost?) everything in Cirq using MeasurementGate checks. Related to #3678.