tencent-quantum-lab / tensorcircuit

Tensor network based quantum software framework for the NISQ era
https://tensorcircuit.readthedocs.io
Apache License 2.0
280 stars 79 forks source link

Draw circuits with midcircuit-measurement and conditional gates #50

Open royess opened 2 years ago

royess commented 2 years ago

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/.

refraction-ray commented 2 years ago

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 commented 2 years ago
Screen Shot 2022-07-20 at 12 52 48 PM

results from current solution, any suggestions or ideas?

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

refraction-ray commented 2 years ago

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

EmilianoG-byte commented 9 months ago

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)

refraction-ray commented 9 months ago

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()