Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

Qasm output doesn't support classical controls

Open Strilanc opened this issue 3 years ago • 2 comments

import cirq

print(cirq.Circuit(
    cirq.measure(cirq.LineQubit(0), key="abc"),
    cirq.X(cirq.LineQubit(0)).with_classical_controls("abc"),
).to_qasm())
ValueError: QASM is defined only for SympyConditions of type key == constant.

Strilanc avatar Jul 08 '22 18:07 Strilanc

As stated in the error message, this will work if converted to a sympy condition. To get it to work for key conditions, we would need to pass in the qid dimension somehow, as this would only be valid for 2D qubits (qasm has no way to express "abc == 1 || abc == 2", nor "abc != 0").

Passing in the dimension will be a big architectural change, as measurement keys don't carry this information at circuit construction time. I'm not sure this will be possible for 1.0.

If qasm doesn't support qudits at all then we could just assume 2D qubits in the output, assuming somewhere else would throw an exception beforehand in the multi-dimensional case. Though even with that, I'm not sure if it would handle multi-qubit measurements correctly.

(Fact is, qasm support will always limited due to differences in how Cirq and Qiskit do measurements. These are highlighted in some of the tests.) xref #5434

daxfohl avatar Jul 10 '22 16:07 daxfohl

I don't know what "converted to a sympy condition" means. The code sample is unambiguous and should work exactly as written. The dimension of all the involved values is clearly 2, because it's using LineQubit. Any ambiguity about that fact is purely a spandrel resulting from the internal implementation failing to explain itself to itself.

This isn't intended as a blocker for 1.0. Just recording that it doesn't work.

Strilanc avatar Jul 10 '22 16:07 Strilanc