tensorcircuit icon indicating copy to clipboard operation
tensorcircuit copied to clipboard

Draw circuits with midcircuit-measurement and conditional gates

Open royess opened this issue 1 year ago • 6 comments

Issue Description

Draw circuits with conditional gates. Currently, such attempt will raise a qiskit.extensions.exceptions.ExtensionError: 'Input matrix is not unitary.'

Proposed Solution

I am not very sure whether it is easy to implement by still using the drawing utilities of qiskit. So I do not have very good solution proposed.

Additional References

In qiskit, it is done by introducing classical registers, e.g. https://quantum-computing.ibm.com/lab/docs/iql/manage/systems/midcircuit-measurement/.

royess avatar Jul 19 '22 13:07 royess

This is indeed a known issue, since we have no classical register concepts (which I think is a too heavy abstraction) in tensorcircuit, it is indeed hard to visualize this, especially when integrated with qiskit.

Currently, one workaround to avoid the error is by adding several lines in the codebase: just ignore nonunitary gate when do the qiskit trasformation (I'll fix like this soon). Still, the obtained circuit.draw() will contain no ingredient of cond_gates, though no error raised.

refraction-ray avatar Jul 20 '22 02:07 refraction-ray

Screen Shot 2022-07-20 at 12 52 48 PM results from current solution, any suggestions or ideas?

refraction-ray avatar Jul 20 '22 04:07 refraction-ray

Screen Shot 2022-07-20 at 12 52 48 PM

results from current solution, any suggestions or ideas?

I think this is a good solution at the moment. But just I want to make sure conditional gates (conditional_gate) or other utilities involving Qiskit's classical bits can also work in the same way.

This is indeed a known issue, since we have no classical register concepts (which I think is a too heavy abstraction) in tensorcircuit, it is indeed hard to visualize this, especially when integrated with qiskit.

I agree that classical registers are not necessary and seem like over-abstraction for tensorcircuit. But I think to guarantee tensorcircuit circuit objects can always be run or simulated on practical quantum computers by using Qiskit is also an important feature for some users.

And I am thinking perhaps we can introduce classical registers when translating tensorcircuit to Qiskit by calling to_qir. In this way, tensorcircuit itself remains clean. And I suppose we can also support translating Qiskit object with classical registers to tensorcircuit in a similar manner if it is not supported yet.

royess avatar Jul 20 '22 15:07 royess

Thanks for sharing your thoughts. For now, the translation from cond_measure (which is just a non-unitary gate for any tensorcircuit circuit instance) is translated as an identity gate in qiskit (just with the name measure), therefore, only the visualization is not broken and the measure information is intrinsically lost when translating from tc to qiskit.

As you described, support mid circuit measurement translation between tc and qiskit is very important for users who would like to try some hardware experiments. The difficult part is that, the measurement results is just a int Tensor, and when does this Tensor is utilized in python is hard to track in circuit intermediate representation level. Though this may be solved with some more deep thoughts and refactoring. In a word, the enhancement you suggested is worth a try and indeed helpful but the implementation is not that easy to me for now. Let's keep this issue open and see whether we can have better inputs or ideas from other developers and users

refraction-ray avatar Jul 21 '22 02:07 refraction-ray

Hi! Is there any update regarding the mid circuit measurements? My use case would be to convert a qiskit circuit to TensorCircuit, where the former contains mid circuit measurements (no classical conditional gates)

EmilianoG-byte avatar Feb 02 '24 12:02 EmilianoG-byte

Hi! Is there any update regarding the mid circuit measurements? My use case would be to convert a qiskit circuit to TensorCircuit, where the former contains mid circuit measurements (no classical conditional gates)

Could you provide a minimal python demo on the qiskit circuit, i.e. which circuit object exactly you want to feed into tc.Circuit.from_qiskit()

refraction-ray avatar Feb 03 '24 02:02 refraction-ray