quantumlib / Cirq

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

Qasm output doesn't support classical controls #5691

Open Strilanc opened 2 years ago

Strilanc commented 2 years ago
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.
daxfohl commented 2 years ago

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

Strilanc commented 2 years ago

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.