Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.08k stars 2.34k forks source link

Single classical bit conditioning breaks various functions #6475

Open TharrmashasthaPV opened 3 years ago

TharrmashasthaPV commented 3 years ago

Information

What is the current behavior?

The PR #6018 enables classical conditioning of gates on individual bits. However, as mentioned in that PR, this new feature breaks the following functions:

Suggested solutions

The fixes should be simple. Need to look for parts that have ClassicalRegister in condition in these functions and extend them also to cases when Clbit is in condition.

mtreinish commented 3 years ago

I just added qpy serialization support to the list of things here. It was pointed out to me that trying to dump (and likely load too) a circuit with a single bit condition won't serialize correctly and error. The source is this line:

https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/circuit/qpy_serialization.py#L699

because a Clbit object doesn't have a name. We might need a qpy format v2 to adjust the condition representation in the serialization format (https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/circuit/qpy_serialization.py#L177-L179 ) to handle the lack of a register. Although, thinking out loud we may be able to hack it in by setting the name to be a the str(index) of the clbit in the circuit since that is not a valid register name.

jakelishman commented 3 years ago

As I understand it, QASM 2 doesn't support conditional operations on anything other than a complete register, so the circuit

import qiskit.circuit.qpy_serialization
from qiskit.circuit import QuantumRegister, QuantumCircuit, ClassicalRegister, Clbit
qr = QuantumRegister(2)
cr = ClassicalRegister(8)
qc = QuantumCircuit(qr, cr)
qc.measure(0, cr[0])
qc.measure(1, cr[1])
qc.x(0).c_if(cr[0], 1)
qc.x(1).c_if(cr, 3)

can't be translated into valid QASM 2. I had intended to go around this by emitting additional 1-bit classical registers and copying the data, but there's also no classical assignment operation, so that doesn't work either.

In theory it is possible to emit valid QASM if the only classical bits used aren't attached to registers, though - instead of binding them all into a single creg regless[...]; we separate them. I don't know if we want to put the effort into supporting this if we can't also support subscripts of ClassicalRegister?

I suspect the "best" solution to this within the framework of QASM 2 is just to emit QasmError in this case? It doesn't seem very satisfying, though.

HuangJunye commented 2 years ago

@TharrmashasthaPV we are planning to release 0.20 soon. Are you planning to fix the rest of issues listed here?