I'm working on different frontend/backend combination examples and ran into the following important consideration. Consider the simple example:
import qiskit
from mitiq.folding import fold_gates_from_left
circuit = qiskit.QuantumCircuit(...) # Some qiskit circuit
folded = fold_gates_from_left(circuit, stretch=2.)
This raises an error of course as folding methods are defined for cirq.Circuit
s. However, it would be very convenient to be able to run this code. If we could run this code, all of the conversions could be handled in the folding methods, and very little changes, if any, would be needed elsewhere to handle conversions.
However, I don't see a way to do this without requiring Qiskit/pyQuil/etc. in the main mitiq library. This is simply because we need to check what type the circuit is in order to convert it properly.
Option 1: Require Qiskit/pyQuil/etc. as dependencies
This fixes the issue, but is rather inelegant.
Option 2: Check the type without dependencies
Check what type the circuit is without requiring all dependencies, then call the appropriate conversion code. This seems like magic to me, but if it could be done that would be amazing. (Can it be done with typing._ForwardRef
? I don't think so, but I'm looking into it now.)
Option 3: Frontend-specific folding methods
One way to do this which avoids requiring Qiskit/pyQuil/etc. in the main mitiq library is to define folding functions in mitiq/qiskit
, mitiq/pyquil
, mitiq/etc.
These folding functions would do the conversions then call the internal folding functions using Cirq circuits. So the example above would be modified to
import qiskit
from mitiq.qiskit.folding import fold_gates_from_left
circuit = qiskit.QuantumCircuit(...) # Some qiskit circuit
folded = fold_gates_from_left(circuit, stretch=2.)
This is a bit clunky, and I still think mitiq would have to depend on Qiskit/pyQuil/etc. in order to run functions in zne.py
properly.
Conclusion
I'm still trying to think of the best way to handle this. Any feedback is welcome, specifically on Option 2 as this would be very nice if its possible. Additional ideas welcome.